Python Project Report
Python Project Report
Submitted To ,
Md. Asfaqur Rahman
Lecturer
Department of Computer Science and Engineering
Faculty of Science and Information Technology
Submitted By,
Name : Jhion Adbar Opi
ID : 221-15-5948
Section : 61_L
Department of Computer Science and Engineering
Faculty of Science and Information Technology
This project report details the development of a 2D “Target Shooting Space Invaders” game developed
using the Pygame library in Python. The game involves a player-controlled “Aircraft” that shoots
“Lasers” to destroy “Alien spaceships” for points. It also includes features like obtaining extra points for
destroying a special alien. The Player “Aircraft” must avoid collisions with the “Aliens” and their lasers
while trying to destroy them. The game also includes obstacles that the player must navigate around and
take shelter from the “Alien Lasers”. The report covers the game's design, implementation,
functionalities, and concludes by summarizing the project's achievements.
Introduction
The objective of this project is to create the classic Two-dimensional “Target Shooting Space Invaders”
arcade game from the 2007s Utilizing Python's “pygame” library. The game development covers various
aspects such as sprite management, collision detection, and sound integration.
In this game, the player controls a defender “Aircraft” that moves horizontally across ( x_start =
screen_width ) the bottom of the screen and fires missiles at enemy “Alien ships” to destroy alien ships.
The player Aircraft must avoid collisions with the aliens and their lasers while trying to destroy them.
The game is initialized with a Game class that sets up the player, obstacles, aliens, and other game
elements. The player is controlled using keyboard inputs and can move left, right, and shoot lasers. The
game also includes obstacles (Created using A Obstacle class) that are placed randomly on the screen the
player must navigate around and stay under for safety from the alien leaser. The aliens are created using
an Alien class and are arranged in a grid formation. The aliens move horizontally and vertically and shoot
lasers at the player. The game includes several functions to handle collisions between the player, aliens,
lasers, and obstacles. The player's lives are displayed at the top right of the screen, and the score is
displayed at the top left. The game ends when the player loses all their lives or hit by any alien or destroys
all the aliens. The game also includes sound effects for shooting lasers ( Player Aircraft and Aline ships),
explosions, and game over. The game's background music is played on a loop.
To recreate this game on our software controls all of the Space Invaders logic and passes the data to byte-
addressable Visual Studio Code in hardware, which is then displayed on a monitor.
Overall, this project demonstrates the use of Pygame to create a simple yet engaging game. The game
could be improved by adding more levels, power-ups, and enemy types. The code could also be optimized
for better performance and readability.
Design and Implementation
The game is implemented using object-oriented programming principles, with separate classes for the player,
aliens, lasers, and obstacles. The game loop is responsible for updating the game state, handling user input, and
rendering the game graphics. The game uses sprites for all game objects, which are drawn to the screen using
Pygame's built-in sprite rendering functions.
Software
To recreate this game on our software controls all of the Space Invaders logic and passes the
data to byte-addressable which is Visual Studio Code in hardware, which is then displayed
on a monitor.
Pygame Install
Pygame is a cross-platform set of Python modules designed for writing video games .
Main.py
First, we import the sys and pygame modules. The pygame module contains the functionality we need to
make a game.
We use tools in the sys module to exit the game when the player
quits.
The call to pygame.display.flip() tells Pygame to make the most recently drawn screen visible. In this case, it
simply draws an empty screen on each pass through the while loop, erasing the old screen so only the new screen
is visible. When we move the game elements around, pygame.display .flip() continually updates the display to
show the new positions of game elements and hide the old ones,
creating the illusion of smooth movement. To determine the
screen size screen_width=1500pix and screen_height = 765pix is
set.
At the end of the file, we create an instance of the game and then
call run_game(). We place run_game() in an if block that only
runs if the file is called directly.
When run this main.py file,
should see an empty Pygame
window.
Then to start for actual game interface, we will inherite all of our class as follow,
Player part..
Obstacle part..
First of all in __init__ function obstacle is inherited,
In hear
shape from
obstacle.py
is set and
size of all
block
(block class) is set in 6 . Amount can be given as 4. In x axes the block position is define as [num * (screen_width
/ self.obstacle_amount) for num in range(self.obstacle_amount)] in hear it is offset so that distance is even. For to
define every other block self.create_multiple_obstacles is used which inherit self.obstacle_x_positions . Distance
is set as x axis screen width/2 and y axis in 575 pix.
In “def create_obstacle(self, x_start, y_start,
offset_x):” to define the string in row and
colam enumerate is used.
Offset_x is used in x axis so that it stay
as even as possible in screen size. To set
a block color “ block = obstacle.Block
(self.block_size,(241,79,80),x,y)” is
used as 241,79,80 is a nice charry red
color.
To add multiple block , self.create_multiple_obstacles is
used which inherit self.obstacle_x_positions.
With this block as set. With “self.blocks.draw(screen)” in
run function . This will work as.
Alines part…
In Game class __init__ function alines and aline_leasers are define.
By using aline_setup we could define how many will be added.
self.alien_direction = 1 is used for movement .
For extra one that float in up of screen extra is use, spawn time is
used for random side 400 and 800 ms.
Collision_checks part…
Collision_checks is where all the magic happened.
Under Game class, Collision_checks function define player and
alien collision . It also collaid with block.
Every life is gone then pygame will quit and system will exit.
alien will also can destroy the block and get the player. Then pygame
will quit and system will exit.
Player Lives…
This is under Game class __init__ funtion ,
life is set as 5 which will show in up
right side of screen.
Display_score..
Sound system…
Under Game class __init__ function all the sound will
generate. It is set as “pygame.mixer.Sound”.
The sound file is in wav format. Only Music is set in loop
that will play all the time. Other are assign as per needed.
Time of play also declare.
Victory_message…
Game_over massage..
The
game window will stay 5 sec and pygame quit,
system exit. Everything ends hear.
Run Function..
In player class (pygame.sprite.Sprite) is assigned in __init__ function pos, constraint and speed is used as variable.
After inascient the super () __init__
contractor .
A picture name 221-15-
5948 is used.
Self.speed = speed is for the player to
move accordingly.
For the player to stay in the frame of screen self.max_x_constraint =
constraint is used.
Everything is ready then its “True”.
To remove the leaser from screen it is set to go beyond the screen for 50. So they will be kill.
Every thing is in right order then it will be update and send to main.py.
score_surf = self.alien_lasers.draw(screen)
self.score += alien.value
self.font.render(f'score:
self.extra.draw(screen)
laser.kill() {self.score}',False,'white')
self.display_lives()
self.ex.play() score_rect =
score_surf.get_rect(topleft = (10,-10)) self.display_score()
if
pygame.sprite.spritecollide(laser,self.e screen.blit(score_surf,score_rect) self.victory_message()
xtra,True):
def victory_message(self): if __name__ == '__main__':
laser.kill()
if not self.aliens.sprites(): pygame.init()
self.score += 500
victory_surf = screen_width = 1500
if self.alien_lasers: self.font.render('GAME DEV
THANKS \n THE screen_height = 765
for laser in self.alien_lasers: WINNER',False,'white')
screen =
# alien ano obastical block victory_rect = pygame.display.set_mode((screen_wid
ke hit korbr victory_surf.get_rect(center = th,screen_height)) # ano theke display
(screen_width / 2, screen_height / 2)) serface hoiche
if
pygame.sprite.spritecollide(laser,self.bl clock = pygame.time.Clock()
ocks,True): screen.blit(victory_surf,victory_rect)
game = Game()
laser.kill() def game_over(self):
ALIENLASER =
if game_over_surf = pygame.USEREVENT + 1
pygame.sprite.spritecollide(laser,self.pl self.font.render('GAME OVER \n
ayer,False): Thanks From GAME DEV Team',
True, 'white') pygame.time.set_timer(ALIENLASER
laser.kill() ,800)
game_over_rect =
print('great creator game_over_surf.get_rect(center = while True:
opi_da') (screen_width / 2, screen_height / 2))
for event in pygame.event.get():
self.bomb.play screen.blit(game_over_surf,
game_over_rect) if event.type == pygame.QUIT:
self.lives -= 1
pygame.display.flip() pygame.quit()
if self.lives <= 0:
sys.exit()
self.game_over()
if event.type == ALIENLASER: game.run() clock.tick(60) # frem ret limit 60
korchi
game.alien_shoot() pygame.display.flip()
screen.fill((30,30,30))
Player.py
import pygame if keys[pygame.K_SPACE] and self.ready:
super().__init__() self.uzi.play()
self.get_input()
if keys[pygame.K_RIGHT]:
self.constraint()
self.rect.x += self.speed
self.recharge()
elif keys[pygame.K_LEFT]:
self.lasers.update()
self.rect.x -= self.speed
Alien.Py
import pygame self.rect = self.image.get_rect(topleft =
(x,y))
class Alien(pygame.sprite.Sprite):
if color == 'red': self.value = 100
def __init__(self,color,x,y):
elif color == 'green': self.value = 200
super().__init__()
else: self.value = 300
file_path = '../python project/' + color +
'.png' def update(self,direction):
super().__init__() x = -50
self.image = self.speed = 3
pygame.image.load('../python
project/extra.png').convert_alpha() self.rect = self.image.get_rect(topleft =
(x,80))
if side == 'right':
def update(self):
x = screen_width + 50
self.rect.x += self.speed
self.speed = - 3
Laser.py
import pygame self.height_y_constraint = screen_height
Obastacle.py
import pygame
class Block(pygame.sprite.Sprite):
def __init__(self,size,color,x,y):
super().__init__()
self.image = pygame.Surface((size,size))
self.image.fill(color)
' xxxxxxxxxxxxxxxxxxxxxxx',
'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
' xxxxxxxxxxxxxxxxxxxxxxx',
' xx xx' ]
Results
The the classic Two-dimensional “Target Shooting Space Invaders” successfully implements a playable
and enjoyable space shooter experience. It was developed using Python and Pygame.
The game offers the following functionalities:
➢ Player controls a spaceship that can move and fire lasers.
➢ Multiple types of aliens with varying point values appear and move horizontally.
➢ A special alien with a higher point value can appear occasionally on top of the screen.
➢ Collisions between the player and aliens/lasers are detected, affecting lives and score.
➢ The game displays the current score and remaining lives in up right and left.
➢ The inclusion of sound effects and a scoring system adds to the game's immersion.
➢ The game loop ensures consistent frame rates, providing a fluid gaming experience.
➢ The game ends when the player loses all lives or eliminates all aliens, displaying an appropriate
message.
This project demonstrates the use of Python and Pygame to create a simple yet engaging 2D
“Target Shooting Space Invaders” game. The game features intuitive controls, multiple levels, and
a scoring system that rewards players for their performance. The game is fully functional and
playable, with smooth graphics and responsive controls. The use of object-oriented programming
principles and sprites for game objects makes the code easy to read and maintain. Overall, this
project provides a solid foundation for creating more complex games using Python and Pygame.
Whether you are boy or grown up man this kind of small fun project will bring back the memory
of from the 2007. That small window screen and little light , sound of nostalgia will bring back
that smile. Life Parahin.
Referance