Advertisement
furas

PyGame - moving player and shooting bullets

Jul 27th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | None | 0 0
  1. #
  2. # https://www.reddit.com/r/learnpython/comments/4uy0rn/new_to_python_having_problems_with_pygame_and/
  3. #
  4.  
  5. import pygame
  6.  
  7. # --- constants - Uppercase ---
  8.  
  9. # Colors used in the game
  10. BLACK   = (   0,   0,   0)
  11. WHITE   = ( 255, 255, 255)
  12. GREEN   = (   0, 255,   0)
  13. RED     = ( 255,   0,   0)
  14.  
  15. # Size of the window / open window
  16. SIZE = (700, 500)
  17.  
  18. # --- classes --- CamelCase ---
  19.  
  20. class Bullet(pygame.sprite.Sprite):
  21.    
  22.     def __init__(self, position):
  23.         super().__init__()
  24.  
  25.         self.image = pygame.Surface([4,10])
  26.         self.image.fill(WHITE)
  27.        
  28.         self.rect = self.image.get_rect()
  29.         self.rect.center = position
  30.  
  31.     def update(self):
  32.         self.rect.y -= 5
  33.  
  34.  
  35. class Player(pygame.sprite.Sprite):
  36.  
  37.     def __init__(self):
  38.         super().__init__()
  39.  
  40.         self.image = pygame.image.load("player1.png").convert()
  41.         # Make color transparent
  42.         self.image.set_colorkey(BLACK)
  43.  
  44.         self.rect = self.image.get_rect()
  45.  
  46.     def draw(self, screen):
  47.         screen.blit(self.image, self.rect)
  48.        
  49.     def move(self, position):
  50.         self.rect.center = position
  51.        
  52.  
  53. # --- main ---
  54.  
  55. # Setup
  56. pygame.init()
  57.  
  58. screen = pygame.display.set_mode(SIZE)
  59. screen_rect = screen.get_rect() # to center background
  60.  
  61. # Window Title
  62. pygame.display.set_caption("Andrew's Cool Game")
  63.  
  64. background_image = pygame.image.load("space1.jpg").convert()
  65. background_image_rect = background_image.get_rect(center=screen_rect.center) # to center background
  66.  
  67. #click_sound = pygame.mixer.Sound("laser5.ogg")
  68.  
  69. # Hide mouse cursor
  70. pygame.mouse.set_visible(False)
  71.  
  72. # Bullet List
  73. bullet_list = pygame.sprite.Group()
  74.  
  75. # Player
  76. player = Player()
  77.  
  78. # ---------------- MAIN PROGRAM LOOP ----------------
  79.  
  80. # Loop until user closes program
  81. done = False
  82.  
  83. # Manages how fast screen updates
  84. clock = pygame.time.Clock()
  85.  
  86. while done == False: # User did something
  87.    
  88.     # ----- PROCESSING -----
  89.    
  90.     for event in pygame.event.get(): # If user clicks close
  91.        
  92.         if event.type == pygame.QUIT:
  93.             done = True
  94.  
  95.         # I like to use ESC to close example    
  96.         elif event.type == pygame.KEYDOWN:
  97.             if event.key == pygame.K_ESCAPE:
  98.                 done = True
  99.  
  100.         # Moving player
  101.         elif event.type == pygame.MOUSEMOTION:
  102.             player.move(event.pos)
  103.            
  104.         # Play sound when mouse button is clicked
  105.         elif event.type == pygame.MOUSEBUTTONDOWN:
  106.             #click_sound.play()
  107.             print ("Testing", event.pos)
  108.            
  109.             bullet = Bullet(event.pos)
  110.  
  111.             print(bullet.rect)
  112.             bullet_list.add(bullet)
  113.  
  114.     # ----- PROCESSING END -----
  115.    
  116.  
  117.     # ----- GAME LOGIC -----
  118.  
  119.     for bullet in bullet_list:
  120.  
  121.         if bullet.rect.y < -10:
  122.             bullet_list.remove(bullet)
  123.  
  124.     bullet_list.update()
  125.    
  126.     # ----- GAME LOGIC END -----
  127.  
  128.  
  129.     # ----- DRAW -----
  130.    
  131.     screen.blit(background_image, background_image_rect) # to center background
  132.    
  133.     player.draw(screen)
  134.  
  135.     bullet_list.draw(screen)
  136.    
  137.     # Update the screen with DRAW
  138.     pygame.display.flip()
  139.    
  140.     # ----- DRAW END -----
  141.  
  142.     # Limit fps
  143.     clock.tick(60)
  144.  
  145. # --- The End ---
  146.  
  147. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement