Facebook
From Ozan, 3 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 105
  1. import pygame
  2. import random
  3.  
  4. # Define some colors
  5. BLACK = (0, 0, 0)
  6. WHITE = (255, 255, 255)
  7. GREEN = (0, 255, 0)
  8. RED = (255, 0, 0)
  9.  
  10.  
  11. class Block(pygame.sprite.Sprite):
  12.     """
  13.    This class represents the ball
  14.    It derives from the "Sprite" class in Pygame
  15.    """
  16.     def __init__(self, color, x, y):
  17.         """ Constructor. Pass in the color of the block,
  18.        and its x and y position. """
  19.         # Call the parent class (Sprite) constructor
  20.         super().__init__()
  21.  
  22.         # Create an image of the block, and fill it with a color.
  23.         # This could also be an image loaded from the disk.
  24.         self.image = pygame.Surface([20, 20])
  25.         self.image.fill(color)
  26.        
  27.         #self.width=100
  28.         #self.height=100
  29.  
  30.         # Fetch the rectangle object that has the dimensions of the image
  31.         # image.
  32.         # Update the position of this object by setting the values
  33.         # of rect.x and rect.y
  34.         self.rect = self.image.get_rect()
  35.         self.rect.x=x
  36.         self.rect.y=y
  37.        
  38.  
  39.          # If block is too far down, reset to top of screen.
  40.        
  41.  
  42.     def reset_pos(self):
  43.         """ Reset position to the top of the screen, at a random x location.
  44.        Called by update() or the main program loop if there is a collision.
  45.        """
  46.         self.rect.y = random.randrange(-300, -20)
  47.         self.rect.x = random.randrange(0, screen_width)
  48.  
  49.     def update(self):        
  50.  
  51.          # Move block down one pixel
  52.          self.rect.y += 0.05
  53.  
  54.          # If block is too far down, reset to top of screen.
  55.          if self.rect.y > 600:
  56.              self.rect.y=100
  57.  
  58.  
  59. class Player(pygame.sprite.Sprite):
  60.     """ This class represents the Player. """
  61.  
  62.     def __init__(self):
  63.         """ Set up the player on creation. """
  64.         # Call the parent class (Sprite) constructor
  65.         super().__init__()
  66.  
  67.         self.image = pygame.Surface([20, 20])
  68.         self.image.fill(RED)
  69.  
  70.         self.rect = self.image.get_rect()
  71.        
  72.  
  73.     def update(self):
  74.         """ Update the player's position. """
  75.         # Get the current mouse position. This returns the position
  76.         # as a list of two numbers.
  77.         pos = pygame.mouse.get_pos()
  78.  
  79.         # Set the player x position to the mouse x position
  80.         self.rect.x = pos[0]
  81.  
  82.  
  83. class Bullet(pygame.sprite.Sprite):
  84.     """ This class represents the bullet . """
  85.     def __init__(self):
  86.         # Call the parent class (Sprite) constructor
  87.         super().__init__()
  88.  
  89.         self.image = pygame.Surface([4, 10])
  90.         self.image.fill(BLACK)
  91.  
  92.         self.rect = self.image.get_rect()
  93.        
  94.     def update(self):
  95.         """ Move the bullet. """
  96.         self.rect.y -= 11
  97.    
  98.  
  99. # Initialize Pygame
  100. pygame.init()
  101.  
  102. # Set the height and width of the screen
  103. screen_width = 1000
  104. screen_height = 800
  105. screen = pygame.display.set_mode([screen_width, screen_height])
  106.  
  107. # This is a list of 'sprites.' Each block in the program is
  108. # added to this list. The list is managed by a class called 'Group.'
  109. block_list = pygame.sprite.Group()
  110.  
  111. # This is a list of every sprite. All blocks and the player block as well.
  112. all_sprites_list = pygame.sprite.Group()
  113.  
  114. bullet_list = pygame.sprite.Group()
  115.  
  116. count=2
  117. for i in range(100, 400, 50):
  118.    
  119.     for x in range(100, 900, 100):
  120.     # This represents a block
  121.         if count%2==0:
  122.            
  123.             block = Block(GREEN, x, i)
  124.         else:
  125.             x=x+50
  126.             print(x)
  127.             block = Block(GREEN, x, i)
  128.          
  129.         # Set a random location for the block
  130.        
  131.      
  132.         # Add the block to the list of objects
  133.         block_list.add(block)
  134.         all_sprites_list.add(block)
  135.     count+=1
  136.  
  137. # Create a red player block
  138. #player = Player(RED, 20, 15)
  139. #all_sprites_list.add(player)
  140.  
  141. # Loop until the user clicks the close button.
  142. player = Player()
  143. all_sprites_list.add(player)
  144.  
  145. # Loop until the user clicks the close button.
  146. done = False
  147.  
  148. # Used to manage how fast the screen updates
  149. clock = pygame.time.Clock()
  150.  
  151. score = 0
  152. player.rect.y = 780
  153.  
  154. # -------- Main Program Loop -----------
  155. while not done:
  156.     # --- Event Processing
  157.     for event in pygame.event.get():
  158.         if event.type == pygame.QUIT:
  159.             done = True
  160.  
  161.         elif event.type == pygame.MOUSEBUTTONDOWN:
  162.             # Fire a bullet if the user clicks the mouse button
  163.             bullet = Bullet()
  164.             # Set the bullet so it is where the player is
  165.             bullet.rect.x = player.rect.x
  166.             bullet.rect.y = player.rect.y
  167.             # Add the bullet to the lists
  168.             all_sprites_list.add(bullet)
  169.             bullet_list.add(bullet)
  170.  
  171.     # --- Game logic
  172.  
  173.     # Call the update() method on all the sprites
  174.     all_sprites_list.update()
  175.     block.update()
  176.  
  177.     # Calculate mechanics for each bullet
  178.     for bullet in bullet_list:
  179.  
  180.         # See if it hit a block
  181.         block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
  182.  
  183.         # For each block hit, remove the bullet and add to the score
  184.         for block in block_hit_list:
  185.             bullet_list.remove(bullet)
  186.             all_sprites_list.remove(bullet)
  187.             score += 1
  188.             print(score)
  189.  
  190.         # Remove the bullet if it flies up off the screen
  191.         if bullet.rect.y < -10:
  192.             bullet_list.remove(bullet)
  193.             all_sprites_list.remove(bullet)
  194.  
  195.     # --- Draw a frame
  196.  
  197.     # Clear the screen
  198.     screen.fill(WHITE)
  199.  
  200.     # Draw all the spites
  201.     all_sprites_list.draw(screen)
  202.  
  203.     # Go ahead and update the screen with what we've drawn.
  204.     pygame.display.flip()
  205.  
  206.     # --- Limit to 20 frames per second
  207.     clock.tick(20)
  208.  
  209. pygame.quit()
  210.