Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Player Movement and Boundary Control | Building Arcade Game
Building Arcade Game
course content

Contenido del Curso

Building Arcade Game

bookPlayer 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.

Tarea

  1. Detect key presses: Use keys = pygame.key.get_pressed();
  2. Move down: if K_DOWN pressed and not at bottom, player_rect.move(player_move_down);
  3. Move up: if K_UP pressed and not at top, player_rect.move(player_move_up);
  4. Move right: if K_RIGHT pressed and not at right edge, player_rect.move(player_move_right);
  5. Move left: if K_LEFT pressed and not at left edge, player_rect.move(player_move_left).

Mark tasks as Completed
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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.

Tarea

  1. Detect key presses: Use keys = pygame.key.get_pressed();
  2. Move down: if K_DOWN pressed and not at bottom, player_rect.move(player_move_down);
  3. Move up: if K_UP pressed and not at top, player_rect.move(player_move_up);
  4. Move right: if K_RIGHT pressed and not at right edge, player_rect.move(player_move_right);
  5. Move left: if K_LEFT pressed and not at left edge, player_rect.move(player_move_left).

Mark tasks as Completed
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 1. Capítulo 10
AVAILABLE TO ULTIMATE ONLY
some-alt