Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating and Collecting Bonuses | Building Arcade Game
Building Arcade Game
course content

Course Content

Building Arcade Game

bookCreating and Collecting Bonuses

In our game, bonuses like crystals can add excitement and reward players. Let's explore how the create_bonus function brings these shiny rewards to life, step by step:

Load and Resize the Bonus Image

The line bonus = pygame.transform.scale(pygame.image.load('crystal.png'), (45, 75)) takes an image named 'crystal.png' and changes its size to 45 pixels wide and 75 pixels tall. This is our bonus item, ready to appear in the game.

Position the Bonus

Next, bonus_rect = pygame.Rect(random.randint(-bonus.get_height(), WIDTH), 0, *bonus.get_size()) places our bonus at a starting position. It can appear randomly along the top of the screen, from the very left (even slightly off-screen up to the bonus's height) to the right edge (WIDTH). This randomness adds an element of surprise.

Set the Bonus Movement

With bonus_move = [0, random.randint(1, 5)], we decide how the bonus will move. Here, it only moves vertically (downwards) at a random speed between 1 and 5 pixels each frame. The 0 means there's no horizontal movement, keeping it simple.

Send the Bonus into the Game

Finally, return [bonus, bonus_rect, bonus_move] sends back the bonus's image, its position, and how it moves. This trio lets the rest of the game know when and how to display and move the bonus on the screen.

This function effectively creates bonuses for players to collect, adding layers of fun and strategy to the game. It’s a simple way to enhance the gaming experience with rewards that players can look forward to grabbing.

Task

  1. Establish a function to generate bonus items within the game by using def keyword;
  2. Utilize pygame.transform.scale(pygame.image.load( 'crystal.png'), (45, 75)) to load the 'crystal.png' image and adjust its dimensions to 45x75 pixels, creating a visual for the bonus;
  3. Create a pygame.Rect with bonus_rect = pygame.Rect( random.randint(-bonus.get_height(), WIDTH), 0, *bonus.get_size()) to randomly place the bonus along the top edge of the screen, allowing it to start anywhere from slightly off-screen to the maximum WIDTH;
  4. Assign bonus_move = [0, random.randint(1, 5)] to create a downward movement at a random speed between 1 and 5 pixels per frame, with no horizontal movement;
  5. Conclude with return [bonus, bonus_rect, bonus_move], providing the necessary elements for the game to display and manage the bonus's movement.

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!

In our game, bonuses like crystals can add excitement and reward players. Let's explore how the create_bonus function brings these shiny rewards to life, step by step:

Load and Resize the Bonus Image

The line bonus = pygame.transform.scale(pygame.image.load('crystal.png'), (45, 75)) takes an image named 'crystal.png' and changes its size to 45 pixels wide and 75 pixels tall. This is our bonus item, ready to appear in the game.

Position the Bonus

Next, bonus_rect = pygame.Rect(random.randint(-bonus.get_height(), WIDTH), 0, *bonus.get_size()) places our bonus at a starting position. It can appear randomly along the top of the screen, from the very left (even slightly off-screen up to the bonus's height) to the right edge (WIDTH). This randomness adds an element of surprise.

Set the Bonus Movement

With bonus_move = [0, random.randint(1, 5)], we decide how the bonus will move. Here, it only moves vertically (downwards) at a random speed between 1 and 5 pixels each frame. The 0 means there's no horizontal movement, keeping it simple.

Send the Bonus into the Game

Finally, return [bonus, bonus_rect, bonus_move] sends back the bonus's image, its position, and how it moves. This trio lets the rest of the game know when and how to display and move the bonus on the screen.

This function effectively creates bonuses for players to collect, adding layers of fun and strategy to the game. It’s a simple way to enhance the gaming experience with rewards that players can look forward to grabbing.

Task

  1. Establish a function to generate bonus items within the game by using def keyword;
  2. Utilize pygame.transform.scale(pygame.image.load( 'crystal.png'), (45, 75)) to load the 'crystal.png' image and adjust its dimensions to 45x75 pixels, creating a visual for the bonus;
  3. Create a pygame.Rect with bonus_rect = pygame.Rect( random.randint(-bonus.get_height(), WIDTH), 0, *bonus.get_size()) to randomly place the bonus along the top edge of the screen, allowing it to start anywhere from slightly off-screen to the maximum WIDTH;
  4. Assign bonus_move = [0, random.randint(1, 5)] to create a downward movement at a random speed between 1 and 5 pixels per frame, with no horizontal movement;
  5. Conclude with return [bonus, bonus_rect, bonus_move], providing the necessary elements for the game to display and manage the bonus's movement.

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