Space Invaders Python/PyGame

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Simple Space Invaders Python/PyGame

DA
11 de Novembro de 2014

NOTES

Files needed to play:


SpaceInvaders.py - the file with to code
spaceship.gif - the player spaceship
enemyship.gif - the enemy spaceship
enemyshipflames.gif - enemy image shown after 1st shot
title.jpg - a title image
shot.wav - spaceship left click shot sound
shotb.wav - spaceship right click shot sound
shoten.wav - enemy shot sound
explosion.wav - enemy explosion sound
explosion2.wav - player explosion sound
music.wav - gameplay music
musicwin.wav - music win
musiclose.wav - music lose
The Game is played using the mouse. In order to destroy the enemy you need to shoot an A-B-A
combination (A - left mouse click, B - right maouse click).

GAME CODE

The comments are in portuguese.


#! u s r / b i n / env python
im po rt pygame
from pygame . l o c a l s i mpo rt
s c r e e n m o d e = ( 8 0 0 , 5 0 0 ) #dimensao do e c r a de j o g o
c o l o r b l a c k = (0 , 0 , 0)
c o l o r d b l u e = (0 , 0 , 20)
c o l o r d g r a y = (20 , 20 , 20)
background = [ c o l o r b l a c k , c o l o r d g r a y , c o l o r d b l u e ] #c o r e s de fundo do e c r a
c o l o r w h i t e = (255 , 255 , 255)
c o l o r y e l l o w = (238 , 238 , 0)
c o l o r o r a n g e = (255 , 165 , 0)
c o l o r r e d = (255 , 0 , 0)
c o l o r g r e e n = (0 , 255 , 0)
p o s c o r e = [ 5 , 5 ] #p o s i c a o dos p o n t o s no e c r a
poenemy = [ 5 , 4 8 0 ] #p o s i c a o da contagem de i n i m i g o s d e s t r u i d o s
p o l i v e s = [ 7 2 0 , 2 ] #p o s i c a o das
v i d a s do u t i l i z a d o r
c l o c k = pygame . time . Clock ( )

c l a s s Game :
#i n i c i a l i z a c a o
def
init
( self ) :
pygame . i n i t ( )
s e l f . s c r e e n = pygame . d i s p l a y . s e t m o d e ( s c r e e n m o d e )
pygame . d i s p l a y . s e t c a p t i o n ( Space I n v a d e r s MDP P r o j )
s e l f . quit = False
s e l f . score = 0
self . lives = 5
s e l f . lose = False
s e l f . win = F a l s e
s e l f . enemycount = 0
s e l f . e n e m i e s = 10
s e l f . scoretxt = str ()
s e l f . enemytxt = s t r ( )
s e l f . livestxt = str ()
s e l f . mousebutton = pygame . mouse . g e t p r e s s e d ( )

