Code a Space Invaders Game With Pygame!

by xX_christopher_Xx in Living > Toys & Games

5412 Views, 12 Favorites, 0 Comments

Code a Space Invaders Game With Pygame!

Space Invaders Game

In this Instructables, I will take you through a coding course to code a Space Invaders type game with Pygame!

To play, run the program. Enter your username in the terminal (not the game screen).

The terminal displays the instructions:

Arrow keys to move.
Space to shoot.

Powerup guide:

  • Orange powerups make your next bullet pierce.
  • Yellow powerups make your next bullet big.
  • Black powerups make your next bullet explode.
  • Grey powerups give you a shield.
  • Cyan (light blue-green) powerups give you 5 points.

Mob guide:

  • Green mobs (1 pt) are normal.
  • Purple mobs (2 pts) have more health.
  • Blue mobs (2 pts) are faster.

Downloads

Supplies

  • Software that can run Python, such as VSCode or IDLE.

Files

space.png

Make a folder called "images" and place all of the images in there. Do the same for the sounds. Make a txt file called high_scores.

The images and sounds are stored here: https://drive.google.com/file/d/1IXvOS7lkpwDFJcBNz...

The "space.png" file is attached above. Thanks to healthcheck82 and jason.sowerby2012 for noticing that it was missing!

Constants & Imports

space_1.JPG

We need to import:

  • os.stat_result and os.path to access files
  • pygame to run the game
  • random to randomize powerups

time is not needed, but I was using it to experiment with a timer before the start of the game.

We need constants for the screen width, height, and FPS. We also need constants for the colors (We don't need all of them, but if you want to change something's color, it'll be easier this way.)

Player Class

space_2a.JPG
space_2b.JPG

The __init__ function sets up the image and collision detection.

The update function handles movement.

Shield and unshield change variables and the image when the player hits a shield powerup.

Mob Class

space_3.JPG

The __init__ function sets up the mob image, health, speed, and worth (points gained from killing it).

The update function handles movement forward and to the side. The 1000 in the section "If self.counter = 1000" can be set to anything between 0 and 1999.

Bullet Class

space_4.JPG

The __init__ section handles different images and stats for powerups, as well as the speed.

The update section handles the movement and destroys the bullet if it goes off screen.

Powerup Class

space_5.JPG
space_5b.JPG

The __init__ function sets up the image.

The return_power function returns the powerup type.

The randomize function randomizes the powerup, and makes sure it won't become a shield powerup if the player is already shielded.

The update function randomizes the powerup if it is not hit after a while.

Button Class

space_6.JPG

The __init__ function creates the button shape and text.

The prep_msg function renders the text.

The draw_button function draws the button.

Game Class, Pt. 1

space_7.JPG
space_7b.JPG
space_7c.JPG
space_7d.JPG
space_7e.JPG
space_7f.JPG

The __init__ function gets the username from the player (from the terminal, not the game screen) and loads all the files.

The reset function assigns values to the mob stats and others, creates groups of sprites, and creates the player and powerups.

The image and sound functions retrieve and handle files.

The create_mobs function creates a rectangular block of mobs.

The display_text function displays a string of text.

The print_instructions function prints the instructions in the terminal.

The get_events and check_play_button functions handle the events.

Game Class, Pt. 2

space_7g_1.JPG
space_7g_2.JPG
space_7g_3.JPG
space_7g_4.JPG

The run function runs the program, completing tasks such as:

  • Updating sprites
  • Building levels
  • Calculating collisions
  • Applying powerups
  • Reloading ammo

Game Class, Pt. 3

space_7h.JPG
space_7i.JPG

The display function displays the button, sprites, and briefly displays level text when entering a new level.

The end_game function ends the game and displays score, high score, and maximum level reached.

Main Function

space_8.JPG

The main function initiates pygame, creates the clock, and runs the game.