Advertisement
shh_algo_PY

Shooter - Part 4.2

Jul 16th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.70 KB | None | 0 0
  1. from pygame import *
  2. from random import randint
  3.  
  4. #background music
  5. mixer.init()
  6. mixer.music.load('space.ogg')
  7. mixer.music.play()
  8. fire_sound = mixer.Sound('fire.ogg')
  9.  
  10. # Load images
  11. img_back = "galaxy.jpg" #game background
  12. img_hero = "rocket.png" #hero
  13. img_enemy = "ufo.png" #enemy
  14. img_bullet = "bullet.png" #bullet
  15.  
  16. # Create a window
  17. win_width = 700
  18. win_height = 500
  19. display.set_caption("Shooter")
  20. window = display.set_mode((win_width, win_height))
  21. background = transform.scale(image.load(img_back), (win_width, win_height))
  22.  
  23. #fonts and captions
  24. font.init()
  25. font2 = font.SysFont('Comic Sans MS', 30)
  26.  
  27. font1 = font.SysFont('Comic Sans MS', 80)
  28. win = font1.render('YOU WIN!', True, (255, 255, 255))
  29. lose = font1.render('YOU LOSE!', True, (180, 0, 0))
  30.  
  31. score = 0 #ships destroyed
  32. lost = 0 #ships missed
  33.  
  34. max_lost = 3 #how many ships you can lose
  35.  
  36. '''add goal'''
  37. goal = 10 #how many ships need to be shot down to win
  38.  
  39. # GameSprite class - inheriting from class Sprite
  40. class GameSprite(sprite.Sprite):
  41.     def __init__(self, player_image, player_x, player_y, size_x, size_y, player_speed):
  42.         # Call for the class (Sprite) constructor:
  43.         sprite.Sprite.__init__(self)
  44.  
  45.         # Every sprite must store the image property
  46.         self.image = transform.scale(image.load(player_image), (size_x, size_y))
  47.         self.speed = player_speed
  48.  
  49.         # Every sprite must have the rect property – the rectangle it is fitted in
  50.         self.rect = self.image.get_rect()
  51.         self.rect.x = player_x
  52.         self.rect.y = player_y
  53.    
  54.     # Puts the character in the window
  55.     def reset(self):
  56.         window.blit(self.image, (self.rect.x, self.rect.y))
  57.  
  58. #main player class
  59. class Player(GameSprite):
  60.     # Function to control the sprite with arrow keys - only LEFT and RIGHT needed
  61.     def update(self):
  62.         keys = key.get_pressed()
  63.         if keys[K_LEFT] and self.rect.x > 5:
  64.             self.rect.x -= self.speed
  65.         if keys[K_RIGHT] and self.rect.x < win_width - 80:
  66.             self.rect.x += self.speed
  67.    
  68.     # Function to "shoot" (use the player position to create a bullet there)
  69.     def fire(self):
  70.         bullet = Bullet(img_bullet, self.rect.centerx, self.rect.top, 15, 20, -15)
  71.         bullets.add(bullet)
  72.    
  73. class Enemy(GameSprite):
  74.     # Enemy movement
  75.     def update(self):
  76.         self.rect.y += self.speed
  77.         global lost
  78.         # Disappears upon reaching the screen edge
  79.         if self.rect.y > win_height:
  80.             self.rect.x = randint(80, win_width - 80)
  81.             self.rect.y = 0
  82.             lost = lost + 1
  83.  
  84. # Bullet sprite class  
  85. class Bullet(GameSprite):
  86.     def update(self):
  87.         self.rect.y += self.speed
  88.         # Disappears upon reaching the screen edge
  89.         if self.rect.y < 0:
  90.             self.kill()
  91.  
  92. #create sprites
  93. ship = Player(img_hero, 5, win_height - 100, 80, 100, 10)
  94.  
  95. #creating a group of enemy sprites
  96. monsters = sprite.Group()
  97. for i in range(1, 6):
  98.    monster = Enemy(img_enemy, randint(80, win_width - 80), -40, 80, 50, randint(1, 5))
  99.    monsters.add(monster)
  100.  
  101. bullets = sprite.Group()
  102.  
  103. # The "game is over" variable
  104. # As soon as it becomes True, all sprites stop working in the main loop
  105. finish = False
  106.  
  107. # Main game loop
  108. # The game is reset by the window close button
  109. run = True
  110.  
  111. '''Update game loop'''
  112.  
  113. while run:
  114.     # "Close" button press event
  115.     for e in event.get():
  116.         if e.type == QUIT:
  117.             run = False
  118.         # Event of pressing the space bar - the sprite shoots
  119.         elif e.type == KEYDOWN:
  120.             if e.key == K_SPACE:
  121.                 fire_sound.play()
  122.                 ship.fire()
  123.    
  124.     if not finish:
  125.         # Update the background
  126.         window.blit(background,(0,0))
  127.    
  128.         # Launch sprite movements
  129.         ship.update()
  130.         monsters.update()
  131.         bullets.update()
  132.    
  133.         # Update them in a new location in each loop iteration
  134.         ship.reset()
  135.         monsters.draw(window)
  136.         bullets.draw(window)
  137.  
  138.         # NEW - Check for a collision between a bullet and monsters (both monster and bullet disappear upon a touch)
  139.  
  140.         collides = sprite.groupcollide(monsters, bullets, True, True)
  141.        
  142.         for c in collides:
  143.             #this loop will repeat as many times as the number of monsters hit
  144.             # Score +1
  145.             score = score + 1
  146.             # Respawn
  147.             monster = Enemy(img_enemy, randint(80, win_width - 80), -40, 80, 50, randint(1, 5))
  148.             # Adding it to the monster group
  149.             monsters.add(monster)
  150.    
  151.         # Losing check
  152.         # Collision or too many missed
  153.         # Lose = change to losing screen, stop sprite controls
  154.  
  155.         if sprite.spritecollide(ship, monsters, False) or lost >= max_lost:
  156.             finish = True
  157.             window.blit(lose, (200, 200))
  158.    
  159.         # Winning check
  160.  
  161.         if score >= goal:
  162.             finish = True
  163.             window.blit(win, (200, 200))
  164.    
  165.         # Captions
  166.  
  167.         text = font2.render("Score: " + str(score), 1, (255, 255, 255))
  168.         window.blit(text, (10, 20))
  169.    
  170.         text_lose = font2.render("Missed: " + str(lost), 1, (255, 255, 255))
  171.         window.blit(text_lose, (10, 50))
  172.    
  173.         display.update()
  174.    
  175.     # Bonus: automatic restart of the game
  176.     else:
  177.         finish = False
  178.  
  179.         score = 0
  180.         lost = 0
  181.        
  182.         for b in bullets:
  183.             b.kill()
  184.         for m in monsters:
  185.             m.kill()
  186.    
  187.         time.delay(3000)
  188.  
  189.         for i in range(1, 6):
  190.             monster = Enemy(img_enemy, randint(80, win_width - 80), -40, 80, 50, randint(1, 5))
  191.             monsters.add(monster)
  192.        
  193.    
  194.     time.delay(50)
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement