Space Invaders Game In Python With Source Code

What is Space Invaders Game In Python?

The Space Invaders Game In Python is a fully functional desktop application project that covers all of the elements that IT students and computer-related courses will require for their college projects or for leisure time purposes. This Space Invaders In Python is a space war game between spacecraft in orbit. The player can use the left and right keys of their PC or laptop. The spacecraft automatically fires at the other spacecraft. The only thing you have to do is aim and destroy them in order to get lots of score. This Space Invaders Python Pygame is useful for learning new skills and practicing Python game development. This project is quite useful, and the concept and logic of the project are simple to grasp. The source code is open source and free to use. Simply scroll down and click the download option.

Watch the whole video to see how to run the Space Invaders Game, from start to finish!

Space Invaders Game In Python : Project Output

Space Invaders Game
Space Invaders Game

How do you play Space Invaders Game In Python?

The player controls the spacecraft at the bottom of the screen, which can move only horizontally. The other spacecraft is approaching from both the horizontal and vertical axes. The spaceship can be programmed to fire a laser at the other spacecraft, destroying it.

This is a simple spacecraft shooting game. The player must move his or her shooting gun with the arrow keys and shoot it with the spacebar in this game. For easier gameplay, there is a basic and clear GUI. The player must maneuver the gun and aim the spaceship using the arrow keys. The game has a lot of potential.

About the Project : Space Invaders Game In Python With Source Code

The project file (main.py) contains python scripts. In terms of gameplay, the player is tasked with shooting the spaceship from a moving gun. To kill the spaceship, the player must press the spacebar. To win the game, the player must shoot and destroy all of the zigzagging spaceships. If the user is unable to destroy all of the spaceships before they collide with the ground, he or she will lose the game. This game’s PC controls are also straightforward. You can maneuver your gun with the left and right arrow keys, and shoot with the spacebar. The gameplay is designed in such a way that the user will have no trouble using and understanding it.

The Space Invaders Using Python is a simple desktop application made using the Python programming language. We may also create highly fascinating games with the Python programming language. The Space invaders game is one of them. The project system file comprises resource files as well as a Python script. The graphics of the game are smooth, and the controls are simple.

This Space Invaders Game Python Code includes a tutorial and a code development guide. This is a simple and basic-level little project for learning purposes. Space Invaders game is open source, so you can download the zip file and change it to fit your needs. You can also customize this project to meet your needs and create a fantastic advanced-level project.

Project Details and Technology : Space Invaders Game In Python

Project Name:Space Invaders Game In Python
Abstract:This Space Invaders Game In Python is a simple space game for users that want this kind of gaming platform for their leisure time, as well as for students that need this kind of project.
Language/s Used:Python (GUI Based)
Python version (Recommended):3.8 or 3.9
Type:Desktop Application
Developer:Source Code Hero
Updates:0
Space Invaders Game In Python With Source Code – Project Information

The code given below is the full source code on How To Make Space Invaders In Python

The given code below is a python file for main.py

from sys import exit
from pygame.locals import *
from StrategyGame import *
import random

# Initialize the game
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Aircraft War Game')

# Loading game music
BULLETSHOT_SOUNDTEST = pygame.mixer.Sound('image/sound/bullet.wav')
OPPONENT1_DOWN_SOUNDTEST = pygame.mixer.Sound('image/sound/opponent1_down.wav')
GAMEOVER_SOUNDTEST = pygame.mixer.Sound('image/sound/game_over.wav')
BULLETSHOT_SOUNDTEST.set_volume(0.3)
OPPONENT1_DOWN_SOUNDTEST.set_volume(0.3)
GAMEOVER_SOUNDTEST.set_volume(0.3)
pygame.mixer.music.load('image/sound/game_music.wav')
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.set_volume(0.25)

# loading background image
GAME_BACKGROUND = pygame.image.load('image/image/background.png').convert()
GAME_OVER = pygame.image.load('image/image/gameover.png')
filename = 'image/image/aircraft_shooter.png'
AIRCRAFT_IMAGES = pygame.image.load(filename)

# Set player related parameters
AIRCRAFT_PLAYER = []
AIRCRAFT_PLAYER.append(pygame.Rect(0, 99, 102, 126))  # Player sprite image area
AIRCRAFT_PLAYER.append(pygame.Rect(165, 360, 102, 126))
AIRCRAFT_PLAYER.append(pygame.Rect(165, 234, 102, 126))  # Player explosion sprite image area
AIRCRAFT_PLAYER.append(pygame.Rect(330, 624, 102, 126))
AIRCRAFT_PLAYER.append(pygame.Rect(330, 498, 102, 126))
AIRCRAFT_PLAYER.append(pygame.Rect(432, 624, 102, 126))
AIRCRAFT_PLAYER_POSITION = [200, 600]
OPPONENT = Opponent(AIRCRAFT_IMAGES, AIRCRAFT_PLAYER, AIRCRAFT_PLAYER_POSITION)

# Define the surface related parameters used by the bullet object
AIRCRAFT_BULLET = pygame.Rect(1004, 987, 9, 21)
BULLET_IMAGES = AIRCRAFT_IMAGES.subsurface(AIRCRAFT_BULLET)

# Define the surface related parameters used by the opponent object
OPPONENT1 = pygame.Rect(534, 612, 57, 43)
OPPONENT1_IMAGES = AIRCRAFT_IMAGES.subsurface(OPPONENT1)
OPPONENT1_DOWN_IMAGES = []
OPPONENT1_DOWN_IMAGES.append(AIRCRAFT_IMAGES.subsurface(pygame.Rect(267, 347, 57, 43)))
OPPONENT1_DOWN_IMAGES.append(AIRCRAFT_IMAGES.subsurface(pygame.Rect(873, 697, 57, 43)))
OPPONENT1_DOWN_IMAGES.append(AIRCRAFT_IMAGES.subsurface(pygame.Rect(267, 296, 57, 43)))
OPPONENT1_DOWN_IMAGES.append(AIRCRAFT_IMAGES.subsurface(pygame.Rect(930, 697, 57, 43)))

CHALLENGER1 = pygame.sprite.Group()

# Store the destroyed aircraft for rendering the wrecking sprite animation
CHALLENGER_DOWN = pygame.sprite.Group()

SHOOT_DISTANCE = 0
CHALLENGER_DISTANCE = 0

OPPONENT_DOWN_INDEX = 16

SCORE = 0

CLOCK = pygame.time.Clock()

RUNNING = True

while RUNNING:
    # Control the maximum frame rate of the game is 60
    CLOCK.tick(60)

    # Control the firing of the bullet frequency and fire the bullet
    if not OPPONENT.is_hit:
        if SHOOT_DISTANCE % 15 == 0:
            BULLETSHOT_SOUNDTEST.play()
            OPPONENT.shoot(BULLET_IMAGES)
        SHOOT_DISTANCE += 1
        if SHOOT_DISTANCE >= 15:
            SHOOT_DISTANCE = 0

    # Generating opponent aircraft
    if SHOOT_DISTANCE % 50 == 0:
        CHALLENGER1_POSITION = [random.randint(0, SCREEN_WIDTH - OPPONENT1.width), 0]
        CHALLENGERS1 = Challenger(OPPONENT1_IMAGES, OPPONENT1_DOWN_IMAGES, CHALLENGER1_POSITION)
        CHALLENGER1.add(CHALLENGERS1)
    CHALLENGER_DISTANCE += 1
    if CHALLENGER_DISTANCE >= 100:
        CHALLENGER_DISTANCE = 0

    # Move the bullet and delete if it is exceeds the window
    for bullet in OPPONENT.bullets:
        bullet.move()
        if bullet.rect.bottom < 0:
            OPPONENT.bullets.remove(bullet)

    # Move opponent aircraft, delete if it is exceeds the window range
    for CHALLENGER in CHALLENGER1:
        CHALLENGER.move()
        # Determine if the player is hit
        if pygame.sprite.collide_circle(CHALLENGER, OPPONENT):
            CHALLENGER_DOWN.add(CHALLENGER)
            CHALLENGER1.remove(CHALLENGER)
            OPPONENT.is_hit = True
            GAMEOVER_SOUNDTEST.play()
            break
        if CHALLENGER.rect.top > SCREEN_HEIGHT:
            CHALLENGER1.remove(CHALLENGER)

    # Add the opponent object that was hit to the destroyed opponent group to render the destroy animation
    CHALLENGER1_DOWN = pygame.sprite.groupcollide(CHALLENGER1, OPPONENT.bullets, 1, 1)
    for CHALLENGERS_DOWN in CHALLENGER1_DOWN:
        CHALLENGER_DOWN.add(CHALLENGERS_DOWN)

    # Drawing background
    screen.fill(0)
    screen.blit(GAME_BACKGROUND, (0, 0))

    # Drawing player plane
    if not OPPONENT.is_hit:
        screen.blit(OPPONENT.image[OPPONENT.img_index], OPPONENT.rect)
        # Change the image index to make the aircraft animated
        OPPONENT.img_index = SHOOT_DISTANCE // 8
    else:
        OPPONENT.img_index = OPPONENT_DOWN_INDEX // 8
        screen.blit(OPPONENT.image[OPPONENT.img_index], OPPONENT.rect)
        OPPONENT_DOWN_INDEX += 1
        if OPPONENT_DOWN_INDEX > 47:
            RUNNING = False

    # Draw an wreck animation
    for CHALLENGERS_DOWN in CHALLENGER_DOWN:
        if CHALLENGERS_DOWN.down_index == 0:
            OPPONENT1_DOWN_SOUNDTEST.play()
        if CHALLENGERS_DOWN.down_index > 7:
            CHALLENGER_DOWN.remove(CHALLENGERS_DOWN)
            SCORE += 1000
            continue
        screen.blit(CHALLENGERS_DOWN.down_imgs[CHALLENGERS_DOWN.down_index // 2], CHALLENGERS_DOWN.rect)
        CHALLENGERS_DOWN.down_index += 1

    # Drawing bullets and opponents planes
    OPPONENT.bullets.draw(screen)
    CHALLENGER1.draw(screen)

    # Draw a score
    SCORE_FONT = pygame.font.Font(None, 36)
    SCORE_TXT = SCORE_FONT.render(str(SCORE), True, (255, 255, 0))
    TXT_AIRCRAFT = SCORE_TXT.get_rect()
    TXT_AIRCRAFT.topleft = [10, 10]
    screen.blit(SCORE_TXT, TXT_AIRCRAFT)

    # Update screen
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    # Listening for keyboard events
    KEY_PRESSED_ENTER = pygame.key.get_pressed()
    # Invalid if the player is hit
    if not OPPONENT.is_hit:
        if KEY_PRESSED_ENTER[K_r] or KEY_PRESSED_ENTER[K_UP]:
            OPPONENT.moveUp()
        if KEY_PRESSED_ENTER[K_f] or KEY_PRESSED_ENTER[K_DOWN]:
            OPPONENT.moveDown()
        if KEY_PRESSED_ENTER[K_d] or KEY_PRESSED_ENTER[K_LEFT]:
            OPPONENT.moveLeft()
        if KEY_PRESSED_ENTER[K_g] or KEY_PRESSED_ENTER[K_RIGHT]:
            OPPONENT.moveRight()

# Brought To You By Itsourcecode.com

FONT = pygame.font.Font(None, 60)
TXT = FONT.render('Score: ' + str(SCORE), True, (255, 255, 0))
TXT_AIRCRAFT = TXT.get_rect()
TXT_AIRCRAFT.centerx = screen.get_rect().centerx
TXT_AIRCRAFT.centery = screen.get_rect().centery + 24
screen.blit(GAME_OVER, (0, 0))
screen.blit(TXT, TXT_AIRCRAFT)

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    pygame.display.update()

The given code below is a python file for StrategyGame.py

import pygame

SCREEN_WIDTH = 480
SCREEN_HEIGHT = 800

TYPE_SMALL = 1
TYPE_MIDDLE = 2
TYPE_BIG = 3

class Bullet(pygame.sprite.Sprite):
    def __init__(self, BULLET_IMAGES, initial_position):
        pygame.sprite.Sprite.__init__(self)
        self.image = BULLET_IMAGES
        self.rect = self.image.get_rect()
        self.rect.midbottom = initial_position
        self.speed = 10

    def move(self):
        self.rect.top -= self.speed

class Opponent(pygame.sprite.Sprite):
    def __init__(self, AIRCRAFT_IMAGES, AIRCRAFT_PLAYER, initial_position):
        pygame.sprite.Sprite.__init__(self)
        self.image = []
        for i in range(len(AIRCRAFT_PLAYER)):
            self.image.append(AIRCRAFT_IMAGES.subsurface(AIRCRAFT_PLAYER[i]).convert_alpha())
        self.rect = AIRCRAFT_PLAYER[0]
        self.rect.topleft = initial_position
        self.speed = 8
        self.bullets = pygame.sprite.Group()
        self.img_index = 0
        self.is_hit = False

    def shoot(self, BULLET_IMAGES):
        bullet = Bullet(BULLET_IMAGES, self.rect.midtop)
        self.bullets.add(bullet)

    def moveUp(self):
        if self.rect.top <= 0:
            self.rect.top = 0
        else:
            self.rect.top -= self.speed

    def moveDown(self):
        if self.rect.top >= SCREEN_HEIGHT - self.rect.height:
            self.rect.top = SCREEN_HEIGHT - self.rect.height
        else:
            self.rect.top += self.speed

    def moveLeft(self):
        if self.rect.left <= 0:
            self.rect.left = 0
        else:
            self.rect.left -= self.speed

    def moveRight(self):
        if self.rect.left >= SCREEN_WIDTH - self.rect.width:
            self.rect.left = SCREEN_WIDTH - self.rect.width
        else:
            self.rect.left += self.speed

class Challenger(pygame.sprite.Sprite):
    def __init__(self, opponent_resource, opponent_down_resources, init_pos):
       pygame.sprite.Sprite.__init__(self)
       self.image = opponent_resource
       self.rect = self.image.get_rect()
       self.rect.topleft = init_pos
       self.down_imgs = opponent_down_resources
       self.speed = 2
       self.down_index = 0

    def move(self):
        self.rect.top += self.speed

This Space Invaders Game In Python With Source Code also includes a downloadable Project With Source Code for free, just find the downloadable source code below and click to start downloading.

By the way, if you are new to python programming and you don’t have any idea what Python IDE to use, I have here a list of Best Python IDE for Windows, Linux, Mac OS for you. Additionally, I also have here How to Download and Install Latest Version of Python on Windows.

To start executing a Space Invaders Game In Python, make sure that you have installed Python in your computer.

Space Invaders Game In Python : Steps on how to run the project

Time needed: 5 minutes.

These are the steps on how to run Space Invaders Game In Python

  • Step 1 : Download Source Code

    First, find the downloadable source code below and click to start downloading the source code file.
    Currency Converter Project In Java

  • Step 2 : Extract File

    Next, after finished to download the file, go to file location and right click the file and click extract.
    customer management system in php

  • Step 3 : Open PyCharm

    Next, open pycharm IDE and open the project you’ve download.
    Space Invaders Game In Python

  • Step 4 : Run Project

    Next, go to the Pycharm and click the run button to start executing the project.
    Space Invaders Game In Python

Download Source Code below!

Summary

This Article is the way to enhance and develop our skills and logic ideas which is important in practicing the python programming language which is most well known and most usable programming language in many company.

Inquiries

If you have any questions or suggestions about Space Invaders Game In Python With Source Code, please feel free to leave a comment below.

Leave a Comment

Your email address will not be published.

%d bloggers like this: