Course Content
PyGame Project: Arcade Game
PyGame Project: Arcade Game
Player Movement and Boundary Control
In this part of the code, we're making our player character move around the screen based on keyboard inputs. Here's a simple explanation of what each part does:
Detecting Key Presses
keys = pygame.key.get_pressed()
checks which keys are being pressed down at this moment. It gives us a way to react to player input.
Moving Down
if keys[K_DOWN] and player_rect.bottom < HEIGHT
: checks two things: if the down arrow key is pressed and if the player is not already at the bottom edge of the screen (HEIGHT). If both are true, it moves the player down by applying player_move_down to player_rect.
Moving Up
if keys[K_UP] and player_rect.top > 0
: checks if the up arrow key is pressed and ensures the player isn't at the top edge of the screen. If so, the player moves up according to player_move_up.
Moving Right
if keys[K_RIGHT] and player_rect.right < WIDTH
: ensures the right arrow key is pressed and the player isn't at the right edge (WIDTH). It moves the player right by using player_move_right.
Moving Left
if keys[K_LEFT] and player_rect.left > 0
: checks for the left arrow key press and that the player isn't at the left edge of the screen. The player moves left with player_move_left.
Each if statement is a condition that, when met, moves the player's rectangle (player_rect
) in the desired direction, ensuring the player stays within the screen bounds.
Swipe to show code editor
- Detect key presses: Use
keys = pygame.key.get_pressed()
; - Move down: if K_DOWN pressed and not at bottom,
player_rect.move(player_move_down)
; - Move up: if K_UP pressed and not at top,
player_rect.move(player_move_up)
; - Move right: if K_RIGHT pressed and not at right edge,
player_rect.move(player_move_right)
; - Move left: if K_LEFT pressed and not at left edge,
player_rect.move(player_move_left)
.
Thanks for your feedback!
In this part of the code, we're making our player character move around the screen based on keyboard inputs. Here's a simple explanation of what each part does:
Detecting Key Presses
keys = pygame.key.get_pressed()
checks which keys are being pressed down at this moment. It gives us a way to react to player input.
Moving Down
if keys[K_DOWN] and player_rect.bottom < HEIGHT
: checks two things: if the down arrow key is pressed and if the player is not already at the bottom edge of the screen (HEIGHT). If both are true, it moves the player down by applying player_move_down to player_rect.
Moving Up
if keys[K_UP] and player_rect.top > 0
: checks if the up arrow key is pressed and ensures the player isn't at the top edge of the screen. If so, the player moves up according to player_move_up.
Moving Right
if keys[K_RIGHT] and player_rect.right < WIDTH
: ensures the right arrow key is pressed and the player isn't at the right edge (WIDTH). It moves the player right by using player_move_right.
Moving Left
if keys[K_LEFT] and player_rect.left > 0
: checks for the left arrow key press and that the player isn't at the left edge of the screen. The player moves left with player_move_left.
Each if statement is a condition that, when met, moves the player's rectangle (player_rect
) in the desired direction, ensuring the player stays within the screen bounds.
Swipe to show code editor
- Detect key presses: Use
keys = pygame.key.get_pressed()
; - Move down: if K_DOWN pressed and not at bottom,
player_rect.move(player_move_down)
; - Move up: if K_UP pressed and not at top,
player_rect.move(player_move_up)
; - Move right: if K_RIGHT pressed and not at right edge,
player_rect.move(player_move_right)
; - Move left: if K_LEFT pressed and not at left edge,
player_rect.move(player_move_left)
.