import pygame import random # Define some colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) class Block(pygame.sprite.Sprite): """ This class represents the ball It derives from the "Sprite" class in Pygame """ def __init__(self, color, x, y): """ Constructor. Pass in the color of the block, and its x and y position. """ # Call the parent class (Sprite) constructor super().__init__() # Create an image of the block, and fill it with a color. # This could also be an image loaded from the disk. self.image = pygame.Surface([20, 20]) self.image.fill(color) #self.width=100 #self.height=100 # Fetch the rectangle object that has the dimensions of the image # image. # Update the position of this object by setting the values # of rect.x and rect.y self.rect = self.image.get_rect() self.rect.x=x self.rect.y=y # If block is too far down, reset to top of screen. def reset_pos(self): """ Reset position to the top of the screen, at a random x location. Called by update() or the main program loop if there is a collision. """ self.rect.y = random.randrange(-300, -20) self.rect.x = random.randrange(0, screen_width) def update(self): # Move block down one pixel self.rect.y += 0.05 # If block is too far down, reset to top of screen. if self.rect.y > 600: self.rect.y=100 class Player(pygame.sprite.Sprite): """ This class represents the Player. """ def __init__(self): """ Set up the player on creation. """ # Call the parent class (Sprite) constructor super().__init__() self.image = pygame.Surface([20, 20]) self.image.fill(RED) self.rect = self.image.get_rect() def update(self): """ Update the player's position. """ # Get the current mouse position. This returns the position # as a list of two numbers. pos = pygame.mouse.get_pos() # Set the player x position to the mouse x position self.rect.x = pos[0] class Bullet(pygame.sprite.Sprite): """ This class represents the bullet . """ def __init__(self): # Call the parent class (Sprite) constructor super().__init__() self.image = pygame.Surface([4, 10]) self.image.fill(BLACK) self.rect = self.image.get_rect() def update(self): """ Move the bullet. """ self.rect.y -= 11 # Initialize Pygame pygame.init() # Set the height and width of the screen screen_width = 1000 screen_height = 800 screen = pygame.display.set_mode([screen_width, screen_height]) # This is a list of 'sprites.' Each block in the program is # added to this list. The list is managed by a class called 'Group.' block_list = pygame.sprite.Group() # This is a list of every sprite. All blocks and the player block as well. all_sprites_list = pygame.sprite.Group() bullet_list = pygame.sprite.Group() count=2 for i in range(100, 400, 50): for x in range(100, 900, 100): # This represents a block if count%2==0: block = Block(GREEN, x, i) else: x=x+50 print(x) block = Block(GREEN, x, i) # Set a random location for the block # Add the block to the list of objects block_list.add(block) all_sprites_list.add(block) count+=1 # Create a red player block #player = Player(RED, 20, 15) #all_sprites_list.add(player) # Loop until the user clicks the close button. player = Player() all_sprites_list.add(player) # Loop until the user clicks the close button. done = False # Used to manage how fast the screen updates clock = pygame.time.Clock() score = 0 player.rect.y = 780 # -------- Main Program Loop ----------- while not done: # --- Event Processing for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.MOUSEBUTTONDOWN: # Fire a bullet if the user clicks the mouse button bullet = Bullet() # Set the bullet so it is where the player is bullet.rect.x = player.rect.x bullet.rect.y = player.rect.y # Add the bullet to the lists all_sprites_list.add(bullet) bullet_list.add(bullet) # --- Game logic # Call the update() method on all the sprites all_sprites_list.update() block.update() # Calculate mechanics for each bullet for bullet in bullet_list: # See if it hit a block block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True) # For each block hit, remove the bullet and add to the score for block in block_hit_list: bullet_list.remove(bullet) all_sprites_list.remove(bullet) score += 1 print(score) # Remove the bullet if it flies up off the screen if bullet.rect.y < -10: bullet_list.remove(bullet) all_sprites_list.remove(bullet) # --- Draw a frame # Clear the screen screen.fill(WHITE) # Draw all the spites all_sprites_list.draw(screen) # Go ahead and update the screen with what we've drawn. pygame.display.flip() # --- Limit to 20 frames per second clock.tick(20) pygame.quit()