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 | """ Simple graphics demo Sample Python/Pygame Programs Simpson College Computer Science """ # Import a library of functions called 'pygame' import pygame # Initialize the game engine pygame.init() # Define some colors BLACK = ( 0 , 0 , 0 ) WHITE = ( 255 , 255 , 255 ) BLUE = ( 0 , 0 , 255 ) GREEN = ( 0 , 255 , 0 ) RED = ( 255 , 0 , 0 ) PI = 3.141592653 # Set the height and width of the screen size = ( 400 , 500 ) screen = pygame.display.set_mode(size) pygame.display.set_caption( "Rotate Text" ) # Loop until the user clicks the close button. done = False clock = pygame.time.Clock() text_rotate_degrees = 0 # Loop as long as done == False while not done: for event in pygame.event.get(): # User did something if event. type = = pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop # All drawing code happens after the for loop and but # inside the main while not done loop. # Clear the screen and set the screen background screen.fill(WHITE) # Draw some borders pygame.draw.line(screen, BLACK, [ 100 , 50 ], [ 200 , 50 ]) pygame.draw.line(screen, BLACK, [ 100 , 50 ], [ 100 , 150 ]) # Select the font to use, size, bold, italics font = pygame.font.SysFont( 'Calibri' , 25 , True , False ) # Sideways text text = font.render( "Sideways text" , True , BLACK) text = pygame.transform.rotate(text, 90 ) screen.blit(text, [ 0 , 0 ]) # Sideways text text = font.render( "Upside down text" , True , BLACK) text = pygame.transform.rotate(text, 180 ) screen.blit(text, [ 30 , 0 ]) # Flipped text text = font.render( "Flipped text" , True , BLACK) text = pygame.transform.flip(text, False , True ) screen.blit(text, [ 30 , 20 ]) # Animated rotation text = font.render( "Rotating text" , True , BLACK) text = pygame.transform.rotate(text, text_rotate_degrees) text_rotate_degrees + = 1 screen.blit(text, [ 100 , 50 ]) # Go ahead and update the screen with what we've drawn. # This MUST happen after all the other drawing commands. pygame.display.flip() # This limits the while loop to a max of 60 times per second. # Leave this out and we will use all CPU we can. clock.tick( 60 ) # Be IDLE friendly pygame.quit() |