Space Invaders Game

A simple space invaders game made by me for my school project
327 Downloads
Updated 5 Jul 2019

View License

Space Invaders is a two-dimensional shooter game in which the player controls a laser cannon by moving it horizontally across the bottom of the screen and firing at descending aliens. The aim is to defeat four rows of seven aliens that move horizontally back and forth across the screen. The player defeats an alien, and earns points, by shooting it with the laser cannon. A mysterious spaceship attempt to destroy the cannon by firing at it randomly. If the player loses all lives, the alien invasion is successful and the game ends.
GUI-Figure
The GUI is traditionally and user-friendly. It consists of start part and game part. At the start part, there is a button to start the game. At the game part, before beginning, user can find instructions to play the game. Two textboxes show and update the score and life count of the player.

Graphics/ Results
This part refers to what the player sees, which mainly consists of the game characters and background. While the game runs in the background, characters’ position, life count, score and laser positions are updated.
Code
Parallel to the graphics, there is a matrix version of the game running in the background. I tried to design as efficient as possible, so it does not add computational costs to the whole program.
Initialization
This part creates figures in structs, by removing unnecessary backgrounds from images with for loop and save coordinates to move them.
Start
This part creates the player and alien object using alien and player classes. Also gives to the player game info, draw background city image and the other figures for the beginning of the game.
After starting, with the help of other functions, it make aliens move horizontally and checks if the game has won or lost.
Keyboard Commands
This part check the pressed button. Right arrow to make player move right, left arrow make player move left and space to fire aliens.
It also creates random shoots from a mysterious spaceship and destroys the succesfully hitted aliens, while doing those, it updates score and life count.
In game there is a bonus alien, if it is hit, player gets an extra life. It appears in the beginning of the game and moves out of the screen in a couple seconds.
Updating game
To update game there is a function called drawagain. It draws background image, aliens, player and game informations again.
Game Classes
There are classes called player, laserthrow, blank, alienship, alien and musicgenerator. Player, laserthrow, blank, alienship and alien classes create the corresponding figures and draw them. Musicgenerator is used for producing game sounds before runtime.
The main purpose to create a blank figure is for replacing it aliens when it is hit. Assigning aliens 0 or Nan values makes error, because of size mismatching problems.
Challenges and Conclusion
The most challenging part of this game was updating every move as effeciently as possible. I had to use global variables in some parts to update score, life count etc. I created class objects and updated them to make it more efficient. Using objects and properties was complicating at first. After working in this project, I have become familiar with working with GUIs and how to share the variables created between the multiple functions that are used in the GUI. It helped me to understand how much the code efficiency is important.
Next steps to extend this project can be adding levels: changing the alien movement velocity, adding multiple mysterious spaceship etc. It would also be interesting to add different characters related to space.

Cite As

IPEK ANIL ATALAY (2024). Space Invaders Game (https://www.mathworks.com/matlabcentral/fileexchange/72069-space-invaders-game), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2018a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Strategy & Logic in Help Center and MATLAB Answers
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.1

Added head image

1.0.0