Ügyességi játékok programozása
Pythonnal és Pygame-mel

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. Many people get confused between the name of the library file, the name of the class, and the variable that points to the instance of the object. Library files should be all lower case. I'd recommend calling your new file that you put the Block class into block_library.py.
  4. Make sure your program runs like before. Adjust import statements as needed. Remember that you prepend the library name to the class, not the variable that points to the instance of the class. For example: my_dog = dog_library.Dog() and NOT dog_library.my_dog = Dog() because Dog is what is in the library, not my_dog.
  5. Define a GoodBlock class in a new file, and inherit from your Block class. I'd recommend using the file name goodblock_library.py to keep with the pattern we set up before. Remember, 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. (You probably don't need a new __init__ 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. Reset the block to the top when it falls off the screen. You may instead use a different type of movement. Take a look at the examples for some ideas.
  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.