# Bruce A. Maxwell # January 2015 # # Updated January 2022 by Bruce Maxwell for Python 3 # # Pygame Tutorial Example 2 # # Creates a scene with a spider and lets the broom follow the mouse. # ####################### Setup ######################### # useful imports import sys # import pygame import pygame # initialize pygame pygame.init() # initialize the fonts try: pygame.font.init() except: print( "Fonts unavailable" ) sys.exit() # create a game clock gameClock = pygame.time.Clock() # create a screen (width, height) screen = pygame.display.set_mode( (640, 480) ) ####################### Making Content ######################### # load some images spider = pygame.image.load( "Spider.png" ).convert_alpha() broom = pygame.image.load( "Broom.png" ).convert_alpha() # create a font afont = pygame.font.SysFont( "Helvetica", 20, bold=True ) # render a surface with some text text = afont.render( "Clean up time", True, (0, 0, 0) ) ####################### Filling the Screen ######################### # A function that draws all of the static background elements def drawBkg(screen, text, refresh, rect=None): # clear the screen with white if rect == None: screen.fill( (255, 255, 255) ) # blit the text surface onto the screen screen.blit( text, (10, 10) ) refresh.append( screen.get_rect() ) else: screen.fill( (255, 255, 255), rect ) # blit the text surface onto the screen if it is inside the rectangle screen.fill( (255, 255, 255), text.get_rect().move(10, 10).clip( rect ) ) trect = text.get_rect().move(10, 10) # rectangle in which to # draw the text clippedRect = trect.clip( rect ) # intersection of the text # screen rectangle and the # area to update # blit the text into the area to update, the second rectangle # indicates which part of the text to use urect = screen.blit( text, clippedRect, clippedRect.move(-10,-10) ) # refresh the rectangle refresh.append( rect ) ############## Setting up the Broom as a sprite ################ # get the current mouse information, and make the cursor invisible if # it is focused on the game window pygame.event.pump() if pygame.mouse.get_focused(): pygame.mouse.set_visible(False) # get the mouse position and put the broom so it is centered on the # mouse location tpos = pygame.mouse.get_pos() trect = broom.get_rect() broomRect = broom.get_rect().move( tpos[0] - trect.width/2, tpos[1] - trect.height/2 ) # blit the broom to the screen and update the display screen.blit( broom, broomRect ) ####################### Main Event Loop ######################### # set up the refresh rectangle container refresh = [] drawBkg( screen, text, refresh ) # update the display before we start the main loop pygame.display.update() # reset the broom rectangle to position (0, 0) until it gets set to the mouse position in the loop broomRect = pygame.Rect( 0, 0, broomRect.width, broomRect.height ) # respond to mouse motion events until someone clicks a mouse or hits a key print( "Entering main loop" ) while 1: # handle events and erase things for event in pygame.event.get(): if event.type == pygame.MOUSEMOTION: # erase the existing broom print(broomRect) drawBkg( screen, text, refresh, broomRect ) if event.type == pygame.MOUSEBUTTONDOWN: sys.exit() if event.type == pygame.KEYDOWN: sys.exit() if event.type == pygame.QUIT: sys.exit() # draw a spider in the middle of the screen spiderRect = spider.get_rect().move( 320-spider.get_rect().width/2, 240 - spider.get_rect().height/2 ) screen.fill( (255, 255, 255), spiderRect ) urect = screen.blit( spider, spiderRect ) refresh.append(spiderRect) # always update the spider refresh.append( urect ) # If the game is in focus, draw things if pygame.mouse.get_focused(): pygame.mouse.set_visible(False) tpos = pygame.mouse.get_pos() # update the position of the cursor broomRect = pygame.Rect( (tpos[0] - broomRect.width/2, tpos[1] - broomRect.height/2), (broomRect.width, broomRect.height) ) # draw the broom in the location of the mouse screen.blit( broom, broomRect ) # add to the refresh list refresh.append( broomRect ) else: pygame.mouse.set_visible(True) # update the parts of the screen that need it pygame.display.update( refresh ) # clear out the refresh rects refresh = [] # throttle the game speed to 30fps gameClock.tick(30) # done print( "Terminating" )