Walls In Pygame
I am trying to make a simple Pac-Man game in Pygame, but I don't know how to make walls. How I can make check if player hit a wall when it moves? This is the code of main.py : # Im
Solution 1:
Before you move the player to a new position, you need to iterate over all elements in WALLS
and check whether the bounding box of the player would intersect with the wall. pygame.Rect
has a number of helper methods for this (Rect.collide*
)
If it does, then reject the move.
Solution 2:
You could check for collisions against the walls in move() by using a.colliderect(b)
#Exampledefmove(self, speed, walls):
# Move the playerif playerRect.colliderect(wallRect) == False:
if self.direction == 'up':
self.y -= speed
if playerRect.colliderect(wallRect) == True:
#Deny the movement
Something along these lines
Post a Comment for "Walls In Pygame"