s e l f . i n i t i m g = pygame . image . l o a d ( t i t l e . j p g )
s e l f . u s e r s h i p = pygame . image . l o a d ( s p a c e s h i p . g i f )
enemy = pygame . image . l o a d ( enemyship . g i f )
enemydown = pygame . image . l o a d ( e n e m y s h i p f l a m e s . g i f )
s e l f . enemyship = [ enemy , enemydown ]
s e l f . x = 0 #p o s i c a o x i n i c i a l nave do u t i l i z a d o r
s e l f . y = 0 #p o s i c a o y i n i c i a l nave do u t i l i z a d o r
s e l f . enx = [ 7 4 0 , 6 4 0 , 5 4 0 , 4 4 0 , 3 4 0 , 7 4 0 , 6 4 0 , 5 4 0 , 4 4 0 , 3 4 0 ] #p o s i c a o x i n i c i a l
nave i n i m i g a
s e l f . eny = [ 2 0 , 2 0 , 2 0 , 2 0 , 2 0 , 6 0 , 6 0 , 6 0 , 6 0 , 6 0 ] #p o s i c a o y i n i c i a l n a v e s
inimigas
s e l f . b u l l e t s a = [ ] #b a l a s t i p o a
s e l f . b u l l e t s b = [ ] #b a l a s t i p o b
s e l f . e n b u l l e t = [ ] #b a l a s do i n i m i g o
s e l f . h i t a = [ False , False , False , False , False ,
F a l s e , F a l s e , F a l s e , F a l s e , F a l s e ] #a t i n g i d o por b a l a t i p o a
s e l f . hitb = [ False , False , False , False , False ,
F a l s e , F a l s e , F a l s e , F a l s e , F a l s e ] #a t i n g i d o por b a l a t i p o b
s e l f . hitab = [ False , False , False , False , False ,
F a l s e , F a l s e , F a l s e , F a l s e , F a l s e ] #a t i n g i d o por combinacao a >b
s e l f . hitaba = [ False , False , False , False , False ,
F a l s e , F a l s e , F a l s e , F a l s e , F a l s e ] #a t i n g i d o por combinacao a >
b> a
s e l f . enshot = [ False , False , False , False , False ,
F a l s e , F a l s e , F a l s e , F a l s e , F a l s e ] #a t i n g i d o p e l o i n i m i g o
s e l f . e n s h o t t i m e = [ 1 0 , 5 5 , 1 5 , 3 5 , 3 0 , 8 5 , 3 0 , 6 0 , 3 5 , 4 5 ] #tempo para o d i s p a r o
dos i n i m i g o s
s e l f . r i g h t = [ True , True , True , True , True ,
True , True , True , True , True ] #a t i n g i u o l i m i t e d i r e i t o da j a n e l a
s e l f . l e f t = [ False , False , False , False , False ,
F a l s e , F a l s e , F a l s e , F a l s e , F a l s e ] #a t i n g i u o l i m i t e e s q u e r d o da
janela
s e l f . aux = 2 #v a r i a v e l a u x i l i a r para t r o c a r o fundo
s e l f . taux = F a l s e #v a r i a v e l a u x i l i a r para t r o c a r o fundo
#c a r r e g a o s s o n s a s e r t o c a d o s
s e l f . s h o t a s o u n d = pygame . mixer . Sound ( s h o t . wav )
s e l f . s h o t b s o u n d = pygame . mixer . Sound ( s h o t b . wav )
s e l f . s h o t e n s o u n d = pygame . mixer . Sound ( s h o t e n . wav )
s e l f . e x p l o s i o n = pygame . mixer . Sound ( e x p l o s i o n . wav )
s e l f . e x p l o s i o n t w o = pygame . mixer . Sound ( e x p l o s i o n 2 . wav )
s e l f . music = pygame . mixer . Sound ( music . wav )
s e l f . m u s i c l o s e = pygame . mixer . Sound ( m u s i c l o s e . wav )
s e l f . musicwin = pygame . mixer . Sound ( musicwin . wav )

#c o d i g o para o t e x t o
d e f t e x t ( s e l f , pos , t x t ) :
#f o n t e a s e r usada e tamanho
f o n t = pygame . f o n t . Font ( None , 2 5 )
t e x = f o n t . r e n d e r ( t x t , True , c o l o r w h i t e )
s e l f . s c r e e n . b l i t ( tex , pos )
d e f t e x t o v e r ( s e l f , pos , t x t ) :
#f o n t e a s e r usada e tamanho
f o n t = pygame . f o n t . Font ( None , 6 0 )
t e x = f o n t . r e n d e r ( t x t , True , c o l o r w h i t e )
s e l f . s c r e e n . b l i t ( tex , pos )

