Course Content
PyGame Project: Arcade Game
PyGame Project: Arcade Game
Handling Enemy and Bonus Interactions
This code handles the movement and interactions of enemies and bonuses within the game. It updates their positions, checks for collisions with the player, and updates the game state accordingly. Here’s a breakdown:
Moving and Drawing Enemies
- Loop through enemies:
for enemy in enemies:
goes through each enemy in the list of enemies; - Move enemy:
enemy[1] = enemy[1].move(enemy[2])
updates the enemy's position based on its movement vector; - Draw enemy:
main_display.blit(enemy[0], enemy[1])
draws the enemy on the screen at its updated position; - Check for collision:
if player_rect.colliderect(enemy[1])
, it means the player and this enemy have collided, soplaying = False
stops the game;
Moving and Drawing Bonuses
- Loop through bonuses:
for bonus in bonuses:
iterates over each bonus in the bonuses list; - Move bonus:
bonus[1] = bonus[1].move(bonus[2])
updates the bonus's position according to its movement direction; - Draw bonus:
main_display.blit(bonus[0], bonus[1])
displays the bonus on the screen where it currently is; - Collision with player:
if player_rect.colliderect(bonus[1])
, this indicates the player has collected the bonus. The player's score increases by 1, and the collected bonus is removed from the list withbonuses.pop(bonuses.index(bonus))
.
This section of code is crucial for adding dynamism and interaction to the game, moving enemies and bonuses across the screen, and handling the consequences of player collisions with these elements.
Swipe to show code editor
- Update enemy positions:
enemy[1].move(enemy[2])
; - Display enemies:
main_display.blit(enemy[0], enemy[1])
; - Check player-enemy collision:
if player_rect.colliderect(enemy[1])
, setplaying = False
; - Update bonus positions:
bonus[1].move(bonus[2])
; - Display bonuses:
main_display.blit(bonus[0], bonus[1])
; - Score and remove bonus on collection:
if player_rect.colliderect(bonus[1])
, increase score and remove the bonus from bonuses.
Thanks for your feedback!
This code handles the movement and interactions of enemies and bonuses within the game. It updates their positions, checks for collisions with the player, and updates the game state accordingly. Here’s a breakdown:
Moving and Drawing Enemies
- Loop through enemies:
for enemy in enemies:
goes through each enemy in the list of enemies; - Move enemy:
enemy[1] = enemy[1].move(enemy[2])
updates the enemy's position based on its movement vector; - Draw enemy:
main_display.blit(enemy[0], enemy[1])
draws the enemy on the screen at its updated position; - Check for collision:
if player_rect.colliderect(enemy[1])
, it means the player and this enemy have collided, soplaying = False
stops the game;
Moving and Drawing Bonuses
- Loop through bonuses:
for bonus in bonuses:
iterates over each bonus in the bonuses list; - Move bonus:
bonus[1] = bonus[1].move(bonus[2])
updates the bonus's position according to its movement direction; - Draw bonus:
main_display.blit(bonus[0], bonus[1])
displays the bonus on the screen where it currently is; - Collision with player:
if player_rect.colliderect(bonus[1])
, this indicates the player has collected the bonus. The player's score increases by 1, and the collected bonus is removed from the list withbonuses.pop(bonuses.index(bonus))
.
This section of code is crucial for adding dynamism and interaction to the game, moving enemies and bonuses across the screen, and handling the consequences of player collisions with these elements.
Swipe to show code editor
- Update enemy positions:
enemy[1].move(enemy[2])
; - Display enemies:
main_display.blit(enemy[0], enemy[1])
; - Check player-enemy collision:
if player_rect.colliderect(enemy[1])
, setplaying = False
; - Update bonus positions:
bonus[1].move(bonus[2])
; - Display bonuses:
main_display.blit(bonus[0], bonus[1])
; - Score and remove bonus on collection:
if player_rect.colliderect(bonus[1])
, increase score and remove the bonus from bonuses.