Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.64 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. import random
  4. import arcade
  5.  
  6. SCREEN_WIDTH = 800
  7. SCREEN_HEIGHT = 600
  8.  
  9. SPRITE_SCALING = 2
  10. MOVEMENT_SPEED = 5
  11.  
  12. BULLET_SPEED = 8
  13.  
  14. ENEMY1_SPEED = 12
  15. ENEMY_BULLET_SPEED = 16
  16.  
  17. EXPLOSION_TEXTURE_COUNT = 270
  18.  
  19. class BgStar:
  20.     # each instance is a star made my drawing filled circles
  21.     def __init__(self):
  22.         self.x = 0
  23.         self.y = 0
  24.  
  25.     def reset_pos(self):
  26.         # reset star when star falls off screen
  27.         self.y = random.randrange(SCREEN_HEIGHT, SCREEN_HEIGHT + 100)
  28.         self.x = random.randrange(SCREEN_WIDTH)
  29.  
  30.  
  31. class PlayerBullet(arcade.Sprite):
  32.  
  33.     def update(self):
  34.  
  35.         self.center_y += BULLET_SPEED
  36.  
  37. class Player_life_Sprite(arcade.Sprite):
  38.  
  39.      def __init__(self):
  40.             super().__init__()
  41.             self.texture = arcade.load_texture('ship.png', scale=SPRITE_SCALING/2)
  42.  
  43.  
  44. class Enemy1(arcade.Sprite):
  45.  
  46.     def __init__(self):
  47.         super().__init__()
  48.         self.texture = arcade.load_texture('enemy1.png', scale=SPRITE_SCALING)
  49.         self.direction = ''
  50.  
  51.     def changeDirection(self, direction):
  52.         if direction == 'left':
  53.             self.direction = 'right'
  54.         if direction == 'right':
  55.             self.direction = 'left'
  56.  
  57.     def update(self):
  58.         # enemy drops
  59.         if self.center_y > SCREEN_HEIGHT * 0.6:
  60.             self.center_y -= ENEMY1_SPEED
  61.  
  62.  
  63.         # ememy moves side to side
  64.         # randomly chnage direction
  65.         random_number = random.randint(1, 30)
  66.         if random_number == 1:
  67.             self.changeDirection(self.direction)
  68.  
  69.         # changes direction at side of screen
  70.         if self.right > SCREEN_WIDTH - 10:
  71.             self.direction = 'left'
  72.         if self.left < 10:
  73.             self.direction = 'right'
  74.  
  75.         # move depending on direction
  76.         if self.direction == 'left':
  77.             self.center_x -= ENEMY1_SPEED
  78.         if self.direction == 'right':
  79.             self.center_x += ENEMY1_SPEED
  80.  
  81.  
  82. class Enemy_Bullet(arcade.Sprite):
  83.     def update(self):
  84.         self.center_y -= ENEMY_BULLET_SPEED
  85.  
  86. class Explosion(arcade.Sprite):  # creates explosion animation
  87.  
  88.     # static variable that holds the explosion textures (per frame)
  89.     explosion_textures = []
  90.  
  91.     def __init__(self):
  92.         super().__init__('explo1.png')
  93.  
  94.         # start at the first frame
  95.         self.current_texture = 0
  96.         self.texture = self.textures[self.current_texture]
  97.  
  98.     def update(self):
  99.  
  100.         # update the next frame of the animation. If we are at the end of our frames, then delete this sprite
  101.         self.current_texture += 1
  102.         print(self.current_texture)
  103.         if self.current_texture < len(Explosion.explosion_textures):
  104.             self.texture = Explosion.explosion_textures[self.current_texture]
  105.         else:
  106.             print('kill')
  107.             self.kill()
  108.  
  109. # pre load animation frames.  Not done in __init__ because ot takes too long and would pause the game.
  110. for i in range(EXPLOSION_TEXTURE_COUNT):
  111.     # loads first half of explosion count alternating between explo1 and explo2, and then 2nd half with explo2, explo3
  112.     if i < EXPLOSION_TEXTURE_COUNT / 2:
  113.         if i < (EXPLOSION_TEXTURE_COUNT / 2) / 4:
  114.             texture = arcade.load_texture('explo1.png', scale=SPRITE_SCALING)
  115.             Explosion.explosion_textures.append(texture)
  116.             print(1)
  117.         elif i > (EXPLOSION_TEXTURE_COUNT / 2) / 2 and i < (EXPLOSION_TEXTURE_COUNT / 2) * 1.125:
  118.             texture = arcade.load_texture('explo1.png', scale=SPRITE_SCALING)
  119.             Explosion.explosion_textures.append(texture)
  120.             print(1)
  121.         else:
  122.             texture = arcade.load_texture('explo2.png', scale=SPRITE_SCALING)
  123.             Explosion.explosion_textures.append(texture)
  124.             print(2)
  125.     else:
  126.         if  i < (EXPLOSION_TEXTURE_COUNT / 2) * 1.125 :
  127.             texture = arcade.load_texture('explo3.png', scale=SPRITE_SCALING)
  128.             Explosion.explosion_textures.append(texture)
  129.             print(3)
  130.         elif i > (EXPLOSION_TEXTURE_COUNT / 2) * 1.125 and i < (EXPLOSION_TEXTURE_COUNT / 2) * 1.5:
  131.             texture = arcade.load_texture('explo3.png', scale=SPRITE_SCALING)
  132.             Explosion.explosion_textures.append(texture)
  133.             print(3)
  134.         elif i > (EXPLOSION_TEXTURE_COUNT / 2) * 1.75:
  135.             texture = arcade.load_texture('explo3.png', scale=SPRITE_SCALING)
  136.             Explosion.explosion_textures.append(texture)
  137.             print(3)
  138.         else:
  139.             texture = arcade.load_texture('explo2.png', scale=SPRITE_SCALING)
  140.             Explosion.explosion_textures.append(texture)
  141.             print(2)
  142. print('len of textures is ' + str(len(Explosion.explosion_textures)))
  143. for i in range(len(Explosion.explosion_textures)):
  144.     print(Explosion.explosion_textures[i])
  145.  
  146.  
  147.  
  148.  
  149. class Player(arcade.Sprite):
  150.  
  151.     def __init__(self):
  152.         super().__init__()
  153.  
  154.         # Load textures, LEFT, RIGHT AND NORMAL
  155.         self.texture_normal = arcade.load_texture('ship.png', scale=SPRITE_SCALING)
  156.         self.texture_left = arcade.load_texture('shipL.png', scale=SPRITE_SCALING)
  157.         self.textur_right = arcade.load_texture('shipR.png', scale=SPRITE_SCALING)
  158.         # set normal as default
  159.         self.texture = self.texture_normal
  160.  
  161.     def update(self):  # enables player sprite moving
  162.                        # and changes sprite dependant on direction
  163.         self.center_x += self.change_x
  164.         self.center_y += self.change_y
  165.  
  166.         # Figure out what player sprite to display
  167.         if self.change_x < 0:  # moving left
  168.             self.texture = self.texture_left
  169.         if self.change_x > 0:  # moving right
  170.             self.texture = self.textur_right
  171.         if self.change_x == 0:  # not moving left of right
  172.             self.texture = self.texture_normal
  173.  
  174.         # logic to keep player from leaving the screen
  175.         if self.left < 0:
  176.             self.left = 0
  177.         elif self.right > SCREEN_WIDTH - 1:
  178.             self.right = SCREEN_WIDTH - 1
  179.  
  180.         if self.bottom < 0:
  181.             self.bottom = 0
  182.         elif self.top > SCREEN_HEIGHT - 1:
  183.             self.top = SCREEN_HEIGHT - 1
  184.  
  185.  
  186. class MyGame(arcade.Window):
  187.     # main apilication class
  188.  
  189.     def __init__(self, width, height):   # takes screen height and width
  190.         # calls init of parent class  (arcade.window) to set up screen
  191.         super().__init__(width, height)
  192.  
  193.         # sprite lists
  194.         self.bgstar_list = None
  195.         self.player_sprite_list = None
  196.         self.player_bullet_list = None
  197.         self.enemy1_list = None
  198.         self.enemies_list = None
  199.         self.player_lives_list = None
  200.         self.enemy_bullet_list = None
  201.         self.explosions_list = None
  202.  
  203.         # set up player info
  204.         self.player_sprite = None
  205.         self.score = 0
  206.  
  207.  
  208.     def setup(self):  # set up game and init variables
  209.  
  210.         # sprite lists
  211.         self.player_sprite_list = arcade.SpriteList()
  212.         self.player_bullet_list = arcade.SpriteList()
  213.         self.enemy1_list = arcade.SpriteList()
  214.         self.enemies_list = arcade.SpriteList()
  215.         self.player_lives_list = arcade.SpriteList()
  216.         self.enemy_bullet_list = arcade.SpriteList()
  217.         self.explosions_list = arcade.SpriteList()
  218.  
  219.         # Set up the player
  220.         self.score = 0
  221.         self.player_sprite = Player()
  222.         self.player_sprite.center_x = SCREEN_WIDTH / 2
  223.         self.player_sprite.center_y = 0 - SCREEN_HEIGHT
  224.         self.player_sprite_list.append(self.player_sprite)
  225.         self.player_lives = 3
  226.         # set up player life sprites in top right corner
  227.         for i in range(self.player_lives):
  228.             life_sprite = Player_life_Sprite()
  229.             life_sprite.center_x = SCREEN_WIDTH -  ( 25 * i+1  + 25)
  230.             life_sprite.center_y = SCREEN_HEIGHT - 30
  231.             self.player_lives_list.append(life_sprite)
  232.  
  233.  
  234.     def start_bgstars(self):  # set up stars background
  235.         self.bgstar_list = []
  236.  
  237.         for i in range(50):
  238.             # create star instance
  239.             bgstar = BgStar()
  240.  
  241.             # randomly postion star
  242.             bgstar.x = random.randrange(SCREEN_WIDTH)
  243.             bgstar.y = random.randrange(SCREEN_HEIGHT + 200)
  244.  
  245.             # set other variables for the star
  246.             bgstar.size = random.randrange(4)
  247.             bgstar.speed = random.randrange(20, 40)
  248.  
  249.             # add star to the list
  250.             self.bgstar_list.append(bgstar)
  251.  
  252.         # hide mouse pointer
  253.         self.set_mouse_visible(False)
  254.  
  255.         # set the background color
  256.         arcade.set_background_color(arcade.color.BLACK)
  257.  
  258.     def on_draw(self):  # render the screen
  259.  
  260.         arcade.start_render()  # This command is necessary before drawing
  261.  
  262.         # Draw the current postion of each star
  263.         for star in self.bgstar_list:
  264.             arcade.draw_circle_filled(star.x, star.y, star.size, arcade.color.WHITE)
  265.  
  266.         # create life list
  267.  
  268.  
  269.  
  270.         # Draw all the sprites.
  271.         self.player_sprite_list.draw()
  272.         self.player_bullet_list.draw()
  273.         self.enemy1_list.draw()
  274.         self.player_lives_list.draw()
  275.         self.enemy_bullet_list.draw()
  276.         self.explosions_list.draw()
  277.  
  278.  
  279.         #draw scrore
  280.         arcade.draw_text(str(self.score), 20, SCREEN_HEIGHT - 40, arcade.color.WHITE, 20)
  281.  
  282.  
  283.  
  284.     def on_key_press(self, key, modifiers):  # call whenever a
  285.                                              # key is pressed
  286.         if key == arcade.key.UP:
  287.             self.player_sprite.change_y = MOVEMENT_SPEED
  288.         elif key ==  arcade.key.DOWN:
  289.             self.player_sprite.change_y =  - MOVEMENT_SPEED
  290.         elif key == arcade.key.LEFT:
  291.             self.player_sprite.change_x =  - MOVEMENT_SPEED
  292.         elif key == arcade.key.RIGHT:
  293.             self.player_sprite.change_x = MOVEMENT_SPEED
  294.  
  295.         if key == arcade.key.SPACE and len(self.player_bullet_list) < 4:  # Fires Bullets
  296.  
  297.             # create a bullet
  298.             bullet = PlayerBullet('bullet.png', SPRITE_SCALING)
  299.  
  300.             # postion the bullet
  301.             bullet.center_x = self.player_sprite.center_x
  302.             bullet.bottom = self.player_sprite.top
  303.  
  304.             # add bullet to list
  305.             self.player_bullet_list.append(bullet)
  306.  
  307.  
  308.  
  309.  
  310.     def on_key_release(self, key, modifiers): # resets change in x, y to 0
  311.                                               # when user releases key
  312.         if key == arcade.key.UP or key == arcade.key.DOWN:
  313.             self.player_sprite.change_y = 0
  314.         elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
  315.             self.player_sprite.change_x = 0
  316.  
  317.  
  318.     def update(self, delta_time):  # All the logic to move and game logic goes here
  319.  
  320.         # animate the stars falling
  321.         for star in self.bgstar_list:
  322.             star.y -= star.speed * delta_time
  323.  
  324.             # check if star has falled below the screen
  325.             if star.y < 0:
  326.                 star.reset_pos()
  327.  
  328.         # Call update on the sprites
  329.         self.player_sprite_list.update()
  330.         self.player_bullet_list.update()
  331.         self.enemy1_list.update()
  332.         self.enemy_bullet_list.update()
  333.         self.explosions_list.update()
  334.  
  335.         # Check if player bullet hit something or off screen
  336.         for bullet in self.player_bullet_list:
  337.  
  338.             if bullet.bottom > SCREEN_HEIGHT:
  339.                 bullet.kill()
  340.  
  341.             hit_list = arcade.check_for_collision_with_list(bullet, self.enemies_list)
  342.             if len(hit_list) > 0:
  343.                 bullet.kill()
  344.  
  345.             # for every enemy, increase score and remove enemy
  346.             for enemy in hit_list:
  347.                 enemy.kill()
  348.                 self.score += 1
  349.  
  350.  
  351.         # look at enemy1 list and spwan enemy if  == 0
  352.         if len(self.enemy1_list) == 0:
  353.             # use random number to decide to generate
  354.             if random.randint(1, 100) == 1:
  355.                 # create enemy1 instance
  356.                 enemy1 = Enemy1()
  357.                 # Rotate and spawn postion
  358.                 enemy1.angle = 180
  359.                 enemy1.bottom = SCREEN_HEIGHT + 1
  360.                 enemy1.center_x = random.randrange(SCREEN_WIDTH)
  361.                 enemy1.direction = random.choice(['left', 'right'])
  362.  
  363.                 # add enemy to list
  364.                 self.enemy1_list.append(enemy1)
  365.                 self.enemies_list.append(enemy1)
  366.  
  367.         # if enemy 1 exists, shoot at random intervals
  368.         if len(self.enemy1_list) != 0:
  369.             for enemy in self.enemy1_list:
  370.                 if random.randint(1, 50) == 1:
  371.                     enemy_bullet = Enemy_Bullet('enemy_bullet1.png', SPRITE_SCALING)
  372.                     enemy_bullet.top = enemy.bottom
  373.                     enemy_bullet.center_x = enemy.center_x
  374.                     self.enemy_bullet_list.append(enemy_bullet)
  375.  
  376.         # check if enemy bullet hits player or if goes off screen
  377.         for e_bullet in self.enemy_bullet_list:
  378.             if e_bullet.center_y < 0:
  379.                 e_bullet.kill()
  380.  
  381.             hit_list = arcade.check_for_collision_with_list(e_bullet, self.player_sprite_list)
  382.             if len(hit_list) > 0:
  383.                 e_bullet.kill()
  384.  
  385.             for hit in hit_list:
  386.                 explosion = Explosion()
  387.                 explosion.center_x = SCREEN_HEIGHT/2
  388.                 explosion.center_y = SCREEN_WIDTH/2
  389.                 self.explosions_list.append(explosion)
  390.  
  391.                 self.player_lives -= 1
  392.                 self.player_lives_list.pop()
  393.  
  394.  
  395.  
  396.  
  397. # NEXT TO DO, CREATE EXPLOSOION ANIMATION ON DEATH AND GAME OVER SCREEN
  398.  
  399.  
  400.  
  401.  
  402. def main():
  403.     window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
  404.     window.start_bgstars()
  405.     window.setup()
  406.     arcade.run()
  407.  
  408.  
  409. if __name__ == "__main__":
  410.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement