4-14 pyPong v2.0

One of the things I love about developing games is that once you have a basic version of the game working, you have full control over adding any enhancements that you can imagine!  The exercise from the last day was to enhance our pyPong v1.0 game by adding additional features.  Today I’m going to walk you through the code to make these enhancements:

    1. Change the Ball sprite from a red circle to an image file of a ball.
    2. Change the Player sprite from a black rectangle to an appropriate image.
    3. Use a custom font for the ScoreKeeper sprite.
    4. Add a background image.
    5. Every time a Ball bounces off a Player, increase the Ball speed by 1.
    6. Play background music while the game is running.
    7. Play a sound effect when the Ball bounces off a Player.
    8. Play a sound effect when a Player scores a point.
    9. Change control of Player 2 to a second joystick or the mouse.
    10. When the game ends, display a “Game Over” message in the window.

There are many ways to accomplish the same goals, so I’m not saying that the way I have implemented the enhancements is the only way to do it.  You may have done it differently.  If so, compare our approaches to understand the differences.

Pulling Together Resources

We’ve learned how to incorporate sound and images already, but you’ll note that I avoided doing this in the first version of the game.  The reason for this is so that I could focus on the basic game mechanics rather than aesthetics for my first version.  There is no point wasting time creating pretty graphics if you end up unable to get the basic game code working!

Now it’s time to jazz up the game, so as a first step I pulled together the images (ball.gif, background.gif, player1.gif, player2.gif, and gameover.gif), font (spaceage.ttf), background music (music.ogg), and sound effects (bounce.wav and score.wav).  Pulling all of this together took a considerable amount of time.  If you plan to distribute your game, be sure that any resources you use are not copyrighted.

Let’s look at the Sprite class revisions first, then the main game loop.

Ball Sprite Revisions

In the __init__() method of the original code, I had created a very plain circle to represent the Ball sprite.

To replace this with an actual image of a ball actually takes less code:

One other change I made was to add a new method called speedUp().  The purpose of this method is to increase the speed of the Ball sprite by 1 pixel.  I’m going to call this method in my main game loop code every time the Ball collides with one of the Player sprites. This will increase the speed/difficulty of the game.

Player Sprite Revisions

In the original __init__() method, I used a simple black rectangle to represent each player:

The code below loads a different image to represent the Player sprite (depending on whether it is player 1 or 2).  The rest of the code in the Player class remains the same.

Again, the code for this enhancement is not much longer than the original, but will really improve the attractiveness of the game.

ScoreKeeper Sprite Revisions

To use a custom font,  I simply changed the following line in the original __init__() method:

to:

Main Game Loop Revisions

We’ve now taken care of enhancements 1, 2, and 3 on our list and implemented a method to help us with enhancement 5.  The rest of the enhancements will happen in the main game loop code.

Let’s start with revising the IDEA sections:

Since we’re going to add sound to the game, on line 4 we add code to initialize the pygame sound mixer module.

In the Entities (IDEA) section I am loading a background image instead of having a plain white background.  I did not need to change any of the Sprite instantiation code.  After this, I’ve created a gameover surface from an image containing the words “Game Over”.  I’ll show this surface when the game loop terminates.

Finally, I’m loading background music with a volume level of 30% and having it play in a continuous loop.  I’m also loading two sound effects so that I can play them as needed during the game action.

Ok, let’s look at the ALTER code revisions:

The main changes here occur in the collision detection if statements.  I’ve added a line of code to each to play an appropriate sound effect if the ball is bounced off a player or if a player scores a point. Also, in the case where the Ball sprite collides with one of the Player sprites, I call my new speedUp() method in the Ball class to increase the speed of action. If the game has been won I fade out the background music before the game loop terminates.

Once the game loop terminates, I blit the “Game Over” graphic onto the screen.

Believe it or not, those simple changes cover enhancements 4, 5, 6, 7, 8, and 10!  I’ll leave enhancement 9 as an exercise for you and a partner.  Our pyPong v2.0 game looks (and sounds) much better.  Yeah, baby!

Congratulations!  At this point, you’ve learned all of the fundamental skills needed to develop your own games.  You are now in an excellent position to continue learning additional features of Pygame on your own.  The best way to improve your skills is to practice by building progressively more challenging games.  What you create is limited only by your imagination, good luck!