I am trying to implement Collision in my game but the way I am doing it right now the player gets stuck at the hitbox and I can't figure out how to do it. The object I am currently trying to collide with is wall1. For sake of clarity, I have only added the Wall object and the main function
class wall(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
#self.rect = pygame.Rect(x, y, width, height)
global ColliderRect
ColliderRect = pygame.Rect(x, y, width, height)
def playerCollide(self):
if (man.x + man.vel > self.x - 45 and man.y + man.vel > self.y -50 and man.y + man.vel < self.y+self.height and man.x + man.vel < self.x + self.width):
return True
else:
return False
#mainloop
collider1 = wall(500, 400, 200, 200)
man = player(200, 410, 64,64)
run = True
while run:
clock.tick(60)
wall1 = collider1.playerCollide()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and man.x > man.vel and wall1== False:
man.x -= man.vel
man.left = True
man.right = False
print(wall1)
elif keys[pygame.K_d] and man.x < scrWidth - man.width - man.vel and wall1 == False:
man.x += man.vel
man.right = True
man.left = False
print(wall1)
elif keys[pygame.K_w] and man.y > man.vel and wall1 == False:
man.y -= man.vel
man.left = False
man.right = False
print(wall1)
elif keys[pygame.K_s] and man.y < scrHeight - man.height - man.vel and wall1 == False:
man.y += man.vel
man.left = False
man.right = False
print(wall1)
else:
man.right = False
man.left = False
man.walkCount = 0
if (wall1 == True):
wall1 = False
man.hitbox(15, 0, 31, 17)
#man.drawhitbox()
redrawGameWindow()
pygame.quit()