Pygame: Defend The City!

November, 2016

The goal was to learn how to use the modules within Pygame and code a compelling game using Python that was actually fun to play. My biggest concern with this project was to make a game that had smooth gameplay and incorporated a number of visual effects.

The objective of the game it to stay alive as as long as possible while also shooting the alien invaders with lasers from your ship - relatively straightforward. Below are some of the game elements that I coded into the game that I was particularly proud of:

  1. Infinitely continuous scrolling background to simulate movement through space
  2. Explosion animation
  3. Health bar
  4. Shooting laser and explosion sound effect
  5. Increasing enemy count as score rise
  6. Gameplay music

Final Outcome

The final product of this project was exactly what I was hoping for. The scrolling background and explosions were the most difficult elements to incorporate. The background is an image of a cityscape that duplicates itself, puts a copy at far left end of the background image that is on the screen, deletes the background image that falls off screen left, and then repeats this process continuously.

The explosions happen by essentially running through a list of images and displaying them quickly on screen. The challenge here was to write that process into my code in a succinct and effective way.

The background and the explosions were the one thing that I definitely wanted to incorporate in order to make the game seem more legitimate and less of a homebrewed side project. Right now, the game only simulates gameplay. What I may try to add in the future is a game menu, and a “Game Over” screen with options (because right now the program just crashes when you die)

See a slightly altered version of my game below, where a chihuahua stops my friend Lloyd from getting into the city...