Reputation: 652
Hi I am attempting to make a platformer where the edges of the screen act as walls. However, the problem is that the player goes through the wall. Here is the function that checks for walls:
def check_walls(self):
if self.rect.right >= WIDTH:
self.speedx = 0.5
if self.rect.left >= WIDTH - WIDTH:
self.speedx = -0.5
And here is the code that makes the player move, I think this is the piece of code that is actually causing the problem but I don't know how to fix it:
def update(self):
self.speedx = 0
# The speedy is above in the __init__ for the player jumping movement
self.onGround = True
keystate = pygame.key.get_pressed()
if self.rect.y <= GROUND - BLOCK_DIM:
self.speedy += self.gravity
self.onGround = False
elif self.rect.y >= GROUND - BLOCK_DIM:
self.speedy = 0
self.onGround = True
if keystate[pygame.K_LEFT]:
self.speedx = -5
if keystate[pygame.K_RIGHT]:
self.speedx = 5
if keystate[pygame.K_UP]:
self.jump()
self.rect.x += self.speedx
self.rect.y += self.speedy
Is there a better method to check for collisions between the player and the wall, take in mind that the walls are just the screen borders not sprites. Also the check_walls() is called in the init which is not added here
Upvotes: 0
Views: 84
Reputation: 10740
I think you meant to do:
def check_walls(self):
if self.rect.right >= WIDTH:
self.speedx = -0.5
if self.rect.left <= 0:
self.speedx = 0.5
also where do you call check_walls?
Upvotes: 1