Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Managing Game Flow and Dynamic Elements | Building Arcade Game
Building Arcade Game
course content

Contenido del Curso

Building Arcade Game

bookManaging Game Flow and Dynamic Elements

In this piece of our game's code, we have a loop that keeps the game running and interacts with events like quitting the game, creating enemies, and creating bonuses. Here's how it works, broken down for someone just starting with Python:

Keep the game running

  • The while playing: loop continues as long as the playing variable is True. Inside this loop, the game updates and responds to events.
  • Control the game speed: FPS.tick(240) is used to limit how fast the game runs, setting it to a maximum of 240 frames per second. You can try different values; This helps make the game run smoothly on different computers;
  • Check for events: The loop for event in pygame.event.get(): looks at all the events that have happened (like key presses or mouse clicks) since the last time it checked;
  • Quit the game: If the player closes the game window (if event.type == QUIT:), we set playing = False, which will stop the while loop and end the game. Use pygame.quit() method to close the game window;
  • Create an enemy: Whenever our custom CREATE_ENEMY event happens (set up earlier to occur every 1.5 seconds), create_enemy() is called to make a new enemy, and then it's added to the enemies list with enemies.append(create_enemy());
  • Create a bonus: Similarly, when the CREATE_BONUS event happens (set up to occur every 3 seconds), create_bonus() is called to create a bonus item, and it's added to the bonuses list with bonuses.append(create_bonus()).

This code segment effectively manages the main gameplay loop, ensuring the game continues to run, responds to player actions like closing the game, and dynamically adds challenges (enemies) and rewards (bonuses) as the game progresses.

Tarea

  1. Use while playing: to keep the game running;
  2. FPS.tick(240) ensures the game runs smoothly;
  3. Loop through pygame.event.get() to find game events;
  4. If QUIT event occurs, set playing = False to stop the game;
  5. On CREATE_ENEMY event, generate and add an enemy with enemies.append(create_enemy());
  6. On CREATE_BONUS event, generate and add a bonus with bonuses.append(create_bonus()).

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 piece of our game's code, we have a loop that keeps the game running and interacts with events like quitting the game, creating enemies, and creating bonuses. Here's how it works, broken down for someone just starting with Python:

Keep the game running

  • The while playing: loop continues as long as the playing variable is True. Inside this loop, the game updates and responds to events.
  • Control the game speed: FPS.tick(240) is used to limit how fast the game runs, setting it to a maximum of 240 frames per second. You can try different values; This helps make the game run smoothly on different computers;
  • Check for events: The loop for event in pygame.event.get(): looks at all the events that have happened (like key presses or mouse clicks) since the last time it checked;
  • Quit the game: If the player closes the game window (if event.type == QUIT:), we set playing = False, which will stop the while loop and end the game. Use pygame.quit() method to close the game window;
  • Create an enemy: Whenever our custom CREATE_ENEMY event happens (set up earlier to occur every 1.5 seconds), create_enemy() is called to make a new enemy, and then it's added to the enemies list with enemies.append(create_enemy());
  • Create a bonus: Similarly, when the CREATE_BONUS event happens (set up to occur every 3 seconds), create_bonus() is called to create a bonus item, and it's added to the bonuses list with bonuses.append(create_bonus()).

This code segment effectively manages the main gameplay loop, ensuring the game continues to run, responds to player actions like closing the game, and dynamically adds challenges (enemies) and rewards (bonuses) as the game progresses.

Tarea

  1. Use while playing: to keep the game running;
  2. FPS.tick(240) ensures the game runs smoothly;
  3. Loop through pygame.event.get() to find game events;
  4. If QUIT event occurs, set playing = False to stop the game;
  5. On CREATE_ENEMY event, generate and add an enemy with enemies.append(create_enemy());
  6. On CREATE_BONUS event, generate and add a bonus with bonuses.append(create_bonus()).

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 8
AVAILABLE TO ULTIMATE ONLY
some-alt