1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | """ Sample Python/Pygame Programs Simpson College Computer Science Explanation video: http://youtu.be/qbEEcQXw8aw """ 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, width, height): """ 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([width, height]) self .image.fill(color) # 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() 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 ): """ Called each frame. """ # Move block down one pixel self .rect.y + = 1 # If block is too far down, reset to top of screen. if self .rect.y > 410 : self .reset_pos() class Player(Block): """ The player class derives from Block, but overrides the 'update' functionality with new a movement function that will move the block with the mouse. """ def update( self ): # Get the current mouse position. This returns the position # as a list of two numbers. pos = pygame.mouse.get_pos() # Fetch the x and y out of the list, # just like we'd fetch letters out of a string. # Set the player object to the mouse location self .rect.x = pos[ 0 ] self .rect.y = pos[ 1 ] # Initialize Pygame pygame.init() # Set the height and width of the screen screen_width = 700 screen_height = 400 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() for i in range ( 50 ): # This represents a block block = Block(BLACK, 20 , 15 ) # Set a random location for the block block.rect.x = random.randrange(screen_width) block.rect.y = random.randrange(screen_height) # Add the block to the list of objects block_list.add(block) all_sprites_list.add(block) # Create a red player block player = Player(RED, 20 , 15 ) 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 # -------- Main Program Loop ----------- while not done: for event in pygame.event.get(): if event. type = = pygame.QUIT: done = True # Clear the screen screen.fill(WHITE) # Calls update() method on every sprite in the list all_sprites_list.update() # See if the player block has collided with anything. blocks_hit_list = pygame.sprite.spritecollide(player, block_list, False ) # Check the list of collisions. for block in blocks_hit_list: score + = 1 print (score) # Reset block to the top of the screen to fall again. block.reset_pos() # Draw all the spites all_sprites_list.draw(screen) # Limit to 20 frames per second clock.tick( 20 ) # Go ahead and update the screen with what we've drawn. pygame.display.flip() pygame.quit() |