Skip to content

Instantly share code, notes, and snippets.

@Shane-Lester
Created May 6, 2019 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shane-Lester/1e0278f52cf3c1c4e376be5d867bd79b to your computer and use it in GitHub Desktop.
Save Shane-Lester/1e0278f52cf3c1c4e376be5d867bd79b to your computer and use it in GitHub Desktop.
SpaceInvaders.py
from scene import *
import levelData as ld
import sound
import random
import math
import time
import ui
A = Action
POSITION=0
TITLE =1
ACTIVE =2
LEVELUP= 3
GAMEOVER = 4
RESET =5
class MyScene (Scene):
def setup(self):
self.width, self.height=self.size
self.state = POSITION
sound.set_honors_silent_switch(False)
self.player = SpriteNode('spc:PlayerShip1Red' ,position=(self.width/2, 50), scale=0.8 )
self.add_child(self.player)
self.score_text=LabelNode('Score: ' + str(0))
self.score_text.position=(50,self.height -30)
self.player.health =3
self.livesText=LabelNode('Lives: '+ str (self.player.health))
self.livesText.position=(310,self.height-30)
self.info_text = LabelNode('Get Ready!!')
self.info_text.position=(200, self.height/2)
self.info_text.alpha=0
self.difficulty = 1
self.add_child(self.info_text)
self.add_child(self.livesText)
self.add_child(self.score_text)
self.setup_player()
self.setup_scene()
def setup_scene(self):
self.background_color='#000000'
self.score=0
self.update_score()
self.dying=[]
self.info_text.alpha=1
self.level = 1
self.state = TITLE
self.setup_starfield()
self.set_up_enemy()
self.touched=False
self.showing_title=False
def setup_player(self):
self.player.rotation =0
self.player.shot_timer=45
self.player.alpha=1
self.lasers=[]
self.player.health =3
self.draw_lives()
self.livesText.alpha = 1
def title_screen(self):
if not self.showing_title:
self.background_color = '#0b568f'
self.player.alpha = 0
self.info_text.text="Shane's \nshooter"
self.info_text.font = ('Copperplate', 50)
self.info_text.alpha=1
seq= [A.move_by (-100,0), A.move_by(200,0),A.move_by(-100,0)]
self.info_text.run_action(A.repeat(A.sequence(seq),-1))
self.livesText.alpha=0
self.score_text.alpha =0
self.touched = False
self.showing_title=True
else:
if self.touched == True:
self.info_text.remove_all_actions()
self.info_text.position= (200,self.height/2)
self.fade_title()
def fade_title(self):
self.background_color='#000000'
self.player.run_action(A.fade_to(1))
self.info_text.text = "Get ready!!"
self.info_text.run_action(A.fade_to(1))
self.score_text.run_action(A.fade_to(1))
self.livesText.run_action(A.fade_to(1))
self.setup_player()
self.state=POSITION
def draw_lives(self):
if self.player.health == 0:
self.livesText.run_action(A.fade_to(0))
return
self.livesText.text=('Lives: '+ str (self.player.health))
def setup_starfield(self):
#three rows at different speeds
self.top_stars = []
self.top_star_speed = -5
x,y=self.width,self.height
positions=[(x/4,y/3),(x*3/4,y/3),(x/3,y*2/3),(x*2/3,y*2/3),(x*3/4,y-5),(x/4,y-5)]
for i,j in positions:
temp= SpriteNode('spc:Star3', position=(i,j))
temp.scale= 0.3
self.top_stars.append(temp)
self.add_child(temp)
#next row
self.other_stars = []
self.other_star_speed = -2
positions=[(x/3,y/3+50),(x*2/3,y/3+50),(x/5,y*2/3+50),(x*4/5,y*2/3+50),(x/3,y-15),(x*2/3,y-15)]
for i,j in positions:
temp= SpriteNode('spc:Star1', position=(i,j))
temp.scale= 0.3
self.other_stars.append(temp)
self.add_child(temp)
def set_up_enemy(self):
self.enemies=[]
self.fire_gap = 20
self.bombs=[]
self.explosions=[]
level=ld.levels[str(self.level)]
for nextEnemy in level:
temp=SpriteNode(nextEnemy['texture'], position=(self.width/2 + nextEnemy['x'],self.height+50))
temp.scale= nextEnemy["scale"]
temp.rotation = nextEnemy["rot"]
temp.enemy_countdown=60
temp.startpos = self.height- self.height/3
temp.dancing=0
temp.health= nextEnemy["health"]
temp.circuit =nextEnemy["circuit"]
temp.Mspeed= 5/float(nextEnemy["speed"]+self.difficulty*2)
temp.fire_rate = nextEnemy["rate"]
self.add_child(temp)
self.enemies.append(temp)
def did_change_size(self):
pass
def player_shoot(self):
self.player.shot_timer -=1
if self.player.shot_timer <=0:
self.player.shot_timer =45
x,y =self.player.position
laser=SpriteNode('spc:LaserGreen12')
laser.position=(x,y+50)
self.lasers.append(laser)
self.add_child(laser)
laser.run_action(A.sequence(A.move_to(x,self.height+100) ,A.remove()))
sound.play_effect('arcade:Laser_1')
def update(self):
if self.state ==TITLE:
self.title_screen()
if self.state == POSITION:
self.update_stars()
self.animate_enemies()
if self.state == ACTIVE:
self.draw_lives()
self.update_stars()
self.player_shoot()
self.animate_enemies()
self.update_score()
self.killing_enemies()
self.player_hit()
elif self.state == LEVELUP:
self.clear_bombs()
self.create_enemies()
self.difficulty +=1
elif self.state == GAMEOVER:
self.draw_lives()
self.clear_bombs()
self.player.run_action(A.fade_to(0))
self.clear_bombs()
for e in self.enemies:
e.remove_all_actions()
e.remove_from_parent()
self.background_color = '#0b568f'
self.info_text.text ="Game Over!"
self.score_text.alpha=0
time.sleep(0.5)
self.info_text.run_action(A.fade_to(1,0.5))
self.state =RESET
self.touched = False
elif self.state == RESET:
self.info_text.text="Press a key!"
time.sleep(0.5)
if self.touched ==True:
for t in self.top_stars:
t.remove_from_parent()
for o in self.other_stars:
o.remove_from_parent()
self.info_text.text=""
self.setup_scene()
def clear_bombs(self):
for b in self.bombs:
b.remove_from_parent()
for b in self.explosions:
b.remove_from_parent()
def killing_enemies(self):
if len(self.dying) >0:
for target in self.dying:
self.blow_up(target)
if len(self.explosions)>0:
for bomb in self.explosions:
self.explode(bomb)
def explode(self,bomb):
bomb.timer-=1
if bomb.timer<=0:
bomb.remove_from_parent()
def update_score(self):
self.score_text.text='Score: ' + str(self.score)
def player_hit(self):
for b in self.bombs:
if b.bbox.intersects(self.player.bbox):
b.texture=Texture('shp:aura')
self.bombs.remove(b)
b.remove_all_actions()
self.explosions.append(b)
self.player_damage()
def player_damage(self):
self.player.health-=1
sound.play_effect('arcade:Explosion_3')
if self.player.health <=0:
self.state = GAMEOVER
sound.play_effect('arcade:Powerup_3')
def create_enemies(self):
self.info_text.text ='Level Up!'
self.info_text.run_action(A.fade_to(1))
self.state = POSITION
self.level +=1
if self.level > len(ld.levels):
self.level = 1
self.set_up_enemy()
def animate_enemies(self):
for enemy in self.enemies:
if enemy.enemy_countdown > 0:
enemy.enemy_countdown -=1
else:
self.move_enemy(enemy)
self.enemy_hit(enemy)
def enemy_hit(self,enemy):
for l in self.lasers:
if l.position.y > self.height:
self.lasers.remove(l)
l.remove_from_parent()
else:
if l.bbox.intersects(enemy.bbox):
sound.play_effect('arcade:Hit_1')
self.score+=10
self.lasers.remove(l)
l.remove_from_parent()
enemy.health -=1
if enemy.health <=0:
self.enemies.remove(enemy)
self.dying.append(enemy)
enemy.run_action(A.fade_to(0))
enemy.dying=21
enemy.texture=Texture('spc:PlayerShip2Damage3')
return
def blow_up(self,target):
target.dying-=1
if target.dying <= 21 and target.dying >= 14:
target.texture=Texture('spc:PlayerShip2Damage3')
return
elif target.dying <= 13and target.dying >= 7:
target.texture=Texture('spc:PlayerShip2Damage2')
return
elif target.dying <= 6 and target.dying >= 1:
target.texture=Texture('spc:PlayerShip2Damage1')
return
elif target.dying <=0:
sound.play_effect('arcade:Explosion_2')
self.dying.remove(target)
target.remove_from_parent()
if len(self.enemies) ==0:
self.state= LEVELUP
return
else:
print('error')
def move_enemy(self,enemy):
x,y=enemy.position
if enemy.dancing == 0:
y-=1
if y<= enemy.startpos:
y=enemy.startpos
enemy.dancing=1
self.state=ACTIVE
enemy.position=(x,y)
elif enemy.dancing == 1:
#create action
enemy.dancing=2
self.enemy_circuit(enemy,x,y)
self.info_text.run_action(A.fade_to(0))
elif enemy.dancing ==2:
if random.randint(0,10) <= enemy.fire_rate:
self.enemy_fire(enemy)
def enemy_fire(self,enemy):
self.fire_gap -=1
if self.fire_gap <= 0:
x,y = enemy.position
temp = SpriteNode('spc:LaserBlue7')
temp.position =(x, y-25)
temp.timer=30
self.fire_gap = 45
self.bombs.append(temp)
self.add_child(temp)
temp.run_action(A.move_to(x,-50,2))
def enemy_circuit(self,enemy,x,y):
s=float(enemy.Mspeed)
act=[]
for dx,dy in enemy.circuit:
x+=dx
y+=dy
act.append(A.move_to(x,y,s))
enemy.run_action(A.repeat(A.sequence(act),-1))
def update_stars(self):
for star in self.top_stars:
self.move_star(star,self.top_star_speed)
for str in self.other_stars:
self.move_star(str,self.other_star_speed)
def move_star(self,str,speed):
x,y =str.position
y+= speed
if y<= 0:
y = self.height + random.randint(-20,20)
x+= random.randint(-20,20)
x=max(0,min(x,self.width))
str.position =(x, y)
def touch_began(self, touch):
if self.state==RESET or self.state == TITLE:
self.touched =True
return
x,y=touch.location
if (x>=self.width /2):
A.remove()
if self.player.position.x >= self.width-60:
pass
else:
self.player.rotation=0
seq=A.sequence(A.rotate_by(-0.1,0.1),A.scale_x_to(0.5,0.1),A.move_by(30,0,0.2),A.rotate_to(0,0.1),A.scale_x_to(0.8,0.1))
self.player.run_action(seq)
else:
if self.player.position.x <= 60:
pass
else:
seq=A.sequence(A.rotate_by(0.1,0.1),A.scale_x_to(0.5,0.1),A.move_by(-30,0,0.2),A.rotate_to(0,0.1),A.scale_x_to(0.8,0.1))
self.player.run_action(seq)
def touch_moved(self, touch):
pass
def touch_ended(self, touch):
pass
if __name__ == '__main__':
run(MyScene(), show_fps=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment