Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3
- import random
- import arcade
- SCREEN_WIDTH = 800
- SCREEN_HEIGHT = 600
- SPRITE_SCALING = 2
- MOVEMENT_SPEED = 5
- BULLET_SPEED = 8
- ENEMY1_SPEED = 12
- ENEMY_BULLET_SPEED = 16
- EXPLOSION_TEXTURE_COUNT = 270
- class BgStar:
- # each instance is a star made my drawing filled circles
- def __init__(self):
- self.x = 0
- self.y = 0
- def reset_pos(self):
- # reset star when star falls off screen
- self.y = random.randrange(SCREEN_HEIGHT, SCREEN_HEIGHT + 100)
- self.x = random.randrange(SCREEN_WIDTH)
- class PlayerBullet(arcade.Sprite):
- def update(self):
- self.center_y += BULLET_SPEED
- class Player_life_Sprite(arcade.Sprite):
- def __init__(self):
- super().__init__()
- self.texture = arcade.load_texture('ship.png', scale=SPRITE_SCALING/2)
- class Enemy1(arcade.Sprite):
- def __init__(self):
- super().__init__()
- self.texture = arcade.load_texture('enemy1.png', scale=SPRITE_SCALING)
- self.direction = ''
- def changeDirection(self, direction):
- if direction == 'left':
- self.direction = 'right'
- if direction == 'right':
- self.direction = 'left'
- def update(self):
- # enemy drops
- if self.center_y > SCREEN_HEIGHT * 0.6:
- self.center_y -= ENEMY1_SPEED
- # ememy moves side to side
- # randomly chnage direction
- random_number = random.randint(1, 30)
- if random_number == 1:
- self.changeDirection(self.direction)
- # changes direction at side of screen
- if self.right > SCREEN_WIDTH - 10:
- self.direction = 'left'
- if self.left < 10:
- self.direction = 'right'
- # move depending on direction
- if self.direction == 'left':
- self.center_x -= ENEMY1_SPEED
- if self.direction == 'right':
- self.center_x += ENEMY1_SPEED
- class Enemy_Bullet(arcade.Sprite):
- def update(self):
- self.center_y -= ENEMY_BULLET_SPEED
- class Explosion(arcade.Sprite): # creates explosion animation
- # static variable that holds the explosion textures (per frame)
- explosion_textures = []
- def __init__(self):
- super().__init__('explo1.png')
- # start at the first frame
- self.current_texture = 0
- self.texture = self.textures[self.current_texture]
- def update(self):
- # update the next frame of the animation. If we are at the end of our frames, then delete this sprite
- self.current_texture += 1
- print(self.current_texture)
- if self.current_texture < len(Explosion.explosion_textures):
- self.texture = Explosion.explosion_textures[self.current_texture]
- else:
- print('kill')
- self.kill()
- # pre load animation frames. Not done in __init__ because ot takes too long and would pause the game.
- for i in range(EXPLOSION_TEXTURE_COUNT):
- # loads first half of explosion count alternating between explo1 and explo2, and then 2nd half with explo2, explo3
- if i < EXPLOSION_TEXTURE_COUNT / 2:
- if i < (EXPLOSION_TEXTURE_COUNT / 2) / 4:
- texture = arcade.load_texture('explo1.png', scale=SPRITE_SCALING)
- Explosion.explosion_textures.append(texture)
- print(1)
- elif i > (EXPLOSION_TEXTURE_COUNT / 2) / 2 and i < (EXPLOSION_TEXTURE_COUNT / 2) * 1.125:
- texture = arcade.load_texture('explo1.png', scale=SPRITE_SCALING)
- Explosion.explosion_textures.append(texture)
- print(1)
- else:
- texture = arcade.load_texture('explo2.png', scale=SPRITE_SCALING)
- Explosion.explosion_textures.append(texture)
- print(2)
- else:
- if i < (EXPLOSION_TEXTURE_COUNT / 2) * 1.125 :
- texture = arcade.load_texture('explo3.png', scale=SPRITE_SCALING)
- Explosion.explosion_textures.append(texture)
- print(3)
- elif i > (EXPLOSION_TEXTURE_COUNT / 2) * 1.125 and i < (EXPLOSION_TEXTURE_COUNT / 2) * 1.5:
- texture = arcade.load_texture('explo3.png', scale=SPRITE_SCALING)
- Explosion.explosion_textures.append(texture)
- print(3)
- elif i > (EXPLOSION_TEXTURE_COUNT / 2) * 1.75:
- texture = arcade.load_texture('explo3.png', scale=SPRITE_SCALING)
- Explosion.explosion_textures.append(texture)
- print(3)
- else:
- texture = arcade.load_texture('explo2.png', scale=SPRITE_SCALING)
- Explosion.explosion_textures.append(texture)
- print(2)
- print('len of textures is ' + str(len(Explosion.explosion_textures)))
- for i in range(len(Explosion.explosion_textures)):
- print(Explosion.explosion_textures[i])
- class Player(arcade.Sprite):
- def __init__(self):
- super().__init__()
- # Load textures, LEFT, RIGHT AND NORMAL
- self.texture_normal = arcade.load_texture('ship.png', scale=SPRITE_SCALING)
- self.texture_left = arcade.load_texture('shipL.png', scale=SPRITE_SCALING)
- self.textur_right = arcade.load_texture('shipR.png', scale=SPRITE_SCALING)
- # set normal as default
- self.texture = self.texture_normal
- def update(self): # enables player sprite moving
- # and changes sprite dependant on direction
- self.center_x += self.change_x
- self.center_y += self.change_y
- # Figure out what player sprite to display
- if self.change_x < 0: # moving left
- self.texture = self.texture_left
- if self.change_x > 0: # moving right
- self.texture = self.textur_right
- if self.change_x == 0: # not moving left of right
- self.texture = self.texture_normal
- # logic to keep player from leaving the screen
- if self.left < 0:
- self.left = 0
- elif self.right > SCREEN_WIDTH - 1:
- self.right = SCREEN_WIDTH - 1
- if self.bottom < 0:
- self.bottom = 0
- elif self.top > SCREEN_HEIGHT - 1:
- self.top = SCREEN_HEIGHT - 1
- class MyGame(arcade.Window):
- # main apilication class
- def __init__(self, width, height): # takes screen height and width
- # calls init of parent class (arcade.window) to set up screen
- super().__init__(width, height)
- # sprite lists
- self.bgstar_list = None
- self.player_sprite_list = None
- self.player_bullet_list = None
- self.enemy1_list = None
- self.enemies_list = None
- self.player_lives_list = None
- self.enemy_bullet_list = None
- self.explosions_list = None
- # set up player info
- self.player_sprite = None
- self.score = 0
- def setup(self): # set up game and init variables
- # sprite lists
- self.player_sprite_list = arcade.SpriteList()
- self.player_bullet_list = arcade.SpriteList()
- self.enemy1_list = arcade.SpriteList()
- self.enemies_list = arcade.SpriteList()
- self.player_lives_list = arcade.SpriteList()
- self.enemy_bullet_list = arcade.SpriteList()
- self.explosions_list = arcade.SpriteList()
- # Set up the player
- self.score = 0
- self.player_sprite = Player()
- self.player_sprite.center_x = SCREEN_WIDTH / 2
- self.player_sprite.center_y = 0 - SCREEN_HEIGHT
- self.player_sprite_list.append(self.player_sprite)
- self.player_lives = 3
- # set up player life sprites in top right corner
- for i in range(self.player_lives):
- life_sprite = Player_life_Sprite()
- life_sprite.center_x = SCREEN_WIDTH - ( 25 * i+1 + 25)
- life_sprite.center_y = SCREEN_HEIGHT - 30
- self.player_lives_list.append(life_sprite)
- def start_bgstars(self): # set up stars background
- self.bgstar_list = []
- for i in range(50):
- # create star instance
- bgstar = BgStar()
- # randomly postion star
- bgstar.x = random.randrange(SCREEN_WIDTH)
- bgstar.y = random.randrange(SCREEN_HEIGHT + 200)
- # set other variables for the star
- bgstar.size = random.randrange(4)
- bgstar.speed = random.randrange(20, 40)
- # add star to the list
- self.bgstar_list.append(bgstar)
- # hide mouse pointer
- self.set_mouse_visible(False)
- # set the background color
- arcade.set_background_color(arcade.color.BLACK)
- def on_draw(self): # render the screen
- arcade.start_render() # This command is necessary before drawing
- # Draw the current postion of each star
- for star in self.bgstar_list:
- arcade.draw_circle_filled(star.x, star.y, star.size, arcade.color.WHITE)
- # create life list
- # Draw all the sprites.
- self.player_sprite_list.draw()
- self.player_bullet_list.draw()
- self.enemy1_list.draw()
- self.player_lives_list.draw()
- self.enemy_bullet_list.draw()
- self.explosions_list.draw()
- #draw scrore
- arcade.draw_text(str(self.score), 20, SCREEN_HEIGHT - 40, arcade.color.WHITE, 20)
- def on_key_press(self, key, modifiers): # call whenever a
- # key is pressed
- if key == arcade.key.UP:
- self.player_sprite.change_y = MOVEMENT_SPEED
- elif key == arcade.key.DOWN:
- self.player_sprite.change_y = - MOVEMENT_SPEED
- elif key == arcade.key.LEFT:
- self.player_sprite.change_x = - MOVEMENT_SPEED
- elif key == arcade.key.RIGHT:
- self.player_sprite.change_x = MOVEMENT_SPEED
- if key == arcade.key.SPACE and len(self.player_bullet_list) < 4: # Fires Bullets
- # create a bullet
- bullet = PlayerBullet('bullet.png', SPRITE_SCALING)
- # postion the bullet
- bullet.center_x = self.player_sprite.center_x
- bullet.bottom = self.player_sprite.top
- # add bullet to list
- self.player_bullet_list.append(bullet)
- def on_key_release(self, key, modifiers): # resets change in x, y to 0
- # when user releases key
- if key == arcade.key.UP or key == arcade.key.DOWN:
- self.player_sprite.change_y = 0
- elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
- self.player_sprite.change_x = 0
- def update(self, delta_time): # All the logic to move and game logic goes here
- # animate the stars falling
- for star in self.bgstar_list:
- star.y -= star.speed * delta_time
- # check if star has falled below the screen
- if star.y < 0:
- star.reset_pos()
- # Call update on the sprites
- self.player_sprite_list.update()
- self.player_bullet_list.update()
- self.enemy1_list.update()
- self.enemy_bullet_list.update()
- self.explosions_list.update()
- # Check if player bullet hit something or off screen
- for bullet in self.player_bullet_list:
- if bullet.bottom > SCREEN_HEIGHT:
- bullet.kill()
- hit_list = arcade.check_for_collision_with_list(bullet, self.enemies_list)
- if len(hit_list) > 0:
- bullet.kill()
- # for every enemy, increase score and remove enemy
- for enemy in hit_list:
- enemy.kill()
- self.score += 1
- # look at enemy1 list and spwan enemy if == 0
- if len(self.enemy1_list) == 0:
- # use random number to decide to generate
- if random.randint(1, 100) == 1:
- # create enemy1 instance
- enemy1 = Enemy1()
- # Rotate and spawn postion
- enemy1.angle = 180
- enemy1.bottom = SCREEN_HEIGHT + 1
- enemy1.center_x = random.randrange(SCREEN_WIDTH)
- enemy1.direction = random.choice(['left', 'right'])
- # add enemy to list
- self.enemy1_list.append(enemy1)
- self.enemies_list.append(enemy1)
- # if enemy 1 exists, shoot at random intervals
- if len(self.enemy1_list) != 0:
- for enemy in self.enemy1_list:
- if random.randint(1, 50) == 1:
- enemy_bullet = Enemy_Bullet('enemy_bullet1.png', SPRITE_SCALING)
- enemy_bullet.top = enemy.bottom
- enemy_bullet.center_x = enemy.center_x
- self.enemy_bullet_list.append(enemy_bullet)
- # check if enemy bullet hits player or if goes off screen
- for e_bullet in self.enemy_bullet_list:
- if e_bullet.center_y < 0:
- e_bullet.kill()
- hit_list = arcade.check_for_collision_with_list(e_bullet, self.player_sprite_list)
- if len(hit_list) > 0:
- e_bullet.kill()
- for hit in hit_list:
- explosion = Explosion()
- explosion.center_x = SCREEN_HEIGHT/2
- explosion.center_y = SCREEN_WIDTH/2
- self.explosions_list.append(explosion)
- self.player_lives -= 1
- self.player_lives_list.pop()
- # NEXT TO DO, CREATE EXPLOSOION ANIMATION ON DEATH AND GAME OVER SCREEN
- def main():
- window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
- window.start_bgstars()
- window.setup()
- arcade.run()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement