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 | """ Example program to show how to do an instruction screen. 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 ) pygame.init() # Set the height and width of the screen size = [ 700 , 500 ] screen = pygame.display.set_mode(size) pygame.display.set_caption( "Instruction Screen" ) # Loop until the user clicks the close button. done = False # Used to manage how fast the screen updates clock = pygame.time.Clock() # Starting position of the rectangle rect_x = 50 rect_y = 50 # Speed and direction of rectangle rect_change_x = 5 rect_change_y = 5 # This is a font we use to draw text on the screen (size 36) font = pygame.font.Font( None , 36 ) display_instructions = True instruction_page = 1 # -------- Instruction Page Loop ----------- while not done and display_instructions: for event in pygame.event.get(): if event. type = = pygame.QUIT: done = True if event. type = = pygame.MOUSEBUTTONDOWN: instruction_page + = 1 if instruction_page = = 3 : display_instructions = False # Set the screen background screen.fill(BLACK) if instruction_page = = 1 : # Draw instructions, page 1 # This could also load an image created in another program. # That could be both easier and more flexible. text = font.render( "Instructions" , True , WHITE) screen.blit(text, [ 10 , 10 ]) text = font.render( "Page 1" , True , WHITE) screen.blit(text, [ 10 , 40 ]) if instruction_page = = 2 : # Draw instructions, page 2 text = font.render( "This program bounces a rectangle" , True , WHITE) screen.blit(text, [ 10 , 10 ]) text = font.render( "Page 2" , True , WHITE) screen.blit(text, [ 10 , 40 ]) # Limit to 60 frames per second clock.tick( 60 ) # Go ahead and update the screen with what we've drawn. pygame.display.flip() # -------- Main Program Loop ----------- while not done: for event in pygame.event.get(): if event. type = = pygame.QUIT: done = True # Set the screen background screen.fill(BLACK) # Draw the rectangle pygame.draw.rect(screen, WHITE, [rect_x, rect_y, 50 , 50 ]) # Move the rectangle starting point rect_x + = rect_change_x rect_y + = rect_change_y # Bounce the ball if needed if rect_y > 450 or rect_y < 0 : rect_change_y = rect_change_y * - 1 if rect_x > 650 or rect_x < 0 : rect_change_x = rect_change_x * - 1 # Limit to 60 frames per second clock.tick( 60 ) # Go ahead and update the screen with what we've drawn. pygame.display.flip() # Be IDLE friendly. If you forget this line, the program will 'hang' # on exit. pygame.quit() |