Arcade games
adapted from Coding Games with Pygame Zero & Python, by Richard Smith
The alien moves when you press the cursor keys.
Program 4.5 Keyboard input
|
|
Make the alien move up and down as well as left and right.
Use the more concise += operator to change the alien.x value.
Use the or operator to allow WASD keys to move the alien in addition to the cursor keys.
Arcade games need to know when one Actor sprite has hit another Actor sprite. Most of this code is copied from Program 4.2 and Program 4.5.
Program 5.1 Collisions
|
|
Add vertical movement (as you did in Exercise Program 4.5).
Make the box chase the alien.
Print number of times the box hits the alien (i.e. the score).
5.2. Chase Instead of moving constantly to the right we can make the movement conditional with an if statement so the box chases the alien. Most of this code is copied from Program 5.1. New lines are highlighted. We have also changed what happens when the box catches the alien: the program now exits and you must run it again to play again. This may not be what you want in your game!
Program 5.2 Alien chase
|
|
Add vertical movement (as you did in previous exercise).
Draw a new enemy image. Save it as enemy.png in your mu_code/images folder. Load it as an Actor(‘enemy’) instead of the Rect().
Instead of an enemy the box here is a powerup that the player must collect. When he does it disappears and moves to a new location.
Program 5.3 Collect the powerups
|
|
Add vertical movement (as you did in Exercise ref{exercise:updown}).
Draw a new powerup image. Save it as powerup.png in your mu_code/images folder. Load it as an Actor(‘powerup’) instead of the Rect().
Combine this program with the enemy from Program Program 5.2 and the background from Program 4.4 and whatever else you want to make your own game.
Pygame Zero comes with one other image alien_hurt.png and one sound eep.wav. If you want more you will have to add them to the sounds and images folders.
Most of this code is copied from Program 5.1
Program 5.4 Sound and animation upon collision
|
|
Record your own sound effect and add it to the game.
Add more boxes or sprites that move in different ways for the player to avoid.
Add a second alien controlled by different keys or gamepad for player 2.
This uses a function call-back for event-based input. It is similar to Program 5.4 but:
The box has been removed.
There is an on_mouse_down() special function that is called automatically when the player click the mouse.
The score is displayed.
See Program 2.17 for more about functions.
Program 5.5 Getting input from mouse clicks
|
|
Program 5.6 Getting input from mouse movement
|
|
What happens if you delete line 8 and replace it with this:
animate(alien, pos=pos, duration=1, tween=‘bounce_end’)
What happens if you change on_mouse_move to on_mouse_down?
Make a game with one alien controlled by mouse and another controlled by keyboard