Course Content
PyGame Project: Arcade Game
PyGame Project: Arcade Game
Managing 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).
Task
- Create unique events for generating enemies and bonuses in the game with
CREATE_ENEMY = pygame.USEREVENT + 1 and CREATE_BONUS = pygame.USEREVENT + 2
; - Set up timers to trigger these events regularly using
pygame.time.set_timer(CREATE_ENEMY, 1500)
for enemies every 1.5 seconds andpygame.time.set_timer(CREATE_BONUS, 3000)
for bonuses every 3 seconds; - Initialize empty lists
enemies = []
andbonuses = []
to store the enemies and bonuses that will appear in the game; - Initialize the player's score with
score = 0
, starting from zero; - Set
playing = True
to begin the game loop, keeping the game running as long as this condition is True.
Thanks for your feedback!
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).
Task
- Create unique events for generating enemies and bonuses in the game with
CREATE_ENEMY = pygame.USEREVENT + 1 and CREATE_BONUS = pygame.USEREVENT + 2
; - Set up timers to trigger these events regularly using
pygame.time.set_timer(CREATE_ENEMY, 1500)
for enemies every 1.5 seconds andpygame.time.set_timer(CREATE_BONUS, 3000)
for bonuses every 3 seconds; - Initialize empty lists
enemies = []
andbonuses = []
to store the enemies and bonuses that will appear in the game; - Initialize the player's score with
score = 0
, starting from zero; - Set
playing = True
to begin the game loop, keeping the game running as long as this condition is True.