#update do j o g o
d e f update ( s e l f ) :
i f s e l f . win == F a l s e :
i f s e l f . l o s e == F a l s e :
i f s e l f . lives > 0:
#mover a nave usando o r a t o
( s e l f . x , s e l f . y ) = pygame . mouse . g e t p o s ( )
#ve que t e c l a do r a t o f o i c l i c a d a
s e l f . mousebutton = pygame . mouse . g e t p r e s s e d ( )
#t o c a um som para o s d i s p a r o s e a d i c i o n a a s b a l a s
i f s e l f . mousebutton [ 0 ] == 1 :
s e l f . shotasound . play ( )
i f s e l f . y < 310:
s e l f . y = 310
s e l f . b u l l e t s a . append ( [ s e l f . x + 3 0 , s e l f . y ] )
i f s e l f . mousebutton [ 2 ] == 1 :
s e l f . shotbsound . play ( )
i f s e l f . y < 310:
s e l f . y = 310
s e l f . b u l l e t s b . append ( [ s e l f . x + 8 5 , s e l f . y ] )
#l i m i t a a nave do u t i l i z a d o r
i f s e l f . y > 4 3 0 : #l i m i t e s u p e r i o r
s e l f . y = 430
i f s e l f . y < 3 1 0 : #l i m i t e i n f e r i o r
s e l f . y = 310
i f s e l f . x > 6 8 0 : #l i m i t e l a t e r a l d i r e i t o
s e l f . x = 680
#move a nave i n i m i g a
movex = 15
f o r i in range ( s e l f . enemies ) :
i f s e l f . eny [ i ] >= 4 3 0 :
s e l f . l o s e = True
i f s e l f . r i g h t [ i ] == True :
s e l f . enx [ i ] = s e l f . enx [ i ] movex
i f s e l f . enx [ i ] < 0 :
s e l f . eny [ i ] = s e l f . eny [ i ] + 20
movex += 10
s e l f . right [ i ] = False
s e l f . l e f t [ i ] = True
i f s e l f . l e f t [ i ] == True :
s e l f . enx [ i ] = s e l f . enx [ i ] + movex
i f s e l f . enx [ i ] > 7 3 5 :
s e l f . eny [ i ] = s e l f . eny [ i ] + 20
movex += 10
s e l f . l e f t [ i ] = False
s e l f . r i g h t [ i ] = True
s e l f . draw ( )
d e f enemy ( s e l f ) :
f o r i in range ( s e l f . enemies ) :
#c a r r e g a a imagem da nave i n i m i g a
i f s e l f . h i t a b a [ i ] == F a l s e :
i f s e l f . h i t a [ i ] == F a l s e :
s e l f . s c r e e n . b l i t ( s e l f . enemyship [ 0 ] , ( s e l f . enx [ i ] ,
i f s e l f . h i t a [ i ] == True :
s e l f . s c r e e n . b l i t ( s e l f . enemyship [ 1 ] , ( s e l f . enx [ i ] ,
if

s e l f . eny [ i ] ) )
s e l f . eny [ i ] ) )

s e l f . win == F a l s e :
i f s e l f . l o s e == F a l s e :
f o r j in range ( s e l f . enemies ) :
i f s e l f . lives > 0:
#t o c a som para d i s p a d o do i n i m i g o e a d i c i o n a s a s b l a s i n i m i g a s
de x em x i t e r a c o e s
i f s e l f . h i t a b a [ j ] == F a l s e :

i f count%s e l f . e n s h o t t i m e [ j ] == 0 :
s e l f . e n b u l l e t . append ( [ s e l f . enx [ j ] + 4 0 , s e l f . eny [ j ] ] )
s e l f . shotensound . play ( )
nenbullet = s e l f . enbullet [ : ]
#move a s b a l a s do i n i m i g o
f o r i in range ( len ( s e l f . e n b u l l e t ) ) :
s e l f . enbullet [ i ] [ 1 ] = s e l f . enbullet [ i ][1]+15
i f s e l f . e n b u l l e t [ i ] [ 1 ] > 5 0 0 : #remove a b a l a quando a t i n g e o f i m da
janela
n e n b u l l e t . remove ( s e l f . e n b u l l e t [ i ] )
e l s e : #de s en h a a s b a l a s
xenbullet = s e l f . enbullet [ i ] [ 0 ]
yenbullet = s e l f . enbullet [ i ] [ 1 ]
pygame . draw . e l l i p s e ( s e l f . s c r e e n , c o l o r g r e e n
,( xenbullet , yenbullet ,5 ,5) )
i f ( x e n b u l l e t > s e l f . x and x e n b u l l e t < s e l f . x+120 and
y e n b u l l e t > s e l f . y and y e n b u l l e t < s e l f . y+15) : #v e r i f i c a s e o
utilizador foi atingido
pygame . draw . e l l i p s e ( s e l f . s c r e e n , c o l o r r e d , [ x e n b u l l e t ,
y e n b u l l e t , 3 0 , 3 0 ] ) #d e se nh a a e x p l o s a o
s e l f . e x p l o s i o n . p l a y ( ) #t o c a som da e x p l o s a o
s e l f . l i v e s = 1 #remove v i d a
#c o d i g o para o d es e nh o
d e f draw ( s e l f ) :
#limpa e p r e e n c h e o e c r a com c o r de fundo
i f count%60 == 0 :
i f s e l f . aux == 0 :
s e l f . taux = True
if

