Pygame: Bullets

Space Game

This second tutorial is designed to show some more advanced features of pygame.  If you follow it to the end you’ll also be introduced to Object Oriented Programming in Python.  If you’re in my Intro to Programming class, this is more advanced than our course will really get – you’ll be on your own learning some of this stuff, but you might find it worth it!  Games are a great way to get started with Object Oriented Programming (sometimes shortened to OOP), since game objects are very concrete.

To get started with the space game tutorial, download the started project here.

Give yourself a tour of the started project – don’t skip this step!  Run the project – the ship moves left and right with the arrow keys.

Enemies

The starter project includes an enemy ship image.  Note that the image size is 40 x 33 pixels.  We’ll use that size to make rectangles for the enemies.

The game starts with no enemies, but we want an endless rain of enemies to fall from above the top of the screen.  The only thing we need to put in the setup section is an empty list to hold future enemies.

A list for our enemy ships.

In the actions that happen automatically section, we want to add ships randomly.   Import the randint function at the top of the page.

Get the randint function

In the actions that happen automatically section, we’ll add an enemy in about one in ten frames.  Our enemies will appear above the top of the screen (y is negative), and at a random x between 1 and 760.

Add an enemy about one frame in ten.

Then we need to move all the enemies down.  Down means add to the y value.  This code goes in the move stuff section.

Moving all the enemies

Then we need to draw the enemies.  Import the enemy image in the setup section, add just the one line that starts with enemy_image.

Import the enemy ship image.

Finally we’ll use the blit function to draw the enemies on the screen.  Put this in the draw section.

Drawing the enemy ships.

Now run the project.  Enemies should be falling!  If they’re not, look for where you made a mistake.

Bullets

To shoot the enemies, we’ll make bullets.  Bullets will appear when we press the spacebar, and will appear at the location of the ship’s nose.  They’ll move up the screen, and collide with enemies.  To get started add an empty list in the setup section.

An empty list of bullets.

Then add the bullets on the spacebar press.  Their initial location depends on the current location of the ship.  Be careful where you put the code – the if’s for different keys should line up.

Bullets appear when you press the spacebar.

Then move and draw the bullets – just like we did the enemies.  The bullets are small yellow circles.

Moving bullets
Drawing bullets

Run the program – you should be able to shoot bullets!

Collisions

In order to test if bullets hit enemies, you need to check every bullet against every enemy!  This is a lot of checks – but for us it is a nested loop.

Checking for collisions between every bullet and every enemy.

When you run it now, bullets hit enemies and both are destroyed.

And more…

This isn’t really a game yet.  You could get points for destroying enemies, and lose points when enemies make it off the bottom.  Maybe after 100 fall off the bottom the game is over.  You decide.

If you play this game long enough, it will start to slow down.  computers are fast, but if you have two lists, one of one thousand enemies and one of one thousand bullets, the computer needs to check one million collisions each 1/60 second.  The best thing to do is to remove bullets and enemies you can no longer see on the screen.  If you don’t they are still in the lists.  Try to do this.