Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Handling Enemy and Bonus Interactions | PyGame Project: Arcade Game
PyGame Project: Arcade Game
course content

Course Content

PyGame Project: Arcade Game

bookHandling 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, so playing = 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 with bonuses.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.

Task
test

Swipe to show code editor

  1. Update enemy positions: enemy[1].move(enemy[2]);
  2. Display enemies: main_display.blit(enemy[0], enemy[1]);
  3. Check player-enemy collision: if player_rect.colliderect(enemy[1]), set playing = False;
  4. Update bonus positions: bonus[1].move(bonus[2]);
  5. Display bonuses: main_display.blit(bonus[0], bonus[1]);
  6. Score and remove bonus on collection: if player_rect.colliderect(bonus[1]), increase score and remove the bonus from bonuses.

Mark tasks as Completed
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

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, so playing = 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 with bonuses.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.

Task
test

Swipe to show code editor

  1. Update enemy positions: enemy[1].move(enemy[2]);
  2. Display enemies: main_display.blit(enemy[0], enemy[1]);
  3. Check player-enemy collision: if player_rect.colliderect(enemy[1]), set playing = False;
  4. Update bonus positions: bonus[1].move(bonus[2]);
  5. Display bonuses: main_display.blit(bonus[0], bonus[1]);
  6. Score and remove bonus on collection: if player_rect.colliderect(bonus[1]), increase score and remove the bonus from bonuses.

Mark tasks as Completed
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 11
AVAILABLE TO ULTIMATE ONLY
We're sorry to hear that something went wrong. What happened?
some-alt