s e l f . aux == 2 :
s e l f . taux = F a l s e

if

s e l f . taux == F a l s e :
s e l f . aux = 1
else :
s e l f . aux += 1
s e l f . s c r e e n . f i l l ( background [ s e l f . aux ] )
#c a r r e g a a imagem da nave do u l i l i z a d o r
s e l f . screen . b l i t ( s e l f . usership , ( s e l f . x ,

s e l f . y) )

nbulletsa = s e l f . bulletsa [ : ]
nbulletsb = s e l f . bulletsb [ : ]
#move a s b a l a s a
f o r i in range ( len ( s e l f . b u l l e t s a ) ) :
s e l f . bulletsa [ i ] [ 1 ] = s e l f . bulletsa [ i ][1] 15
i f s e l f . b u l l e t s a [ i ] [ 1 ] < 0 : #remove a b a l a quando a t i n g e o f i m da j a n e l a
n b u l l e t s a . remove ( s e l f . b u l l e t s a [ i ] )
e l s e : #de s en ha a s b a l a s
xbulleta = s e l f . bulletsa [ i ] [ 0 ]
ybulleta = s e l f . bulletsa [ i ] [ 1 ]
pygame . draw . r e c t ( s e l f . s c r e e n , c o l o r y e l l o w
, Rect ( x b u l l e t a , y b u l l e t a , 2 , 5 ) )
f o r j i n r a n g e ( s e l f . e n e m i e s ) : #v e r i f i c a s e o i n i m i g o f o i a t i n g i d o
i f s e l f . h i t a b a [ j ] == F a l s e :
i f ( x b u l l e t a > s e l f . enx [ j ] and x b u l l e t a < s e l f . enx [ j ]+65 and
y b u l l e t a > s e l f . eny [ j ] and y b u l l e t a < s e l f . eny [ j ]+15) : #d es en h a
as balas
pygame . draw . e l l i p s e ( s e l f . s c r e e n , c o l o r r e d , [ x b u l l e t a , y b u l l e t a
,30 ,30])
s e l f . explosion . play ( )
s e l f . h i t a [ j ] = True
i f s e l f . h i t a [ j ] == True and s e l f . h i t b [ j ] == True :
s e l f . h i t a b a [ j ] = True
s e l f . enemycount += 1 #a c t u a l i z a o s i n i m i g o s a b a t i d o s
s e l f . s c o r e += 1000 #a c t u a l i z a a pontuacao
i f s e l f . enemycount == s e l f . e n e m i e s :

