Simple Space Invaders Python/PyGame DA 11 de Novembro de 2014
1
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 playe played d using using the mouse. mouse. In order order to destro destroy y the enemy enemy you you need need to shoot an A-B-A combination (A - left mouse click, B - right maouse click).
1
2
GAME CODE
The comments are in portuguese. #! u s r / b i n / e nv p yt ho n import pygame from pygame . l o c a l s i mp or t ∗ s c re e n m o d e = ( 8 0 0 , 5 0 0 ) #d im en sa o d o e c r a d e j o g o c o l or b l a ck = ( 0 , 0 , 0 ) c o l o r d b l u e = ( 0 , 0 , 2 0) c o l o r d g r ay = ( 2 0 , 2 0 , 2 0) b ac kg ro un d = [ 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 d e f u nd o d o e c r a c o l o r w h i t e = ( 2 55 , 2 55 , 2 55 ) c o l o r y e l l o w = ( 2 3 8 , 2 38 , 0 ) c o l o r o r a n g e = ( 2 55 , 1 65 , 0 ) c o l o r r e d = ( 2 55 , 0 , 0 ) c o l or g r e en = ( 0 , 2 55 , 0 ) p o sc o re = [ 5 , 5 ] #p o s i c ao d os p o nt o s n o e c r a p oe ne my = [ 5 , 4 8 0 ] #p o s i c a o d a c on ta ge m d e 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 os ic ao d as v id as d o u t i l i z a d o r clo ck = pygame. time . Clock ()
c l a s s Game: #inicializacao d e f init ( self ) : pygame. in it () s e l f . s c r e e n = p yg ame . d i s p l a y . s e t m o d e ( s c r e e n m o d e ) p yg ame . d i s p l a y . s e t c a p t i o n ( ” S pa ce I n v a d e r s − MDP Pr oj ” ) s e l f . q u i t = F a l se sel f . score = 0 self . lives = 5 s e l f . l o s e = F a ls e s e l f . w in = F a l s e se l f . enemycount = 0 s e l f . e n em i es = 1 0 s e l f . s c o r e t xt = s t r ( ) s e l f . e n em y tx t = s t r ( ) s e l f . l i v e s t x t = s t r ( ) s e l f . m o us e bu t to n = py ga me . m ou se . g e t p r e s s e d ( )
2
se l f . ini tim g = pygame. image . load ( ” t i t l e . j p g ” ) se l f . use rsh ip = pygame. image . load ( ” s p a c e s h i p . g i f ” ) enemy = pygame. image . loa d ( ”enemyship . gi f ”) enemydown = pygame. image . loa d (”enemyshi pflames . gi f ” ) s e l f . enemyship = [ enemy , enemydown ] s e l f . x = 0 #p os ic ao 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 os ic ao 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 40 , 5 40 , 4 40 , 3 40 , 7 40 , 6 40 , 5 40 , 4 40 , 3 40 ] #p o si ca o x i n i c i a l n av e i n i m i g a s e l f . eny = [ 2 0 , 20 , 20 , 20 , 20 , 60 , 6 0 , 60 , 6 0 , 6 0] #p o si c ao y i n i c i a l n av es inimigas s e l f . b u l l e t s a = [ ] #b a la s t i p o a s e l f . b u l l e t s b = [ ] #b a la s t i p o b s e l f . e n b u l l e t = [ ] #b a l a s d o i n i m i g o s e l f . h i t a = [ F a ls e , F a ls e , F a ls e , F a ls e , F a ls e , F a l se , F a l se , F a l se , F a l se , F a l s e ] #a t i n gi d o p or b al a t i p o a s e l f . h i t b = [ F a ls e , F a ls e , F a ls e , F a ls e , F a ls e , F a l se , F a l se , F a l se , F a l se , F a l s e ] #a t i n gi d o p or b al a t i p o b s e l f . h i t a b = [ F a l se , F a l se , F a l se , F a l se , F a l se , F a l se , F a l se , F a l se , F a l se , F a l s e ] #a t i n g i d o p o r c om bi na ca o a
b
−>
s e l f . h i t a b a = [ F a l se , F a l se , F a l se , F a l se , F a l se , F a l se , F a l se , F a l se , F a l se , F a l s e ] #a t i n g i d o p o r c om bi na ca o a b−> a
−>
s e l f . e n s ho t = [ F a l se , F a l se , F a l se , F a l se , F a l se , F a l se , F a l se , F a l se , F a l se , F a l s e ] #a t i n g i d o p e l o i n i mi g o s e l f . e ns ho tt im e = [ 1 0 , 5 5 , 1 5 , 3 5 , 3 0 , 8 5 , 3 0 , 6 0 , 3 5 , 4 5] #t em po p a r a o d i s p a r o d os i n i m i g o s s e l f . r i g h t = [ T ru e , T ru e , T ru e , T ru e , T ru e , True , True , True , True , True ] #a t i ng i u o l i m i t e d i r e i t o da j a n el a s e l f . l e f t = [ F a ls e , F a ls e , F a ls e , F a ls e , F a ls e , F a l se , F a l se , F a l se , F a l se , F a l s e ] #a t i n g i u o l i m i t e e sq ue rd o da j a n e l a s e l f . a ux = 2 #v a r i a v e l a u x i l i a r p ar a t r o c a r o f un do s e l f . t a ux = F a l s e #v a r i a v e l a u x i l i a r p ar a t r o c a r o f un do #c a rr e g a o s s on s a s e r t oc a do s se l f . shotasound = pygame. mixer . Sound( ”s hot . wav” ) se l f . shotbsound = pygame. mixer . Sound( ”shotb .wav” ) se l f . shotensound = pygame. mixer . Sound( ”sho ten . wav” ) s e l f . e x p l o s i o n = py ga me . m ix e r . S ou nd ( ” e x p l o s i o n . w av ”) self . explosiontwo = pygame. mixer .Sound( ” e x p l o s i o n 2 . w av ”) se l f . music = pygame. mixer . Sound( ”music . wav” ) se l f . musi clo se = pygame. mixer . Sound( ” m u s i c l o s e . w av ” ) se l f . musicwin = pygame. mixer . Sound( ”musicwin . wav” )
#c o d ig o p ar a o t e x t o d e f tex t ( sel f , pos , txt ) : #f o n t e a s e r u sa d a e t am anh o fon t = pygame. font . Font( None,2 5) t e x = f o n t . r e n d e r ( t x t , T ru e , c o l o r w h i t e ) s e l f . s c r e e n . b l i t ( t ex , p o s ) d e f tex tov er ( se lf , pos , txt ) : #f o n t e a s e r u sa d a e t am anh o fon t = pygame. font . Font( None,6 0) t e x = f o n t . r e n d e r ( t x t , T ru e , c o l o r w h i t e ) s e l f . s c r e e n . b l i t ( t ex , p o s )
3
#u p da t e d o d e f u p d at e ( i f s e l f i f
jogo se lf ) : . w in == F a l s e : s e l f . l o s e == F a l s e : i f s e l f . l i v e s > 0 : #m ov er a n a ve u s an d o o r a t o ( s e l f . x , s e l f . y ) = p yg ame . mo use . g e t p o s ( ) #ve q ue t e c l a d o r a t o f o i c l i ca d a s e l f . m o us e bu t to n = py ga me . m ou se . g e t p r e s s e d ( ) #t o ca um som p a r a 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 . m o us e bu t to n [ 0 ] == 1 : s e l f . shotasound . play () i f s e l f . y < 3 1 0 : s e l f . y = 3 10 s e l f . b u l l e t s a . a pp end ( [ s e l f . x + 3 0 , s e l f . y ] ) i f s e l f . m o us e bu t to n [ 2 ] == 1 : s e l f . shotbsound . play () i f s e l f . y < 3 1 0 : s e l f . y = 3 10 s e l f . b u l l e t s b . a pp end ( [ s e l f . x + 8 5 , s e l f . y ] ) #l i m i t a a na ve d o 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 er i o r s e l f . y = 4 30 i f s e l f . y < 3 1 0 : #l i m i te i n f e r i o r s e l f . y = 3 10 i f s e l f . x > 6 8 0 : #l i mi t e l a t e r a l d i r e it o s e l f . x = 6 80 #mo ve a n a ve i n i m i g a movex = 15 f o r i i n r a ng e ( s e l f . e n e m i e s ) : i f s e l f . eny [ i ] >= 4 3 0 : s e l f . l o s e = Tru e i f s e l f . ri gh t [ i ] == True : s e l f . e nx [ i ] = s e l f . e nx [ i ] − movex i f s e l f . enx [ i ] < 0 : s e l f . e ny [ i ] = s e l f . e ny [ i ] + 2 0 movex += 10 s e l f . r i g h t [ i ] = F a l se s e l f . l e f t [ i ] = T ru e i f s e l f . l e f t [ i ] == T ru e : s e l f . e nx [ i ] = s e l f . e nx [ i ] + mo ve x i f s e l f . enx [ i ] > 7 3 5 : s e l f . e ny [ i ] = s e l f . e ny [ i ] + 2 0 movex += 10 s e l f . l e f t [ i ] = F a l se s e l f . r i g h t [ i ] = T ru e self .draw()
d e f enemy( se l f ) : f o r i i n r a ng e ( s e l f . e n e m i e s ) : #c a r r e g a a imagem d a n av e 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 . en e my s hi p [ 0 ] , ( s e l f . e nx [ i ] , s e l f . e ny [ i ] ) ) i f s e l f . hit a [ i ] == True : s e l f . s c r e e n . b l i t ( s e l f . en e my s hi p [ 1 ] , ( s e l f . e nx [ i ] , s e l f . e ny [ i ] ) ) i f s e l f . w in == F a l s e : i f s e l f . l o s e == F a l s e : f o r j i n r a ng e ( s e l f . e n e m i e s ) : i f s e l f . l i v e s > 0 : #t o ca s om p a ra d i sp a do do i n i mi 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 :
4
i f count%se l f . enshott ime [ j ] == 0: s e l f . e n b u l l e t . a pp en d ( [ s e l f . e nx [ j ] + 4 0 , s e l f . en y [ j ] ] ) s e l f . shotensound . play () n e n b u l l et = s e l f . e n b u l l e t [ : ] #move a s b a l a s d o i n i m i g o f o r i i n r a ng e ( l e n ( s e l f . e n b u l l e t ) ) : se lf . enbullet [ i ] [ 1 ] = se lf . enbullet [ i ][1]+15 i f s e l f . e n b u l l e t [ i ] [ 1 ] > 5 0 0 : #r em ov e a b a l a q ua nd o a t i n g e o f i m d a j a n e l a n e n b u l l e t . r em ov e ( s e l f . e n b u l l e t [ i ] ) e l s e : #d e se n ha a s b a l a s xenbullet = se lf . enbullet [ i ] [ 0 ] yenbullet = se lf . enbullet [ i ] [ 1 ] pyga me . d ra w . 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 +1 20 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 u t i l i z a d o r f o i a t in g id o p yg am e . d ra w . 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 n ha a e x p l o s a o s e l f . e x p l o s i o n . p la y ( ) #t o c a som d a e x p l o s a o s e l f . l i v e s −= 1 #remove vida #c o d i g o p a ra o d e se n ho d e f draw( s e l f ) : #l im pa e p r ee n ch e o e c r a com c o r de f un do i f count%60 == 0: i f s e l f . a ux == 0 : s e l f . t a u x = T ru e i f s e l f . a ux == 2 : s e l f . t a ux = F a l s e i f s e l f . t a u x == F a l s e : s e l f . a ux −= 1 e l s e : s e l f . aux += 1 se l f . scree n . f i l l ( background [ s e l f . aux ]) #c a r r e g a a imagem da n av e d o u l i l i z a d o r s e l f . s c r e e n . b l i t ( s e l f . u s e rs h i p , ( s e l f . x ,
se lf .y) )
nbulletsa = se lf . bulletsa [ : ] nbulletsb = se lf . bulletsb [ : ] #move a s b a l a s a f o r i i n r a ng e ( l e n ( s e l f . b u l l e t s a ) ) : s e l f . b u l l e t s a [ i ] [ 1 ] = s e l f . b u l l e t s a [ i ] [ 1 ] − 1 5 i f s e l f . b u l l e t s a [ i ] [ 1 ] < 0 : #r em ov e a b a la q ua ndo a t i n g e o f im da j a n e l a n b u l l e t s a . r em ov e ( s e l f . b u l l e t s a [ i ] ) e l s e : #d e se n ha a s b a l a s xbullet a = se lf . bullets a [ i ] [ 0 ] ybullet a = se lf . bullets a [ i ] [ 1 ] p yg ame . d ra w . 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( xbullet a , ybulleta , 2 ,5) ) f o r j i n r a ng e ( s e l f . e n e m i e s ) : #v e r i f i c a s e o i ni mi go f o i a t in g id 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) : #desenha a s b a la s p yg am e . d ra w . 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 . e x p l o s i o n . p la y ( ) s e l f . h i t a [ j ] = T ru e i f s e l f . hit a [ j ] == True and s e l f . hit b [ j ] == True : s e l f . h i t a b a [ j ] = T ru e s e l f . enemycount += 1 #a c t u a l i z a o s i n im i g os a b at i do s s e l f . s c o r e += 1 00 0 #a c t u a l i z a a p o nt u ac a o i f s e l f . enemycount == s e l f . enemies :
5
s e l f . w in = T ru e s e l f . s c o r e += 5 #a c t u a l i z a a p o nt u ac a o #move a s b a l a s b f o r i i n r a ng e ( l e n ( s e l f . b u l l e t s b ) ) : s e l f . b u l l e t s b [ i ] [ 1 ] = s e l f . b u l l e t s b [ i ] [ 1 ] − 1 5 i f s e l f . b u l l e t s b [ i ] [ 1 ] < 0 : #r em ov e a b a la q ua ndo a t i n g e o f im da j a n e l a n b u l l e t s b . r em ov e ( s e l f . b u l l e t s b [ i ] ) e l s e : #d e se n ha a s b a l a s xbulletb = se lf . bullets b [ i ][ 0 ] ybulletb = se lf . bullets b [ i ][ 1 ] pyga me . d ra w . 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 ng e ( s e l f . e n e m i e s ) : #v e r i f i c a s e o i ni mi go f o i a t in g id 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) : #desenha a s b a la s p yg am e . d ra w . 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 . e x p l o s i o n . p la y ( ) i f s e l f . hit a [ j ] == True : s e l f . h i t b [ j ] = T ru e s e l f . s c o r e += 5 #a c t u a l i z a a p o nt u ac a o #c a r r e g a a s n av es i n i m i g a s self .enemy() s e l f . s c o r e t xt = ’ S c o r e : ’+s t r ( s e l f . s c o r e ) s e l f . e n em y tx t = ’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 rr e g a o t e xt o n as d ev i da s p o s i c oe s s e l f . t e x t ( p o s co r e , s e l f . s c o r e t x t ) sel f . text ( polives , se lf . liv est xt ) se l f . text (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 == T ru e : 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 or e ’+s t r ( s e l f . s c o r e ) )
game = Game( ) time = 13 count=0 start = False end = False w h i le n ot game. quit : #i n i c i a o j og o quando s e c a rr e ga em e n te r i f not s t a r t : ga me . s c r e e n . b l i t ( game . i n i t i m g , ( 0 , 0 ) ) k e y s = py ga me . ke y . g e t p r e s s e d ( ) i f ke y s [K RETURN ] : start = True f o r e v e n t i n pygame. even t . get () : i f event . type == QUIT: game. qui t = True p yg ame . d i s p l a y . f l i p ( ) i f s t a r t : #t o c a m us ic a n o j o g o d e x e m x t emp o i f count%(2 ∗ 60 ∗ time+52∗ t i m e ) == 0 : # CHANGE HERE TO MATCH YOUR SONG DURATION game . music . pl ay () count += 1
6
#t r a t a d e e v en t o s f o r e v e n t i n pygame. even t . get () : i f event . type == QUIT: game. qui t = True #u pd a te a c ad a i t e r a c a o game. update () #c a so p e rc a o j o g o t o ca e s t a m us ic a i f g ame . l i v e s <= 0 o r game. los e == True : game. music . stop () i f not end : game. musi clo se . play () end = True #c a so g an he o j o g o t o ca e s t a i f game . win == True : game. music . stop () i f not end : game. musicwin . play ( ) end = True time passed = clock . tick (time) #m os tr a o u p da t e d a s c e n a s p yg ame . d i s p l a y . f l i p ( ) #f e ch a a j a n e l a i f game. qu i t==True : pygame . qui t ()
7