A cup of coffee to get the brain ready for coding.

Enemy AI is an important aspect of any game. Graphics, story, and music can all be drowned out if your game contains some wonky or useless AI or gameplay. Sometimes graphics takes a backseat and the developer can create a memorable experience from gameplay and simple graphics. When you decide to make your first game, think about the users’ experience. Try and think about what adventure you want them to have and put yourself in their shoes.

The AI in games has gotten better over the years and will continue to improve. There have been some amazing games recently with fantastic and seemingly intelligent AI, or with experiences that adapt to the player based upon their decisions within the game. Even the enemy AI can be complemented by awesome gameplay techniques or incredible environments that add to the experience.

This week we’re back to continue developing our retro arcade style space shooter (that’s a mouth full). In Part 1, we talked about getting setup in Pygame and different types of movement styles, and in Part 2, we discussed how to import images. Please refer back to those tutorials if you have any questions while going through today’s code.

In part 3, we’re going to use Pygame to:

1. Get our spaceship to move,

2. Create enemies, load multiple enemy sprites, and detect to see if they go out of bounds. We will also begin to build simple AI for the enemies, 

3. Learn how to rotate sprites in our game. [UPDATE: Dec. 03, 2018]


Starting game window with spaceship and pretty planet.

Player Movement

Let’s first combine parts of the code and some of the ideas from Parts 1 and 2 and get our spaceship to move. Open the space_shooter.py file we created in the last tutorial.

In Part 2, we loaded and drew the image to the screen. This time we are going to create a Player class like we did back in Part 1. Using the arrow keys, the player will be able to move left, right, down and up.

Then, we will check boundary conditions to make sure the player doesn’t go outside of the window and update the player movement.

Create a Player object in main( ) and add it to the all_active_sprites list.

Here is what our ship looks like moving around in the window.

Go, Spaceship, Go!

Let’s Get to Know Our Enemies

What do we want our gameplay to be like? Space is a vast empty, uh, space with planets, stars and asteroids. We are going to create a game where our lonely ship is flying through an asteroid field, dodging or shooting down asteroids and other enemy ships. In this post we will discuss how to implement the asteroids and get the ship moving. In future posts, we will add more realistic movement to them.

Let’s take a look at our asteroid sprites. They are all of different sizes, shapes, colors, and textures to create a little more visual realism in our game.




In the main( ) function of our space_shooter.py file, we need to load the images for our asteroids.

In line 1, we create an empty list that will hold all of the asteroid images. In lines 2 – 8, we load all of the image names into a list that we use in lines 10 – 11 to append to the asteroid_images list. Note in line 11 how path.join is used to connect the image file location and each image name in asteroid_list. At the top of our file, add the os module:

Now, let’s create the Asteroid class.

Here, the Asteroid class is created and in line 5 we randomly select asteroids from the asteroid_img list to create a little more realism. Asteroids are spawned above the game window in line 10 and then move down the window towards our ship.

Above the while True main game loop, add code that will create asteroids and hurl them towards the ship.

Unfortunately, once the asteroids go outside of the window they don’t re-spawn. We need to create code in the Asteroid class that checks if the asteroids are outside of the game window, and if so, re-spawns new asteroids above the top of the game window.

That’s better.

But nothing is blowing up… yet.


[UPDATE: Dec. 03, 2018]

In the above gif, you can see that our asteroids are just hovering in space towards our ship. But it doesn’t look realistic. So let’s add rotation to our asteroid objects. (Which led me to search if they do really rotate in space. Fact: They actually kind of tumble.)

In our Asteroid class, let’s add some new attributes. self.angle is the number of degrees we want the asteroid image to rotate every time it updates, self.rotation_speed is fast we want them to rotate.

In line 3, we check the current_time, and if the last time that asteroid rotated is greater than 50, we update the current time in line 5. Then, we need to keep track of the angle of rotation on line 6. In line 7, we ensure that the angle does not increase beyond a 360 degree rotation. We then rotate the asteroid in line 8, and keep updating the image for the asteroid.

Finally, we add the rotate( ) function in update.


In this post, we looked at how to create a simple enemy AI using asteroids for our space shooter game. In the next post, we will create small enemy ships and add rotation to our asteroids.

If you have any questions, please leave a comment below.



Pygame documentation

kisspng.com: Tons of free, downloadable pngs.

Visit Us
Follow Me

Leave a Reply

Your email address will not be published. Required fields are marked *