s e l f . win = True
s e l f . s c o r e += 5 #a c t u a l i z a a pontuacao
#move a s b a l a s b
f o r i in range ( len ( s e l f . b u l l e t s b ) ) :
s e l f . bulletsb [ i ] [ 1 ] = s e l f . bulletsb [ i ][1] 15
i f s e l f . b u l l e t s b [ i ] [ 1 ] < 0 : #remove a b a l a quando a t i n g e o f i m da j a n e l a
n b u l l e t s b . remove ( s e l f . b u l l e t s b [ i ] )
e l s e : #de s en ha a s b a l a s
xbulletb = s e l f . bulletsb [ i ] [ 0 ]
ybulletb = s e l f . bulletsb [ i ] [ 1 ]
pygame . draw . e l l i p s e ( s e l f . s c r e e n , c o l o r o r a n g e
,( xbulletb , ybulletb ,5 ,5) )
f o r j i n r a n g e ( s e l f . e n e m i e s ) : #v e r i f i c a s e o i n i m i g o f o i a t i n g i d o
i f s e l f . h i t a b a [ j ] == F a l s e :
i f ( x b u l l e t b > s e l f . enx [ j ] and x b u l l e t b < s e l f . enx [ j ]+65 and
y b u l l e t b > s e l f . eny [ j ] and y b u l l e t b < s e l f . eny [ j ]+15) : #d es en h a
as balas
pygame . draw . e l l i p s e ( s e l f . s c r e e n , c o l o r r e d , [ x b u l l e t b , y b u l l e t b
,30 ,30])
s e l f . explosion . play ( )
i f s e l f . h i t a [ j ] == True :
s e l f . h i t b [ j ] = True
s e l f . s c o r e += 5 #a c t u a l i z a a pontuacao
#c a r r e g a a s n a v e s i n i m i g a s
s e l f . enemy ( )
s e l f . s c o r e t x t = S c o r e : +s t r ( s e l f . s c o r e )
s e l f . enemytxt = Enemies Down : +s t r ( s e l f . enemycount )
s e l f . l i v e s t x t = L i v e s : +s t r ( s e l f . l i v e s )
#c a r r e g a o t e x t o nas d e v i d a s p o s i c o e s
s e l f . text ( poscore , s e l f . s c o r e t x t )
s e l f . text ( polives , s e l f . l i v e s t x t )
s e l f . t e x t ( poenemy , s e l f . enemytxt )
i f s e l f . l i v e s <= 0 o r s e l f . l o s e == True :
s e l f . t e x t o v e r ( ( 2 6 0 , 2 0 0 ) , GAME OVER )
s e l f . t e x t o v e r ( ( 2 8 0 , 2 7 0 ) , YOU LOSE )
i f s e l f . win == True :
s e l f . t e x t o v e r ( ( 3 1 0 , 2 0 0 ) , YOU WIN )
s e l f . t e x t o v e r ( ( 2 9 0 , 2 7 0 ) , S c o r e +s t r ( s e l f . s c o r e ) )

game = Game ( )
time = 13
count=0
start = False
end = F a l s e
w h i l e not game . q u i t :
#i n i c i a o j o g o quando s e c a r r e g a em e n t e r
i f not s t a r t :
game . s c r e e n . b l i t ( game . i n i t i m g , ( 0 , 0 ) )
k e y s = pygame . key . g e t p r e s s e d ( )
i f k e y s [K RETURN ] :
s t a r t = True
f o r e v e n t i n pygame . e v e n t . g e t ( ) :
i f e v e n t . t y p e == QUIT :
game . q u i t = True
pygame . d i s p l a y . f l i p ( )
if

start :
#t o c a musica no j o g o de x em x tempo
i f count %(260 time +52 time ) == 0 : # CHANGE HERE TO MATCH YOUR SONG DURATION
game . music . p l a y ( )
count += 1

#t r a t a de e v e n t o s
f o r e v e n t i n pygame . e v e n t . g e t ( ) :
i f e v e n t . t y p e == QUIT :
game . q u i t = True
#update a cada i t e r a c a o
game . update ( )
#c a s o p e r c a o j o g o t o c a e s t a musica
i f game . l i v e s <= 0 o r game . l o s e == True :
game . music . s t o p ( )
i f not end :
game . m u s i c l o s e . p l a y ( )
end = True
#c a s o ganhe o j o g o t o c a e s t a
i f game . win == True :
game . music . s t o p ( )
i f not end :
game . musicwin . p l a y ( )
end = True
t i m e p a s s e d = c l o c k . t i c k ( time )
#mostra o update das c e n a s
pygame . d i s p l a y . f l i p ( )
#f e c h a a j a n e l a
i f game . q u i t==True :
pygame . q u i t ( )

You might also like