def __init__(self): self.score = games.Text(value=0, size=30, top=5, color=color.white, right=games.screen.width - 10, is_collideable=False) games.screen.add(self.score) self.startup = games.load_sound("Sounds/FX/startup.wav") self.startup.play() self.sound = games.load_sound("Sounds/FX/level.wav") self.theme = games.music.load("Sounds/Theme/far.wav") self.level = 0 self.birth_steve()
def main(): #load data bg_image = games.load_image("images/background.jpg",transparent = False) explosion_files = ["images/explosion1.bmp", "images/explosion2.bmp", "images/explosion3.bmp", "images/explosion4.bmp", "images/explosion5.bmp"] missile_sound = games.load_sound("sounds/missile.wav") games.music.load("sounds/gamenoise.mp4") #create objects the_ship = Ship() explosion = games.Animation(images = explosion_files, x = games.screen.width/2, y = games.screen.height/2, n_repeats = 0, repeat_interval = 4) #draw games.screen.background = bg_image games.screen.add(the_ship) games.screen.add(explosion) #game setup #start loop games.screen.mainloop()
class Ship(games.Sprite): image = games.load_image("images/spaceship.png") sound = games.load_sound("sounds/thrusters.wav") ROTATION_STEP = 7 VELOCITY_STEP = .03 MISSILE_DELAY = 25 def __init__(self): super(Ship, self).__init__(image=Ship.image, x=games.screen.width / 2, y=games.screen.height / 2) self.missile_wait = 0 def update(self): if games.keyboard.is_pressed(games.K_a) or games.keyboard.is_pressed( games.K_LEFT): self.angle -= Ship.ROTATION_STEP if games.keyboard.is_pressed(games.K_d) or games.keyboard.is_pressed( games.K_RIGHT): self.angle += Ship.ROTATION_STEP if games.keyboard.is_pressed(games.K_w) or games.keyboard.is_pressed( games.K_UP): Ship.sound.play() angle = self.angle * math.pi / 180 self.dx += Ship.VELOCITY_STEP * math.sin(angle) self.dy += Ship.VELOCITY_STEP * -math.cos(angle) if self.missile_wait > 0: self.missile_wait -= 1 if games.keyboard.is_pressed(games.K_SPACE) and self.missile_wait == 0: new_missile = Missile(self.x, self.y, self.angle) games.screen.add(new_missile) self.missile_wait = Ship.MISSILE_DELAY # This is copied code look into to better ways of doing it #Wrap the ship around the screen if self.left > games.screen.width: self.right = 0 if self.right < 0: self.left = games.screen.width if self.top > games.screen.height: self.bottom = 0 if self.bottom < 0: self.top = games.screen.height #check if missile overlaps any other object if self.overlapping_sprites: for sprite in self.overlapping_sprites: sprite.die() self.die() def die(self): self.destroy()
class Steve(games.Sprite): image = games.load_image("Images/steve.png") sound = games.load_sound("Sounds/FX/thruster2.wav") ROTATION_STEP = 8 VELOCITY_STEP = .05 def update(self): if games.keyboard.is_pressed(games.K_UP) or games.keyboard.is_pressed( games.K_w): Steve.sound.play() angle = self.angle * math.pi / 180 self.dx += Steve.VELOCITY_STEP * math.sin(angle) self.dy += Steve.VELOCITY_STEP * -math.cos(angle) if games.keyboard.is_pressed( games.K_LEFT) or games.keyboard.is_pressed(games.K_a): self.angle -= Steve.ROTATION_STEP if games.keyboard.is_pressed( games.K_RIGHT) or games.keyboard.is_pressed(games.K_d): self.angle += Steve.ROTATION_STEP if self.top > games.screen.height: self.bottom = 0 if self.left > games.screen.width: self.right = 0 if self.right < 0: self.left = games.screen.width if self.bottom < 0: self.bottom = games.screen.height
def __init__(self, num, game): games.Sprite.__init__( self, load_scaled_image(resource_path('res/ship' + str(num) + '.png')), x=games.screen.width - 200 if num == 1 else 200, y=games.screen.height / 2, angle=0 if num == 1 else 180) self.convert = lambda t, r: (-math.cos(math.pi * t / 180) * r, -math. sin(math.pi * t / 180) * r) self.num = num self.v = self.a = 0 self.health = 100 self.keys = [] self.game = game if not MISSILE_IMAGE: global MISSILE_SOUND MISSILE_SOUND = games.load_sound("missile.wav") load_images() self.health_bar = Health(self) games.screen.add(self.health_bar) games.screen.add(self.health_bar.outline) games.screen.add(self.health_bar.bg) self.frozen = False
class Ship(games.Sprite): image = games.load_image("images/ship.png") sound = games.load_sound("Music/thruster.wav") ROTATION_STEP = 3 VELOCITY_STEP = .13 MISSILE_DELAY = 20 def __init__(self): super(Ship,self).__init__(image = Ship.image, x=games.screen.width / 2, y=games.screen.height / 2) self.missile_wait = 0 def update(self): if games.keyboard.is_pressed(games.K_LEFT) or games.keyboard.is_pressed(games.K_a): self.angle -= Ship.ROTATION_STEP if games.keyboard.is_pressed(games.K_RIGHT) or games.keyboard.is_pressed(games.K_d): self.angle += Ship.ROTATION_STEP if games.keyboard.is_pressed(games.K_w) or games.keyboard.is_pressed(games.K_UP): Ship.sound.play() angle = self.angle * math.pi/180 self.dx += Ship.VELOCITY_STEP * math.sin(angle) self.dy += Ship.VELOCITY_STEP * -math.cos(angle) if self.missile_wait > 0: self.missile_wait -= 1 if games.keyboard.is_pressed(games.K_SPACE) and self.missile_wait ==0: new_missile = Missile(self.x, self.y, self.angle) games.screen.add(new_missile) self.missile_wait = Ship.MISSILE_DELAY print("bang pop pow") if self.left > games.screen.width: self.right = 0 if self.right < 0: self.left = games.screen.width if self.bottom < 0: self.top = games.screen.height if self.top > games.screen.height: self.bottom = 0 if self.overlapping_sprites: for sprite in self.overlapping_sprites: sprite.die() self.die() def die(self): self.destroy()
class Snowball(games.Sprite): image = games.load_image("Images/snowball.png") sound = games.load_sound("Sounds/FX/shoot.wav") BUFFER = 50 VELOCITY_FACTOR = 10 LIFETIME = 40 def __init__(self, steve_x, steve_y, steve_angle): Snowball.sound.play() angle = steve_angle * math.pi / 180 buffer_x = Snowball.BUFFER * math.sin(angle) buffer_y = Snowball.BUFFER * -math.cos(angle) x = steve_x + buffer_x y = steve_y + buffer_y dx = Snowball.VELOCITY_FACTOR * math.sin(angle) dy = Snowball.VELOCITY_FACTOR * -math.cos(angle) super(Snowball, self).__init__(image=Snowball.image, x=x, y=y, dx=dx, dy=dy) self.lifetime = Snowball.LIFETIME def update(self): self.lifetime -= 1 if self.lifetime == 0: self.destroy() if self.top > games.screen.height: self.bottom = 0 if self.left > games.screen.width: self.right = 0 if self.right < 0: self.left = games.screen.width if self.bottom < 0: self.top = games.screen.height
class Ship(games.Sprite): image = games.load_image("img/ship.png") sound = games.load_sound("sounds/thrusters.ogg") ROTATION_STEP = 7 VELOCITY_STEP = .03 def __init__(self): super(Ship, self).__init__(image=Ship.image, x=games.screen.width / 2, y=games.screen.height / 2) def update(self): if games.keyboard.is_pressed( games.K_LEFT) or games.keyboard.is_pressed(games.K_a): self.angle -= Ship.ROTATION_STEP if games.keyboard.is_pressed( games.K_RIGHT) or games.keyboard.is_pressed(games.K_d): self.angle += Ship.ROTATION_STEP if games.keyboard.is_pressed(games.K_UP) or games.keyboard.is_pressed( games.K_w): Ship.sound.play() angle = self.angle * math.pi / 180 self.dx += Ship.VELOCITY_STEP * math.sin(angle) self.dy += Ship.VELOCITY_STEP * -math.cos(angle) # this is copied code, look into better ways of doing this. if self.right < 0: self.left = games.screen.width if self.left < 0: self.right = games.screen.width if self.top > games.screen.height: self.bottom = 0 if self.bottom < 0: self.top = games.screen.height
class Missile(Collider): image = games.load_image( "C:/Users/tyson.vorwaller/Desktop/Asteroids Game/Images/Fortnite_Boogie_Bomb.png" ) sound = games.load_sound( "C:/Users/tyson.vorwaller/Downloads/Quack Quack-SoundBible.com-620056916.wav" ) BUFFER = 60 VELOCITY_FACTOR = 7 LIFETIME = 40 def __init__(self, ship_x, ship_y, ship_angle): Missile.sound.play() angle = ship_angle * math.pi / 180 # calculate missle's starting point buffer_x = Missile.BUFFER * math.sin(angle) buffer_y = Missile.BUFFER * -math.cos(angle) x = ship_x + buffer_x y = ship_y + buffer_y dx = Missile.VELOCITY_FACTOR * math.sin(angle) dy = Missile.VELOCITY_FACTOR * -math.cos(angle) super(Missile, self).__init__(image=Missile.image, x=x, y=y, dx=dx, dy=dy) self.lifetime = Missile.LIFETIME def update(self): super(Missile, self).update() self.lifetime -= 1 if self.lifetime == 0: self.destroy()
class Missile(Collider): image = games.load_image("sprites/projectile.png") sound = games.load_sound("sounds/soundfx/laser.wav") BUFFER = 200 VELOCITY_FACTOR = 7 LIFETIME = 40 def __init__(self, ship_x, ship_y, ship_angle): Missile.sound.play() angle = ship_angle * math.pi / 180 buffer_x = Missile.BUFFER * math.sin(angle) buffer_y = Missile.BUFFER * math.sin(angle) x = ship_x + buffer_x y = ship_y + buffer_y dx = Missile.VELOCITY_FACTOR * math.sin(angle) dy = Missile.VELOCITY_FACTOR * -math.cos(angle) super(Missile, self).__init__(image=Missile.image, x=x, y=y, dx=dx, dy=dy) self.lifetime = Missile.LIFETIME def update(self): super(Missile, self).update() self.lifetime -= 1 if self.lifetime == 0: self.destroy()
def __init__(self): # set level self.level = 0 # load sound for level advance self.sound = games.load_sound("sounds/mvm_used_powerup.ogg") # create score self.score = games.Text(value=0, size=30, color=color.white, top=5, right=games.screen.width - 10, is_collideable=False) games.screen.add(self.score) self.create_ship() start_message1 = games.Message( value="Move your Ship with the Arrow Keys.", size=36, color=color.red, x=games.screen.width / 2, y=games.screen.height / 2, lifetime=3 * games.screen.fps, is_collideable=False) start_message2 = games.Message( value="Press the Space Bar to fire a laser.", size=36, color=color.red, x=games.screen.width / 2, y=games.screen.height * 3 / 4, lifetime=3 * games.screen.fps, is_collideable=False) games.screen.add(start_message1) games.screen.add(start_message2)
class Ship(games.Sprite): image = games.load_image("sprites/spaceship2.png") sound = games.load_sound("sounds/soundfx/thrust.wav") ROTATION_STEP = 6 VELOCITY_STEP = .03 MISSILE_DELAY = 20 def __init__(self, x, y): super(Ship, self).__init__(image=Ship.image, x=x, y=y) self.missile_wait = 0 def update(self): #Missile counter if self.missile_wait > 0: self.missile_wait -= 1 #key setup if games.keyboard.is_pressed(games.K_a) or games.keyboard.is_pressed( games.K_LEFT): self.angle -= Ship.ROTATION_STEP if games.keyboard.is_pressed(games.K_d) or games.keyboard.is_pressed( games.K_RIGHT): self.angle += Ship.ROTATION_STEP if games.keyboard.is_pressed(games.K_t): Ship.sound.play() #apply thrust if games.keyboard.is_pressed(games.K_w) or games.keyboard.is_pressed( games.K_UP): angle = self.angle * math.pi / 180 self.dx += Ship.VELOCITY_STEP * math.sin(angle) self.dy += Ship.VELOCITY_STEP * -math.cos(angle) #Wrapping screen if self.left > games.screen.width: self.right = 0 if self.right < 0: self.left = games.screen.width if self.top > games.screen.height: self.bottom = 0 if self.bottom < 0: self.top = games.screen.height #fire missile if games.keyboard.is_pressed(games.K_SPACE) and self.missile_wait <= 0: Missile.sound.play() new_missile = Missile(self.x, self.y, self.angle) games.screen.add(new_missile) self.missile_wait = Ship.MISSILE_DELAY #Check if overlapping if self.overlapping_sprites: for sprite in self.overlapping_sprites: sprite.die() self.die()
class Steve(Collider): image = games.load_image("Images/steve.png") sound = games.load_sound("Sounds/FX/thruster.wav") ROTATION_STEP = 8 VELOCITY_STEP = .04 SNOWBALL_DELAY = 30 def __init__(self, x, y): super(Steve, self).__init__(image=Steve.image, x=x, y=y) self.snowball_wait = 0 def update(self): super(Steve, self).update() if self.snowball_wait > 0: self.snowball_wait -= 1 if games.keyboard.is_pressed(games.K_UP) or games.keyboard.is_pressed( games.K_w): Steve.sound.play() angle = self.angle * math.pi / 180 self.dx += Steve.VELOCITY_STEP * math.sin(angle) self.dy += Steve.VELOCITY_STEP * -math.cos(angle) if games.keyboard.is_pressed( games.K_LEFT) or games.keyboard.is_pressed(games.K_a): self.angle -= Steve.ROTATION_STEP if games.keyboard.is_pressed( games.K_RIGHT) or games.keyboard.is_pressed(games.K_d): self.angle += Steve.ROTATION_STEP if games.keyboard.is_pressed( games.K_SPACE) and self.snowball_wait <= 0: new_snowball = Snowball(self.x, self.y, self.angle) games.screen.add(new_snowball) self.snowball_wait = Steve.SNOWBALL_DELAY
class Missile(games.Sprite): image = games.load_image("missile.png") sound = games.load_sound("explosion.wav") BUFFER = 60 LIFE = 40 VELOCITY = 10 def __init__(self, tank_x, tank_y, tank_angle): missile_angle = tank_angle * math.pi / 180 buffer_x = Missile.BUFFER * math.cos(missile_angle) buffer_y = Missile.BUFFER * math.sin(missile_angle) x = tank_x + buffer_x y = tank_y + buffer_y dx = Missile.VELOCITY * math.cos(missile_angle) dy = Missile.VELOCITY * math.sin(missile_angle) super(Missile, self).__init__(image=Missile.image, x=x, y=y, dx=dx, dy=dy) def update(self): for sprite in self.overlapping_sprites: sprite.die() self.destroy() explosion = Explosion(self.x, self.y) games.screen.add(explosion) Missile.sound.play() def die(self): self.destroy()
class Snowball(Collider): image = games.load_image("Images/snowball.png") sound = games.load_sound("Sounds/FX/shoot.wav") BUFFER = 55 VELOCITY_FACTOR = 10 LIFETIME = 40 def __init__(self, steve_x, steve_y, steve_angle): Snowball.sound.play() angle = steve_angle * math.pi / 180 buffer_x = Snowball.BUFFER * math.sin(angle) buffer_y = Snowball.BUFFER * -math.cos(angle) x = steve_x + buffer_x y = steve_y + buffer_y dx = Snowball.VELOCITY_FACTOR * math.sin(angle) dy = Snowball.VELOCITY_FACTOR * -math.cos(angle) super(Snowball, self).__init__(image=Snowball.image, x=x, y=y, dx=dx, dy=dy) self.lifetime = Snowball.LIFETIME def update(self): super(Snowball, self).update() self.lifetime -= 1 if self.lifetime == 0: self.destroy()
class Missile(Collider): image = games.load_image("images2/laser.png") sound = games.load_sound("sounds/laser.wav") buffer = 45 velocity_factor = 7 lifetime = 40 def __init__(self, ship_x, ship_y, ship_angle): Missile.sound.play() angle = ship_angle * math.pi / 180 #calculate missile's starting position buffer_x = Missile.buffer * math.sin(angle) buffer_y = Missile.buffer * -math.cos(angle) x = ship_x + buffer_x y = ship_y + buffer_y dx = Missile.velocity_factor * math.sin(angle) dy = Missile.velocity_factor * -math.cos(angle) super(Missile, self).__init__(image=Missile.image, x=x, y=y, dx=dx, dy=dy) self.lifetime = Missile.lifetime self.angle = ship_angle def update(self): super(Missile, self).update() self.lifetime -= 1 if self.lifetime == 0: self.destroy()
class Missile(games.Sprite): """ A missile launched by the player's ship. """ image = games.load_image("missile.bmp") sound = games.load_sound("missile.wav") BUFFER = 40 VELOCITY_FACTOR = 7 LIFETIME = 40 def __init__(self, ship_x, ship_y, ship_angle): """ Initialize missile sprite. """ Missile.sound.play() # convert to radians angle = ship_angle * math.pi / 180 # calculate missile's starting position buffer_x = Missile.BUFFER * math.sin(angle) buffer_y = Missile.BUFFER * -math.cos(angle) x = ship_x + buffer_x y = ship_y + buffer_y # calculate missile's velocity components dx = Missile.VELOCITY_FACTOR * math.sin(angle) dy = Missile.VELOCITY_FACTOR * -math.cos(angle) # create the missile super(Missile, self).__init__(image = Missile.image, x = x, y = y, dx = dx, dy = dy) self.lifetime = Missile.LIFETIME def update(self): """ Move the missile. """ # if lifetime is up, destroy the missile self.lifetime -= 1 if self.lifetime == 0: self.destroy() # wrap the missile around screen if self.top > games.screen.height: self.bottom = 0 if self.bottom < 0: self.top = games.screen.height if self.left > games.screen.width: self.right = 0 if self.right < 0: self.left = games.screen.width # check if missile overlaps any other object if self.overlapping_sprites: for sprite in self.overlapping_sprites: sprite.die() self.die() def die(self): """ Destroy the missile. """ self.destroy()
class Ship(game.Sprite): ship_image = game.load_image("img/Spaceship.png") ROTATION_STEP = 7 VELOCITY_STEP = .03 thruster_sound = game.load_sound("snd/thrusters.wav") thruster_sound.set_volume(.5) def __init__(self): super(Ship, self).__init__(image=Ship.ship_image, x=game.screen.width / 2, y=game.screen.height / 2) def update(self): if game.keyboard.is_pressed(game.K_a) or game.keyboard.is_pressed( game.K_LEFT): self.angle -= Ship.ROTATION_STEP if game.keyboard.is_pressed(game.K_d) or game.keyboard.is_pressed( game.K_RIGHT): self.angle += Ship.ROTATION_STEP if game.keyboard.is_pressed(game.K_w) or game.keyboard.is_pressed( game.K_UP): Ship.thruster_sound.play() angle = self.angle * math.pi / 180 self.dx += Ship.VELOCITY_STEP * math.sin(angle) self.dy += Ship.VELOCITY_STEP * -math.cos(angle) # this is copied code look in better ways of doing it if self.left > game.screen.width: self.right = 0 if self.right < 0: self.left = game.screen.width if self.top > game.screen.height: self.bottom = 0 if self.bottom < 0: self.top = game.screen.height
class Ship(Collider): ship_image = games.load_image("images2/ship3.png") sound = games.load_sound("sounds/thruster.wav") rotation_step = 7 velocity_step = .03 missile_delay = 25 max_velocity = 3 lives = 3 def __init__(self, game, x, y): super(Ship, self).__init__(image=Ship.ship_image, x=x, y=y) self.game = game self.missile_wait = 0 self.lives = games.Text(value="Lives: " + str(Ship.lives), size=25, color=color.white, top=25, right=games.screen.width - 10, is_collideable=False) games.screen.add(self.lives) def update(self): super(Ship, self).update() if games.keyboard.is_pressed( games.K_LEFT) or games.keyboard.is_pressed(games.K_a): self.angle -= Ship.rotation_step if games.keyboard.is_pressed( games.K_RIGHT) or games.keyboard.is_pressed(games.K_d): self.angle += Ship.rotation_step if games.keyboard.is_pressed(games.K_UP) or games.keyboard.is_pressed( games.K_w): Ship.sound.play() angle = self.angle * math.pi / 180 self.dx += Ship.velocity_step * math.sin(angle) self.dy += Ship.velocity_step * -math.cos(angle) self.dx = min(max(self.dx, -Ship.max_velocity), Ship.max_velocity) self.dy = min(max(self.dy, -Ship.max_velocity), Ship.max_velocity) if self.missile_wait > 0: self.missile_wait -= 1 if games.keyboard.is_pressed(games.K_SPACE) and self.missile_wait == 0: new_missile = Missile(self.x, self.y, self.angle) games.screen.add(new_missile) self.missile_wait = Ship.missile_delay def lose_life(self): Ship.lives -= 1 self.lives.destroy() if Ship.lives <= 0: self.game.end() else: self.game.new_ship() def die(self): """Destroy the ship and end the game""" super(Ship, self).die() self.lose_life()
class Missile(Collider): image = games.load_image("images/missile.png", transparent=True) sound = games.load_sound("Music/missile.wav") BUFFER = 60 VELOCITY_FACTOR = 9 LIFETIME = 80 def __init__(self, ship_x, ship_y, ship_angle): Missile.sound.play() angle = ship_angle * math.pi / 180 # calculate missle's starting position buffer_x = Missile.BUFFER * math.sin(angle) buffer_y = Missile.BUFFER * -math.cos(angle) x = ship_x + buffer_x y = ship_y + buffer_y dx = Missile.VELOCITY_FACTOR * math.sin(angle) dy = Missile.VELOCITY_FACTOR * -math.cos(angle) super(Missile, self).__init__(image=Missile.image, x=x, y=y, dx=dx, dy=dy) self.lifetime = Missile.LIFETIME def update(self): super(Missile, self).update() self.lifetime -= 1 if self.lifetime == 0: self.destroy()
class Ship(Collider): image = games.load_image("img/ship.png") sound = games.load_sound("sounds/thrusters.ogg") ROTATION_STEP = 7 VELOCITY_STEP = .03 MISSILE_DELAY = 25 def __init__(self): super(Ship, self).__init__(image=Ship.image, x=games.screen.width / 2, y=games.screen.height / 2) self.missile_wait = 0 def update(self): super(Ship, self).update() if games.keyboard.is_pressed( games.K_LEFT) or games.keyboard.is_pressed(games.K_a): self.angle -= Ship.ROTATION_STEP if games.keyboard.is_pressed( games.K_RIGHT) or games.keyboard.is_pressed(games.K_d): self.angle += Ship.ROTATION_STEP if games.keyboard.is_pressed(games.K_UP) or games.keyboard.is_pressed( games.K_w): Ship.sound.play() angle = self.angle * math.pi / 180 self.dx += Ship.VELOCITY_STEP * math.sin(angle) self.dy += Ship.VELOCITY_STEP * -math.cos(angle) if self.missile_wait > 0: self.missile_wait -= 1 if games.keyboard.is_pressed(games.K_SPACE) and self.missile_wait == 0: new_missile = Missile(self.x, self.y, self.angle) games.screen.add(new_missile) self.missile_wait = Ship.MISSILE_DELAY
class Laser(Collider): laser_image = game.load_image("img/laser.png") laser_sound = game.load_sound("snd/laser_shoot.wav") laser_sound.set_volume(.3) BUFFER = 60 VELOCITY_FACTOR = 7 LIFETIME = 40 def __init__(self, ship_x, ship_y, ship_angle): Laser.laser_sound.play() angle = ship_angle * math.pi / 180 # calculate missle's starting position buffer_x = Laser.BUFFER * math.sin(angle) buffer_y = Laser.BUFFER * -math.cos(angle) x = ship_x + buffer_x y = ship_y + buffer_y dx = Laser.VELOCITY_FACTOR * math.sin(angle) dy = Laser.VELOCITY_FACTOR * -math.cos(angle) super(Laser, self).__init__(image=Laser.laser_image, x=x, y=y, dx=dx, dy=dy) self.lifetime = Laser.LIFETIME self.angle = ship_angle def update(self): super(Laser, self).update() self.lifetime -= 1 if self.lifetime == 0: self.destroy()
class Missile(Collider): image = games.load_image("pocisk.bmp") sound = games.load_sound("pocisk.wav") BUFFER = 40 LIFETIME = 100 VELOCITY = 3 def __init__(self, ship_x, ship_y): Missile.sound.play() buffer_y = Missile.BUFFER x = ship_x y = ship_y - buffer_y dx = 0 dy = -Missile.VELOCITY super(Missile, self).__init__(image=Missile.image, x=x, y=y, dx=dx, dy=dy) self.lifetime = Missile.LIFETIME def update(self): super(Missile, self).update() self.lifetime -= 1 if self.lifetime == 0: self.destroy()
class Ship(games.Sprite): ship_image = games.load_image("images2/ship3.png") sound = games.load_sound("sounds/thruster.wav") rotation_step = 7 velocity_step = .03 def __init__(self): super(Ship, self).__init__(image=Ship.ship_image, x=games.screen.width / 2, y=games.screen.height / 2) def update(self): if games.keyboard.is_pressed( games.K_LEFT) or games.keyboard.is_pressed(games.K_a): self.angle -= Ship.rotation_step if games.keyboard.is_pressed( games.K_RIGHT) or games.keyboard.is_pressed(games.K_d): self.angle += Ship.rotation_step if games.keyboard.is_pressed(games.K_UP) or games.keyboard.is_pressed( games.K_w): Ship.sound.play() angle = self.angle * math.pi / 180 self.dx += Ship.velocity_step * math.sin(angle) self.dy += Ship.velocity_step * -math.cos(angle) ## #this is copied code to look into better ways of doing it if self.left > games.screen.width: self.right = 0 if self.right < 0: self.left = games.screen.width if self.top > games.screen.height: self.bottom = 0 if self.bottom < 0: self.top = games.screen.height
class Missile(Colider): image = games.load_image("reszta/pocisk.bmp") sound = games.load_sound("reszta/pocisk.wav") BUFFER = 40 VELOCITY = 7 LIFETIME = 40 def __init__(self,x,y, angle): Missile.sound.play() angle = angle *math.pi/180 buffer_x =Missile.BUFFER * math.sin(angle) buffer_y =Missile.BUFFER * -math.cos(angle) x= x+buffer_x y= y + buffer_y dx = Missile.VELOCITY * math.sin(angle) dy = Missile.VELOCITY * -math.cos(angle) super(Missile, self).__init__(image=Missile.image, x = x, y=y, dx=dx, dy=dy) self.lifetime = Missile.LIFETIME def update(self): super(Missile, self).update() self.lifetime-=1 if self.lifetime == 0: self.destroy()
class GunBonus(games.Animation): """A model of a gun bonus animation""" gun_bonus_sound = games.load_sound('./res/level.wav') images = ['./res/star coin rotate 1.png', './res/star coin rotate 2.png', './res/star coin rotate 3.png', './res/star coin rotate 4.png', './res/star coin rotate 5.png', './res/star coin rotate 6.png'] def __init__(self, x, y): """Initializes gun bonus animation""" GunBonus.gun_bonus_sound.play() super().__init__(images=GunBonus.images, x=x, y=y, repeat_interval=3, n_repeats=0, is_collideable=True) self.name = 'gun_bonus' self.lifetime = 7 * games.screen.fps def update(self): if self.lifetime > 0: self.lifetime -= 1 else: self.die() def die(self): """Destroys gun bonus""" GunBonus.gun_bonus_sound.play() self.destroy()
class Missile(Collider): image = games.load_image("img/lasere.png") sound = games.load_sound("sounds/laser.ogg") BUFFER = 40 VELOCITY_FACTOR = 7 LIFETIME = 40 def __init__(self, ship_x, ship_y, ship_angle): Missile.sound.play() angle = ship_angle * math.pi / 180 # calculate missile's starting position buffer_x = Missile.BUFFER * math.sin(angle) buffer_y = Missile.BUFFER * -math.cos(angle) x = ship_x + buffer_x y = ship_y + buffer_y dx = Missile.VELOCITY_FACTOR * math.sin(angle) dy = Missile.VELOCITY_FACTOR * -math.cos(angle) super(Missile, self).__init__(image=Missile.image, x=x, y=y, dx=dx, dy=dy) self.lifetime = Missile.LIFETIME def update(self): super(Missile, self).update() # if lifetime is up, destroy the missile self.lifetime -= 1 if self.lifetime == 0: self.destroy()
def main(): #load Data nebula_image = games.load_image("img/space.png", transparent=False) explosion_files = ["img/explosion1.bmp", "img/explosion2.bmp", "img/explosion3.bmp", "img/explosion4.bmp", "img/explosion5.bmp", "img/explosion6.bmp", "img/explosion7.bmp", "img/explosion8.bmp", "img/explosion9.bmp"] missile_sound = games.load_sound("sounds/laser.ogg") games.music.load("sounds/Erebus.ogg") #create objects the_ship = Ship() explosion = games.Animation(images=explosion_files, x=games.screen.width/2, y=games.screen.height/2, n_repeats=5, repeat_interval=5) #draw games.screen.background = nebula_image games.screen.add(the_ship) games.screen.add(explosion) #game setup #start loop games.screen.mainloop()
class Ship(Collider): image = games.load_image("img/ship.png") sound = games.load_sound("sounds/thrusters.ogg") ROTATION_STEP = 7 VELOCITY_STEP = .03 MISSILE_DELAY = 25 VELOCITY_MAX = 3 LIVES = 3 def __init__(self, game, x, y): super(Ship, self).__init__(image=Ship.image, x=x, y=y) self.game = game self.missile_wait = 0 self.lives = games.Text(value="Lives: " + str(Ship.LIVES), size=25, color=color.white, top=25, right=games.screen.width - 10, is_collideable=False) games.screen.add(self.lives) def update(self): super(Ship, self).update() if games.keyboard.is_pressed( games.K_LEFT) or games.keyboard.is_pressed(games.K_a): self.angle -= Ship.ROTATION_STEP if games.keyboard.is_pressed( games.K_RIGHT) or games.keyboard.is_pressed(games.K_d): self.angle += Ship.ROTATION_STEP if games.keyboard.is_pressed(games.K_UP) or games.keyboard.is_pressed( games.K_w): Ship.sound.play() angle = self.angle * math.pi / 180 self.dx += Ship.VELOCITY_STEP * math.sin(angle) self.dy += Ship.VELOCITY_STEP * -math.cos(angle) self.dx = min(max(self.dx, -Ship.VELOCITY_MAX), Ship.VELOCITY_MAX) self.dy = min(max(self.dy, -Ship.VELOCITY_MAX), Ship.VELOCITY_MAX) if self.missile_wait > 0: self.missile_wait -= 1 if games.keyboard.is_pressed(games.K_SPACE) and self.missile_wait == 0: new_missile = Missile(self.x, self.y, self.angle) games.screen.add(new_missile) self.missile_wait = Ship.MISSILE_DELAY def loselife(self): Ship.LIVES -= 1 self.lives.destroy() if Ship.LIVES <= 0: print("Hello!") self.game.end() else: self.game.create_ship() def die(self): super(Ship, self).die() self.loselife()
class Ship(games.Sprite): ship_image = games.load_image("images2/ship3.png") sound = games.load_sound("sounds/thruster.wav") rotation_step = 7 velocity_step = .03 missile_delay = 25 def __init__(self): super(Ship, self).__init__(image=Ship.ship_image, x=games.screen.width / 2, y=games.screen.height / 2) self.missile_wait = 0 def update(self): if games.keyboard.is_pressed( games.K_LEFT) or games.keyboard.is_pressed(games.K_a): self.angle -= Ship.rotation_step if games.keyboard.is_pressed( games.K_RIGHT) or games.keyboard.is_pressed(games.K_d): self.angle += Ship.rotation_step if games.keyboard.is_pressed(games.K_UP) or games.keyboard.is_pressed( games.K_w): Ship.sound.play() angle = self.angle * math.pi / 180 self.dx += Ship.velocity_step * math.sin(angle) self.dy += Ship.velocity_step * -math.cos(angle) if self.missile_wait > 0: self.missile_wait -= 1 if games.keyboard.is_pressed(games.K_SPACE) and self.missile_wait == 0: new_missile = Missile(self.x, self.y, self.angle) games.screen.add(new_missile) self.missile_wait = Ship.missile_delay ## #this is copied code to look into better ways of doing it if self.left > games.screen.width: self.right = 0 if self.right < 0: self.left = games.screen.width if self.top > games.screen.height: self.bottom = 0 if self.bottom < 0: self.top = games.screen.height if self.overlapping_sprites: for sprite in self.overlapping_sprites: sprite.die() self.die() def die(self): self.destroy()
def __init__(self): """ game init """ # set level self.level = 0 # sound load new level self.sound = games.load_sound(os.path.join(FILE_SERVING_ROOT, "poziom.wav")) # score self.score = games.Text(value = 0, size = 30, color = color.white, top = 5, right = games.screen.width - 10, is_collideable = False) games.screen.add(self.score) self.ship = Ship(game = self, x = games.screen.width/2, y = games.screen.height/2) games.screen.add(self.ship)