Space shoot em up game using Pygame
Message:
Create a game using python pygame ..and make the ( space shooter ) game.
Also to write a report and making a report that provide:
brief description of aim analyse of requirement of program
design – document of flow chart
testing – tables of test to be under taken to prove that program achieve its goals
Critique – what worked and what didn’t work.
Solution
Alien1
Alien2
Edit Highscore File.py
file = open(“HighScore.txt”,”w”)
file.write(str(0))
file.close()
Space Shots.py
importpygame
import sys
import math
import random
# Chooses The Screen Size
ScreenSizeX = 800
ScreenSizeY = 600
# Initialize Pygame
pygame.init()
GameDisplay = pygame.display.set_mode((ScreenSizeX,ScreenSizeY))
pygame.display.set_caption(‘Space Shots’)
pygame.display.update()
# Define Colors
White = (255,255,255)
Black = (0,0,0)
Blue = (0,0,255)
Red = (255,0,0)
Dark = (40,40,40)
#Load All Images
SpaceShip1 = pygame.image.load(“SpaceShip1.png”)
SpaceShip2 = pygame.image.load(“SpaceShip2.png”)
Alien1 = pygame.image.load(“Alien1.png”)
Alien2 = pygame.image.load(“Alien2.png”)
# Define Fonts
FontText = pygame.font.SysFont(“Cambria”,25)
FontTitle = pygame.font.SysFont(“Times New Roman”,40)
####################################################################### All Classes #######################################################################################
classSpaceShip():
def __init__(self,Acell,iniX,iniY,Image1,Image2,Display):
self.Acell= Acell
self.Vx = 0
self.Vy = 0
self.Move =0
self.Theta = 0
self.iniX = iniX
self.iniY = iniY
self.Image1 = Image1
self.Image2 = Image2
self.RotatedImage = pygame.transform.rotate(self.Image1,self.Theta)
self.ShipRect = self.RotatedImage.get_rect()
self.ShipRect.center = (iniX,iniY)
self.Display = Display
self.Points = 0
self.Lives = 3
self.HighScore = 0
defDrawSpaceShip(self):
self.Display.blit(self.RotatedImage,self.ShipRect.topleft)
defUpdateRot(self):
ifself.Move == 0:
self.RotatedImage = pygame.transform.rotate(self.Image1,self.Theta)
else:
self.RotatedImage = pygame.transform.rotate(self.Image2,self.Theta)
x, y = self.ShipRect.center
self.ShipRect = self.RotatedImage.get_rect() # Replace old rect with new rect.
self.ShipRect.center = (x, y) # Put the new rect’s center at old center.
defUpdateMovement(self):
MoveX = self.Move*math.cos(math.radians(self.Theta))
MoveY = -self.Move*math.sin(math.radians(self.Theta))
#if math.pow(Player.Vx+MoveX,2) + math.pow(Player.Vy+MoveY,2) <math.pow(self.MaxVel,2):
self.Vx += MoveX
self.Vy += MoveY
self.ShipRect.centerx += self.Vx
self.ShipRect.centery += self.Vy
ifself.ShipRect.centerx>ScreenSizeX: self.ShipRect.centerx = 0
ifself.ShipRect.centerx< 0: self.ShipRect.centerx = ScreenSizeX
ifself.ShipRect.centery>ScreenSizeY: self.ShipRect.centery = 0
ifself.ShipRect.centery< 0: self.ShipRect.centery = ScreenSizeY
class Shots():
def __init__(self,Speed,iniX,iniY,Theta,Size,color,Display):
self.color = color
self.Display = Display
self.Vx = Speed*math.cos(math.radians(Theta))
self.Vy = -Speed*math.sin(math.radians(Theta))
self.iniX = iniX
self.iniY = iniY
self.Rect = pygame.Rect(0,0,Size,Size)
self.Rect.center = (iniX,iniY)
self.ShotDestroyed = False
defDrawShot(self):
pygame.draw.ellipse(self.Display, self.color, self.Rect)
defUpdateMovement(self):
self.Rect.centerx += self.Vx
self.Rect.centery += self.Vy
ifself.Rect.centerx>ScreenSizeX or self.Rect.centerx< 0 or self.Rect.centery>ScreenSizeY or self.Rect.centery< 0:
self.ShotDestroyed = True
def Collision(self,Target):
X = self.Rect.centerx
Y = self.Rect.centery
if X <Target.Rect.right and X >Target.Rect.left and Y >Target.Rect.top and Y <Target.Rect.bottom:
self.ShotDestroyed = True
Target.AlienDestroyed = True
return True
else:
return False
class Alien():
def __init__(self,Speed,iniX,iniY,Image1,Image2,SpaceShip,Display):
self.Display = Display
self.iniX = iniX
self.iniY = iniY
ifSpaceShip.ShipRect.centerx>iniX:
self.Vx = Speed
else:
self.Vx = -Speed
ifSpaceShip.ShipRect.centery>iniY:
self.Vy = Speed
else:
self.Vy = -Speed
self.Image1 = Image1
self.Image2 = Image2
self.Rect = self.Image1.get_rect()
self.Rect.center = (iniX,iniY)
self.AlienDestroyed = False
self.Anim = Image1
self.AnimCount = 0
defDrawAlien(self,AnimCicle):
ifself.AnimCount<AnimCicle: self.Anim = self.Image1
ifself.AnimCount>AnimCicle: self.Anim = self.Image2
ifself.AnimCount>AnimCicle*2: self.AnimCount = 0
self.Display.blit(self.Anim,self.Rect)
self.AnimCount += 1
defUpdateMovement(self):
self.Rect.centerx += self.Vx
self.Rect.centery += self.Vy
ifself.Rect.centerx> ScreenSizeX+500 or self.Rect.centerx< -500 or self.Rect.centery> ScreenSizeY+500 or self.Rect.centery< -500:
self.AlienDestroyed = True
def Collision(self,Target):
X = self.Rect.centerx
Y = self.Rect.centery
if X <Target.ShipRect.right and X >Target.ShipRect.left and Y >Target.ShipRect.top and Y <Target.ShipRect.bottom:
self.AlienDestroyed = True
Target.ShipRect.center = (ScreenSizeX/2,ScreenSizeY/2)
Target.Vx = 0
Target.Vy = 0
return True
else:
return False
class Button:
#textoBotão
def __init__(self,color,colorEffect,colorText,line,MSG,Display):
self.color = color
self.colorEffect = colorEffect
self.colorText = colorText
self.line = line
self.MSG = MSG
self.Rect = pygame.Rect(0,0,300,50)
self.Rect.centerx = ScreenSizeX/2
self.Rect.centery = 50+ 90*self.line
self.RectEffect = pygame.Rect(0,0,311,61)
self.RectEffect.centerx = ScreenSizeX/2 -1
self.RectEffect.centery = 50+ 90*self.line -1
self.Display = Display
defDrawRect(self):
pygame.draw.rect(self.Display, self.color, self.Rect, 3)
defButtonText(self,Offset):
Text = FontText.render(self.MSG,True,self.colorText)
self.Display.blit(Text, [ScreenSizeX/2 – Offset,35+ 90*self.line])
def Effect(self,posx,posy):
ifposx>self.RectEffect.left and posx<self.RectEffect.right and posy <self.RectEffect.bottom and posy >self.RectEffect.top:
pygame.draw.rect(self.Display, self.colorEffect, self.RectEffect, 2)
defButtonPress(self,posx,posy,mouseclick):
ifposx>self.RectEffect.left and posx<self.RectEffect.right and posy <self.RectEffect.bottom and posy >self.RectEffect.top and mouseclick == True:
return True
else:
return False
##########################################################################################################################################################
###################################################################### All Functions #####################################################################
defTexto(MSG,cor,posx,posy,linha,GameDisplay):
Texto = FontText.render(MSG,True,cor)
GameDisplay.blit(Texto, [posx,(posy+linha*25)])
def Title(MSG,color,line,X,Display):
Title = FontTitle.render(MSG,True,color)
Display.blit(Title,[ScreenSizeX/2- int(len(MSG)/2)*20+X,70+line*25])
def Game(GameDisplay,LarguraTela,AlturaTela):
Rotate = 0
Move = 0
GameExit = False
Shot = []
Aliens = []
ShotFired = False
clock = pygame.time.Clock()
Player = SpaceShip(0.2,ScreenSizeX/2,ScreenSizeY/2,SpaceShip1,SpaceShip2,GameDisplay)
x=0
y=0
AlienRespawn = 0
while not GameExit:
for event in pygame.event.get():
ifevent.type == pygame.QUIT:
GameExit = True
ifevent.type == pygame.KEYDOWN:
ifevent.key == pygame.K_UP:
Player.Move =Player.Acell
ifevent.key == pygame.K_LEFT:
Rotate = 3
ifevent.key == pygame.K_RIGHT:
Rotate = -3
ifevent.key == pygame.K_SPACE:
ShotFired = True
ifevent.type == pygame.KEYUP:
ifevent.key == pygame.K_UP:
Player.Move = 0
ifevent.key == pygame.K_LEFT:
Rotate = 0
ifevent.key == pygame.K_RIGHT:
Rotate = 0
ifevent.key == pygame.K_ESCAPE:
GameExit = True
GameDisplay.fill(Dark)
ifAlienRespawn> 100:
AlienRespawn = 0
ifrandom.randrange(0, 4) == 0:
x = -100
y = random.randrange(0, ScreenSizeY)
elifrandom.randrange(0, 4) == 1:
x = ScreenSizeX+100
y = random.randrange(0, ScreenSizeY)
elifrandom.randrange(0, 4) == 2:
y = -100
x = random.randrange(0, ScreenSizeX)
elifrandom.randrange(0, 4) == 3:
y = ScreenSizeX-100
x = random.randrange(0, ScreenSizeX)
Aliens.append(Alien(1,x,y,Alien1,Alien2,Player,GameDisplay))
AlienRespawn += 1
NAliens = len(Aliens)
for i in range(len(Aliens)):
Aliens[i].UpdateMovement()
Aliens[i].DrawAlien(15)
for i in range(NAliens):
iflen(Aliens) == NAliens and Aliens[i].AlienDestroyed == True: Aliens.pop(i)
for i in range(len(Aliens)):
if Aliens[i].Collision(Player) == True: Player.Lives -= 1
ifShotFired == True:
ShotFired = False Shot.append(Shots(20,Player.ShipRect.centerx,Player.ShipRect.centery,Player.Theta,5,White,GameDisplay))
NShots = len(Shot)
for i in range(NShots):
Shot[i].UpdateMovement()
Shot[i].DrawShot()
for j in range(len(Aliens)):
if Shot[i].Collision(Aliens[j]) == True: Player.Points += 10
for i in range(NShots):
iflen(Shot) == NShots and Shot[i].ShotDestroyed == True: Shot.pop(i)
Player.Theta += Rotate
ifPlayer.Theta> 359: Player.Theta = 0
ifPlayer.Theta< 0: Player.Theta = 359
Player.UpdateRot()
Player.UpdateMovement()
Player.DrawSpaceShip()
ifPlayer.Lives< 1: GameExit = True
Texto(str(“Points”),White,0,0,0,GameDisplay)
Texto(str(Player.Points),White,0,0,1,GameDisplay)
Texto(str(“Lives”),White,0,0,2,GameDisplay)
Texto(str(Player.Lives),White,0,0,3,GameDisplay)
#Texto(str(AlienRespawn),White,0,0,2,GameDisplay)
#Texto(str(len(Aliens)),White,0,0,3,GameDisplay)
pygame.display.update()
clock.tick(30)
returnPlayer.Points
###################################################################################################################################################################################
################ LOOP ###############
TitleRect = pygame.Rect(0,0,400,120)
TitleRect.centerx = ScreenSizeX/2
TitleRect.centery = 90
NewGame = Button(Black,Blue,Black,2,”New Game”,GameDisplay)
HighScore = Button(Black,Blue,Black,3,” High Score”,GameDisplay)
Instructions = Button(Black,Blue,Black,4,” Instructions”,GameDisplay)
Exit = Button(Black,Blue,Black,5,” Exit”,GameDisplay)
Back = Button(Black,Blue,Black,5,”Back”,GameDisplay)
Window = 0
Points = 0
BestPoints = 0
clock = pygame.time.Clock()
file = open(“HighScore.txt”,”r”)
BestPointsStr = file.read()
BestPoints = int(BestPointsStr)
file.close()
while not GameExit:
for event in pygame.event.get():
ifevent.type == pygame.QUIT:
GameExit = True
ifevent.type == pygame.MOUSEMOTION:
mousex, mousey = event.pos
ifevent.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mouseclick = True
else:
mouseclick = False
GameDisplay.fill(White)
if Window == 0: # Main Menu
NewGame.DrawRect()
HighScore.DrawRect()
Instructions.DrawRect()
Exit.DrawRect()
pygame.draw.rect(GameDisplay, Black, TitleRect, 3)
NewGame.ButtonText(55)
HighScore.ButtonText(65)
Instructions.ButtonText(75)
Exit.ButtonText(35)
Title(“Space Shots”,Black,0,0,GameDisplay)
NewGame.Effect(mousex,mousey)
HighScore.Effect(mousex,mousey)
Instructions.Effect(mousex,mousey)
Exit.Effect(mousex,mousey)
ifNewGame.ButtonPress(mousex,mousey,mouseclick) == True:
Points = Game(GameDisplay,ScreenSizeX ,ScreenSizeY )
if Points >BestPoints:
BestPoints = Points
file = open(“HighScore.txt”,”w”)
file.write(str(BestPoints))
file.close()
Window = 1
mouseclick = False
ifHighScore.ButtonPress(mousex,mousey,mouseclick) == True:
Window = 1
GameDisplay.fill(White)
mouseclick = False
ifInstructions.ButtonPress(mousex,mousey,mouseclick) == True:
Window = 2
GameDisplay.fill(White)
mouseclick = False
ifExit.ButtonPress(mousex,mousey,mouseclick) == True:
GameExit = True
mouseclick = False
if Window == 1: # HighScore
Back.DrawRect()
Back.ButtonText(25)
Back.Effect(mousex,mousey)
Title(“Last Score:”,Black,0,0,GameDisplay)
Title(str(Points),Black,0,150,GameDisplay)
Title(“High Score:”,Black,4,0,GameDisplay)
Title(str(BestPoints),Black,4,150,GameDisplay)
ifBack.ButtonPress(mousex,mousey,mouseclick) == True:
Window = 0
mouseclick = False
if Window == 2: # Instructions
Back.DrawRect()
Back.ButtonText(25)
Back.Effect(mousex,mousey)
Texto(str(“Destroy as many Alien Space Ships as you can.”),Black,200,200,0,GameDisplay)
Texto(str(“Controls:”),Black,200,200,2,GameDisplay)
Texto(str(“Arrow Keys – Movement”),Black,200,200,3,GameDisplay)
Texto(str(“Space Bar – Shot”),Black,200,200,4,GameDisplay)
Texto(str(“EscKey – Goes Back to the Menu”),Black,200,200,4,GameDisplay)
ifBack.ButtonPress(mousex,mousey,mouseclick) == True:
Window = 0
mouseclick = False
pygame.display.update()
clock.tick(30)
pygame.quit()
sys.exit()
quit()