Program Arcade Games
With Python And Pygame

Language
English
Dutch - Nederlands
Finnish - Suomi
French - Français
Hungarian - Magyar
Portuguese - Português
Russian - Русский
Spanish - Español
Turkish - Türkçe

Lab 14: Sprite Moving

fig.who_lab15

This lab practices uses Pygame sprites as described in Chapter 13, and separates the classes into different files as described in Chapter 14.

  1. Make sure this program is created in its own directory. If you are starting from the template I have at BitBucket, then make sure you start in the “Lab 14 - Sprite Moving” folder and put everything in there.
  2. Start with a copy of the program you wrote for Lab 13: Sprite Collecting. Copy those files into the directory for this lab.
  3. Move the Block class into a new file.
  4. Make sure your program runs like before. Adjust import statements as needed.
  5. Define a GoodBlock class in a new file, and inherit from your Block class. (Define the class, don't create an instance of it. Your for loop that creates the instances does not move.)
  6. Add a new update method. Make the good block randomly move up, down, left or right each update. (Change self.rect.x and self.rect.y randomly each time the update function is called. Not to a completely new number, but add a random number from -3 to 3 or so. Remember that random.randrange(-3,3) does not generate a random number from -3 to 3.)
  7. Change your for loop so that it creates instances of the GoodBlock class and not your old regular Block class.
  8. Call update on the list of all the sprites you have. Do this in the main program loop so the blocks keep moving, not just once at the start of the program.
  9. Test and make sure it works.
  10. It is ok if the sprites move off the screen, but a common mistake results in the sprites all moving up and to the left off the screen. If that happens go back four steps and check your random range.
  11. Double check, make sure GoodBlock inherits from the Block class. If done correctly, GoodBlock won't need an __init__ method because it will get this method from Block.
  12. Create a BadBlock class in a new file and inherit from the Block class.
  13. Make an update function and have the bad block sprites move down the screen, similar to what was done in Chapter 13, section 2. Extra kudos if you make a bouncing rectangle.
  14. Test, make sure it works.
  15. Double check to make sure each class is in its own file.
  16. If you have extra time, you can look at the sprite examples section on the website and see how to get sprites to bounce or move in circles.
Video: Lab 14 Sample

You are not logged in. Log in here and track your progress.

About Buy the Book Help Translate My College My Twitter Updates