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 | # Sample Python/Pygame Programs # Simpson College Computer Science import pygame # Define some colors BLACK = ( 0 , 0 , 0 ) WHITE = ( 255 , 255 , 255 ) GREEN = ( 0 , 255 , 0 ) RED = ( 255 , 0 , 0 ) BLUE = ( 50 , 50 , 255 ) DKGREEN = ( 0 , 100 , 0 ) # This class represents the player # It derives from the "Sprite" class in Pygame class Player(pygame.sprite.Sprite): # Constructor. Pass in the color of the block, and its x and y position def __init__( self ): # Call the parent class (Sprite) constructor super ().__init__() # Variables to hold the height and width of the block width = 20 height = 15 # Create an image of the player, 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(BLACK) # Fetch the rectangle object that has the dimensions of the image self .rect = self .image.get_rect() # Update the position of the player 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. # NOTE: If you want to keep the mouse at the bottom of the screen, just # set y = 380, and not update it with the mouse position stored in # pos[1] x = pos[ 0 ] y = pos[ 1 ] # Set the attribute for the top left corner where this object is # located self .rect.x = x self .rect.y = y pygame.init() # Set the height and width of the screen size = [ 700 , 500 ] screen = pygame.display.set_mode(size) # Don't display the mouse pointer pygame.mouse.set_visible( False ) # Loop until the user clicks the close button. done = False # Used to manage how fast the screen updates clock = pygame.time.Clock() # This is a list of 'sprites.' Each sprite in the program (there is only 1) is # added to this list. The list is managed by a class called 'Group.' all_sprites_list = pygame.sprite.Group() # This represents the ball controlled by the player player = Player() # Add the ball to the list of player-controlled objects all_sprites_list.add(player) # -------- Main Program Loop ----------- while not done: for event in pygame.event.get(): if event. type = = pygame.QUIT: done = True # --- Game logic all_sprites_list.update() # --- Display / Drawing code # Clear the screen screen.fill(WHITE) # Update the position of the ball (using the mouse) and draw the ball all_sprites_list.draw(screen) # Limit to 60 frames per second clock.tick( 60 ) # Go ahead and update the screen with what we've drawn. pygame.display.flip() pygame.quit() |