What I tried to do was to verify if the positions of the two objects were equal, but this way if the objects are only near each other (ex: 5 pixels apart) the collision isn't detected. I want to create some sort of area around the moving objects and if my character enters that area the game recognises the collision.
TX = 1*size
TY = 14*size
W1X = 0
W1Y = 5*size
W2X = 2*size
W2Y = 0
W7X = 7*size
W7Y = 0
W8X = 0
W8Y = 11*size
columns = [size, 2*size, 3*size, 4*size, 5*size, 6*size, 7*size, 8*size, 9*size,
10*size, 11*size, 12*size, 13*size, 14*size, 15*size]
def board():
screen.blit(bg, (0,0))
for a in range(len(tiles)):
for b in range(len(tiles[0])):
if tiles[a][b]==2:
screen.blit(rp, (b*size, a*size))
screen.blit(pp, (TX, TY-5))
screen.blit(w1, (W1X, W1Y-7))
screen.blit(w1, (W2X, W2Y-7))
screen.blit(w1, (W7X, W7Y-7))
screen.blit(w1, (WX, W8Y-7))
playing=True
while playing:
clock = pygame.time.Clock()
clock.tick(20)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key==pygame.K_q:
pygame.quit()
elif event.key==pygame.K_w:
dir="Up"
elif event.key==pygame.K_s:
dir="Down"
elif event.key==pygame.K_a:
dir="Left"
elif event.key==pygame.K_d:
dir="Right"
if dir=="Up":
if valid_move((TY-15)//size, (TX)//size):
TY-=15
if dir=="Down":
if valid_move((TY+45)//size, (TX)//size):
TY+=15
if dir=="Right":
if valid_move((TY)//size, (TX+45)//size):
TX+=15
if dir=="Left":
if valid_move((TY)//size, (TX-15)//size):
TX-=15
if tiles[TY//size][(TX)//size]==2:
points+=1
tiles[TY//size][(TX)//size]=0
if W8X < 16*size:
W8X += 10
if W8X == 16*size:
W8X = 0
W8Y = random.choice(columns)
if W1X < 16*size:
W1X += 10
if W1X == 16*size:
W1X = 0
W1Y = random.choice(columns)
if W2Y < 16*size:
W2Y += 10
if W2Y == 16*size:
W2Y = 0
W2X = random.choice(columns)
if W7Y < 16*size:
W7Y += 10
if W7Y == 16*size:
W7Y = 0
W7X = random.choice(columns)
if TX==W8X and TY==W8Y:
playing=False
...
board()
pygame.display.update()
game_over()