Facebook
From Algo, 1 Month ago, written in Python.
Embed
Download Paste or View Raw
Hits: 135
  1.  
  2.  
  3. from pygame import *
  4. from random import randint
  5.  
  6.  
  7. #background music
  8. mixer.init()
  9. mixer.music.load('space.ogg')
  10. mixer.music.play()
  11. fire_sound = mixer.Sound('fire.ogg')
  12.  
  13.  
  14. #fonts and captions
  15. font.init()
  16. font2 = font.Font(None, 36)
  17.  
  18.  
  19. #we need the following images:
  20. img_back = "galaxy.jpg" # game background
  21. img_hero = "rocket.png" # hero
  22. img_enemy = "ufo.png" # enemy
  23.  
  24.  
  25. score = 0 #ships destroyed
  26. lost = 0 #ships missed
  27.  
  28.  
  29. #parent class for other sprites
  30. class GameSprite(sprite.Sprite):
  31.  #class constructor
  32.    def __init__(self, player_image, player_x, player_y, size_x, size_y, player_speed):
  33.        #Call for the class (Sprite) constructor:
  34.        sprite.Sprite.__init__(self)
  35.  
  36.  
  37.        #every sprite must store the image property
  38.        self.image = transform.scale(image.load(player_image), (size_x, size_y))
  39.        self.speed = player_speed
  40.  
  41.  
  42.        #every sprite must have the rect property that represents the rectangle it is fitted in
  43.        self.rect = self.image.get_rect()
  44.        self.rect.x = player_x
  45.        self.rect.y = player_y
  46.  #method drawing the character on the window
  47.    def reset(self):
  48.        window.blit(self.image, (self.rect.x, self.rect.y))
  49.  
  50.  
  51. #main player class
  52. class Player(GameSprite):
  53.    #method to control the sprite with arrow keys
  54.    def update(self):
  55.        keys = key.get_pressed()
  56.        if keys[K_LEFT] and self.rect.x > 5:
  57.            self.rect.x -= self.speed
  58.        if keys[K_RIGHT] and self.rect.x < win_width - 80:
  59.            self.rect.x += self.speed
  60.  #method to "shoot" (use the player position to create a bullet there)
  61.    def fire(self):
  62.        pass
  63.  
  64.  
  65. #enemy sprite class  
  66. class Enemy(GameSprite):
  67.    #enemy movement
  68.    def update(self):
  69.        self.rect.y += self.speed
  70.        global lost
  71.        #disappears upon reaching the screen edge
  72.        if self.rect.y > win_height:
  73.            self.rect.x = randint(80, win_width - 80)
  74.            self.rect.y = 0
  75.            lost = lost + 1
  76.  
  77.  
  78. #create a small window
  79. win_width = 700
  80. win_height = 500
  81. display.set_caption("Shooter")
  82. window = display.set_mode((win_width, win_height))
  83. background = transform.scale(image.load(img_back), (win_width, win_height))
  84.  
  85.  
  86. #create sprites
  87. ship = Player(img_hero, 5, win_height - 100, 80, 100, 10)
  88.  
  89.  
  90. monsters = sprite.Group()
  91. for i in range(1, 6):
  92.    monster = Enemy(img_enemy, randint(80, win_width - 80), -40, 80, 50, randint(1, 5))
  93.    monsters.add(monster)
  94.  
  95.  
  96. #the "game is over" variable: as soon as True is there, sprites stop working in the main loop
  97. finish = False
  98. #Main game loop:
  99. run = True #the flag is reset by the window close button
  100. while run:
  101.    #"Close" button press event
  102.    for e in event.get():
  103.        if e.type == QUIT:
  104.            run = False
  105.  
  106.  
  107.    if not finish:
  108.        #update the background
  109.        window.blit(background,(0,0))
  110.  
  111.  
  112.        #write text on the screen
  113.        text = font2.render("Score: " + str(score), 1, (255, 255, 255))
  114.        window.blit(text, (10, 20))
  115.  
  116.  
  117.        text_lose = font2.render("Missed: " + str(lost), 1, (255, 255, 255))
  118.        window.blit(text_lose, (10, 50))
  119.  
  120.  
  121.        #launch sprite movements
  122.        ship.update()
  123.        monsters.update()
  124.  
  125.  
  126.        #update them in a new location in each loop iteration
  127.        ship.reset()
  128.        monsters.draw(window)
  129.  
  130.  
  131.        display.update()
  132.    #the loop is executed each 0.05 sec
  133.    time.delay(50)