I have a map im creating in python using pygame with a player and walls:
import pygame
x = 25
y = 19
collision = [
[0,0,0,37],#vertical
[4,23,4,27],
[12,18,12,25],
[13,0,13,1],
[13,4,13,7],
[13,10,13,11],
[15,13,15,18],
[15,23,15,37],
[19,0,19,13],
[29,25,29,26],
[29,29,29,37],
[35,0,35,9],
[35,12,35,17],
[35,21,35,26],
[35,29,35,37],
[36,17,36,21],
[44,0,44,6],
[44,10,44,17],
[54,0,54,17],
[0,0,19,0],#horizontal
[35,0,46,0],
[52,0,54,0],
[13,5,19,5],
[19,6,24,6],
[30,6,35,6],
[0,13,10,13],
[15,13,21,13],
[25,13,44,13],
[35,17,36,17],
[44,17,54,17],
[35,21,36,21],
[4,23,12,23],
[0,25,4,25],
[19,25,36,25],
[4,27,12,27],
[0,37,35,37],
[14,12,14,12],#dots
[11,14,11,14]
]
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
pygame.init()
screen = pygame.display.set_mode((200,200))
pygame.display.set_caption('Collision')
screen.fill(white)
for list in collision:#draw map
pygame.draw.line(screen,black,(list[0],list[1]),(list[2],list[3]),1)
pygame.draw.line(screen,red,(x,y),(x,y),1)#draw player
pygame.display.update()
while(True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
for list in collision:
#---START OF EQUATION---#
if(list[0]<=list[2] and not(y-1==list[1] and x>=list[0] and x<=list[2])) or (list[0]>list[2] and not(y-1==list[1] and x>=list[2] and x<=list[0])):#updated
#---END OF EQUATION---#
pygame.draw.line(screen,white,(x,y),(x,y),1)
y-=1
pygame.draw.line(screen,red,(x,y),(x,y),1)
break
elif event.key == pygame.K_s:
for list in collision:
if(list[0]<=list[2] and not(y+1==list[1] and x>=list[0] and x<=list[2])) or (list[0]>list[2] and not(y+1==list[1] and x>=list[2] and x<=list[0])):#updated
pygame.draw.line(screen,white,(x,y),(x,y),1)
y+=1
pygame.draw.line(screen,red,(x,y),(x,y),1)
break
elif event.key == pygame.K_a:
for list in collision:
if(list[1]<=list[3] and not(x-1==list[0] and y>=list[1] and y<=list[3])) or (list[1]>list[3] and not(x-1==list[0] and y>=list[3] and y<=list[1])):#updated
pygame.draw.line(screen,white,(x,y),(x,y),1)
x-=1
pygame.draw.line(screen,red,(x,y),(x,y),1)
break
elif event.key == pygame.K_d:
for list in collision:
if(list[1]<=list[3] and not(x+1==list[0] and y>=list[1] and y<=list[3])) or (list[1]>list[3] and not(x+1==list[0] and y>=list[3] and y<=list[1])):#updated
pygame.draw.line(screen,white,(x,y),(x,y),1)
x+=1
pygame.draw.line(screen,red,(x,y),(x,y),1)
break
pygame.display.update()
The equation, in an 'easier' to read format:
if (
list[0]<=list[2]
and not(
y-1==list[1]
and
x>=list[0]
and
x<=list[2])
)
or
(
list[0]>list[2]
and not(
y-1==list[1]
and
x>=list[2]
and
x<=list[0]
)
)
Down where keypresses are being detected is where im having an issue. I have tested this method several times, but it always results in the wrong functionality.
I am having my code loop through a list of 'walls' every time the user presses w/a/s/d, and tests if they are capable of going through that space if no wall is present. Lets say that there is a wall like this here with the red dot being the player. How can I prevent the player from moving up or to the left with modification of the equation?
Equation thats being used to test for collision (is different for every direction, in this case is for the up direction):
if(list[0]<=list[2] and not(y-1==list[1] and x>=list[0] and x<=list[2])) or (list[0]>list[2] and not(y-1==list[1] and x>=list[2] and x<=list[0]))