Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Managing Game Events | PyGame Project: Arcade Game
PyGame Project: Arcade Game
course content

Зміст курсу

PyGame Project: Arcade Game

bookManaging Game Events

In this section of our game's code, we're setting up some important elements that bring our game to life. Let's break it down into simpler terms:

Creating Timed Events for Enemies and Bonuses

  • CREATE_ENEMY = pygame.USEREVENT + 1: We're creating a special event for adding enemies. pygame.USEREVENT is a unique kind of event in PyGame that lets us create custom events. By adding + 1, we make sure this event is distinct; -pygame.time.set_timer(CREATE_ENEMY, 1500): This tells PyGame to trigger our CREATE_ENEMY event every 1500 milliseconds (or 1.5 seconds), so our game knows it's time to add a new enemy at these intervals;
  • CREATE_BONUS = pygame.USEREVENT + 2: Similar to the enemy event, but this time for bonuses. We add + 2 to create another unique event;
  • pygame.time.set_timer(CREATE_BONUS, 3000): Sets up the CREATE_BONUS event to occur every 3000 milliseconds (or 3 seconds), signaling when to add a new bonus.

Setting Up Game Variables

  • enemies = []: Initializes an empty list to keep track of all the enemies that appear in the game;
  • bonuses = []: Similarly, this list will hold all the bonuses that appear;
  • score = 0: Starts the player's score at 0. As players progress, this will increase.

Starting the Game Loop

  • playing = True: This variable controls the main game loop. As long as playing is True, the game will continue running. This loop handles events, updates the game state, and draws everything on the screen.

By setting these elements, we're preparing our game to dynamically add challenges (enemies) and rewards (bonuses), keep track of the player's progress (score), and ensure the game runs as intended (with the playing condition).

Завдання

  1. Create unique events for generating enemies and bonuses in the game with CREATE_ENEMY = pygame.USEREVENT + 1 and CREATE_BONUS = pygame.USEREVENT + 2;
  2. Set up timers to trigger these events regularly using pygame.time.set_timer(CREATE_ENEMY, 1500) for enemies every 1.5 seconds and pygame.time.set_timer(CREATE_BONUS, 3000) for bonuses every 3 seconds;
  3. Initialize empty lists enemies = [] and bonuses = [] to store the enemies and bonuses that will appear in the game;
  4. Initialize the player's score with score = 0, starting from zero;
  5. Set playing = True to begin the game loop, keeping the game running as long as this condition is True.

Mark tasks as Completed
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

In this section of our game's code, we're setting up some important elements that bring our game to life. Let's break it down into simpler terms:

Creating Timed Events for Enemies and Bonuses

  • CREATE_ENEMY = pygame.USEREVENT + 1: We're creating a special event for adding enemies. pygame.USEREVENT is a unique kind of event in PyGame that lets us create custom events. By adding + 1, we make sure this event is distinct; -pygame.time.set_timer(CREATE_ENEMY, 1500): This tells PyGame to trigger our CREATE_ENEMY event every 1500 milliseconds (or 1.5 seconds), so our game knows it's time to add a new enemy at these intervals;
  • CREATE_BONUS = pygame.USEREVENT + 2: Similar to the enemy event, but this time for bonuses. We add + 2 to create another unique event;
  • pygame.time.set_timer(CREATE_BONUS, 3000): Sets up the CREATE_BONUS event to occur every 3000 milliseconds (or 3 seconds), signaling when to add a new bonus.

Setting Up Game Variables

  • enemies = []: Initializes an empty list to keep track of all the enemies that appear in the game;
  • bonuses = []: Similarly, this list will hold all the bonuses that appear;
  • score = 0: Starts the player's score at 0. As players progress, this will increase.

Starting the Game Loop

  • playing = True: This variable controls the main game loop. As long as playing is True, the game will continue running. This loop handles events, updates the game state, and draws everything on the screen.

By setting these elements, we're preparing our game to dynamically add challenges (enemies) and rewards (bonuses), keep track of the player's progress (score), and ensure the game runs as intended (with the playing condition).

Завдання

  1. Create unique events for generating enemies and bonuses in the game with CREATE_ENEMY = pygame.USEREVENT + 1 and CREATE_BONUS = pygame.USEREVENT + 2;
  2. Set up timers to trigger these events regularly using pygame.time.set_timer(CREATE_ENEMY, 1500) for enemies every 1.5 seconds and pygame.time.set_timer(CREATE_BONUS, 3000) for bonuses every 3 seconds;
  3. Initialize empty lists enemies = [] and bonuses = [] to store the enemies and bonuses that will appear in the game;
  4. Initialize the player's score with score = 0, starting from zero;
  5. Set playing = True to begin the game loop, keeping the game running as long as this condition is True.

Mark tasks as Completed
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 1. Розділ 7
AVAILABLE TO ULTIMATE ONLY
some-alt