Advertisement
shh_algo_PY

Shooting Game - Day 1

Jun 25th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. from pygame import *
  2.  
  3. # Background music
  4. mixer.init()
  5. mixer.music.load('space.ogg')
  6. mixer.music.play()
  7.  
  8. # Load images
  9. img_back = "galaxy.jpg" #game background
  10. img_hero = "rocket.png" #hero
  11.  
  12. # Create a window
  13. win_width = 700
  14. win_height = 500
  15. window = display.set_mode((win_width, win_height))
  16. display.set_caption("Space shooter")
  17. background = transform.scale(image.load(img_back), (win_width, win_height))
  18.  
  19. # GameSprite class - inheriting from class Sprite
  20. class GameSprite(sprite.Sprite):
  21.     def __init__(self, player_image, player_x, player_y, size_x, size_y, player_speed):
  22.         # Call for the class (Sprite) constructor:
  23.         sprite.Sprite.__init__(self)
  24.  
  25.         # Every sprite must store the image property
  26.         self.image = transform.scale(image.load(player_image), (size_x, size_y))
  27.         self.speed = player_speed
  28.  
  29.         # Every sprite must have the rect property – the rectangle it is fitted in
  30.         self.rect = self.image.get_rect()
  31.         self.rect.x = player_x
  32.         self.rect.y = player_y
  33.    
  34.     # Puts the character in the window
  35.     def reset(self):
  36.         window.blit(self.image, (self.rect.x, self.rect.y))
  37.  
  38. # Player class - inherits from GameSprite
  39. class Player(GameSprite):
  40.     # Function to control the sprite with arrow keys - only LEFT and RIGHT needed
  41.     def update(self):
  42.         keys = key.get_pressed()
  43.         if keys[K_LEFT] and self.rect.x > 5:
  44.             self.rect.x -= self.speed
  45.         if keys[K_RIGHT] and self.rect.x < win_width - 80:
  46.             self.rect.x += self.speed
  47.    
  48.     # Function to "shoot" (use the player position to create a bullet there)
  49.     def fire(self):
  50.         pass
  51.  
  52. # Create sprites
  53. ship = Player(img_hero, 5, win_height - 100, 80, 100, 10)
  54.  
  55. # The "game is over" variable
  56. # As soon as it becomes True, all sprites stop working in the main loop
  57. finish = False
  58.  
  59. # Main game loop
  60. # The game is reset by the window close button
  61. run = True
  62.  
  63. while run:
  64.     # "Close" button press event
  65.     for e in event.get():
  66.         if e.type == QUIT:
  67.             run = False
  68.  
  69.     if not finish:
  70.         # Update the background
  71.         window.blit(background,(0,0))
  72.  
  73.         # Start sprite movements
  74.         ship.update()
  75.  
  76.         # Update them in a new location in each loop iteration
  77.         ship.reset()
  78.  
  79.         display.update()
  80.    
  81.     # The loop is executed each 0.05 sec
  82.     time.delay(50)
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement