Advertisement
shh_algo_PY

Shooter Game - Game 2

Nov 4th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.01 KB | None | 0 0
  1. from pygame import *
  2. #-----------------------------#
  3. # Add random module
  4. from random import randint
  5. #-----------------------------#
  6.  
  7. # Background music
  8. mixer.init()
  9. mixer.music.load('space.ogg')
  10. mixer.music.play()
  11.  
  12. #-----------------------------#
  13. # Load images --- Add your enemy!
  14. img_back = "galaxy.jpg" #game background
  15. img_hero = "rocket.png" #hero
  16. img_enemy = "ufo.png" #enemy
  17. #-----------------------------#
  18.  
  19. # Create a window
  20. win_width = 700
  21. win_height = 500
  22. window = display.set_mode((win_width, win_height))
  23. display.set_caption("Space shooter")
  24. background = transform.scale(image.load(img_back), (win_width, win_height))
  25.  
  26. #-----------------------------#
  27. # Fonts and captions
  28. font.init()
  29. font2 = font.SysFont('Comic Sans MS', 30)
  30.  
  31. score = 0 #ships destroyed
  32. lost = 0 #ships missed
  33. #-----------------------------#
  34.  
  35. # GameSprite class - inheriting from class Sprite
  36. class GameSprite(sprite.Sprite):
  37.     def __init__(self, player_image, player_x, player_y, size_x, size_y, player_speed):
  38.         # Call for the class (Sprite) constructor:
  39.         sprite.Sprite.__init__(self)
  40.  
  41.         # Every sprite must store the image property
  42.         self.image = transform.scale(image.load(player_image), (size_x, size_y))
  43.         self.speed = player_speed
  44.  
  45.         # Every sprite must have the rect property – the rectangle it is fitted in
  46.         self.rect = self.image.get_rect()
  47.         self.rect.x = player_x
  48.         self.rect.y = player_y
  49.    
  50.     # Puts the character in the window
  51.     def reset(self):
  52.         window.blit(self.image, (self.rect.x, self.rect.y))
  53.  
  54. # Player class - inherits from GameSprite
  55. class Player(GameSprite):
  56.     # Function to control the sprite with arrow keys - only LEFT and RIGHT needed
  57.     def update(self):
  58.         keys = key.get_pressed()
  59.         if keys[K_LEFT] and self.rect.x > 5:
  60.             self.rect.x -= self.speed
  61.         if keys[K_RIGHT] and self.rect.x < win_width - 80:
  62.             self.rect.x += self.speed
  63.    
  64.     # Function to "shoot" (use the player position to create a bullet there)
  65.     def fire(self):
  66.         pass
  67.  
  68. #-----------------------------#
  69. # Add your enemy class
  70.  
  71. class Enemy(GameSprite):
  72.     #enemy movement
  73.     def update(self):
  74.         self.rect.y += self.speed
  75.         global lost
  76.         #disappears upon reaching the screen edge
  77.         if self.rect.y > win_height:
  78.             self.rect.x = randint(80, win_width - 80)
  79.             self.rect.y = 0
  80.             lost = lost + 1
  81.  
  82. #-----------------------------#
  83.  
  84. # Create sprites
  85. ship = Player(img_hero, 5, win_height - 100, 80, 100, 10)
  86.  
  87.  
  88. #-----------------------------#
  89. # ALIEN LOOP!
  90.  
  91. monsters = sprite.Group()
  92. for i in range(1,10):
  93.    #player_image, player_x, player_y, size_x, size_y, player_speed
  94.    monster = Enemy(img_enemy, randint(80, win_width - 80), -40, 80, 50, randint(1, 10))
  95.    monsters.add(monster)
  96.  
  97. #-----------------------------#
  98.  
  99. # The "game is over" variable
  100. # As soon as it becomes True, all sprites stop working in the main loop
  101. finish = False
  102.  
  103. # Main game loop
  104. # The game is reset by the window close button
  105. run = True
  106.  
  107. #-----------------------------#
  108. # UPDATE THE GAME LOOP!
  109. # inside if not finish --- aliens, text
  110. #-----------------------------#
  111.  
  112. while run:
  113.     # "Close" button press event
  114.     for e in event.get():
  115.         if e.type == QUIT:
  116.             run = False
  117.  
  118.     if not finish:
  119.         # Update the background
  120.         window.blit(background,(0,0))
  121.  
  122.         # Add text on the screen
  123.         text = font2.render("Score: " + str(score), 1, (255, 255, 255))
  124.         window.blit(text, (10, 20))
  125.  
  126.         text_lose = font2.render("Missed: " + str(lost), 1, (255, 255, 255))
  127.         window.blit(text_lose, (10, 50))
  128.  
  129.         # Start sprite movements
  130.         ship.update()
  131.         monsters.update()
  132.  
  133.         # Update them in a new location in each loop iteration
  134.         ship.reset()
  135.         monsters.draw(window)
  136.  
  137.         display.update()
  138.    
  139.     #the loop is executed each 0.05 sec
  140.     time.delay(50)
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement