# Look At This Rock 2: A Different Rock # By Al Sweigart # (C) 1973 Dynavid Untersoft Inc. # For a free 87-page catalog of all of Dynavid Untersoft Inc.'s exciting games, # send a self-addressed stamped envelope to: # Dynavid Untersoft Inc. # PO BOX 10110 # San Burntington, CA 94130 # http://inventwithpython.com/lookatthisrock # Photo credit: Steve Parker http://www.flickr.com/photos/sparker/6171897798/ import pygame, sys from pygame.locals import * pygame.init() FPS = 20 fpsClock = pygame.time.Clock() # set up the window WINDOWSURF = pygame.display.set_mode((400, 550), 0, 32) pygame.display.set_caption('Look At This Rock 2: A Different Rock') WHITE = (255, 255, 255) BLUE = (0, 0, 128) rockImg = pygame.image.load('rock2.png') fontObj = pygame.font.Font('freesansbold.ttf', 14) score = 0 dontClickMessageCounter = 0 dontClickSurf = fontObj.render('Please do not click on the rock.', True, BLUE) dontClickRect = dontClickSurf.get_rect() dontClickRect.bottom = 545 dontClickRect.centerx = 200 while True: # the main game loop WINDOWSURF.fill(WHITE) WINDOWSURF.blit(rockImg, (10, 10)) textSurfaceObj = fontObj.render('Score: ' + str(score), True, BLUE) textRectObj = textSurfaceObj.get_rect() textRectObj.bottomright = (390, 290) WINDOWSURF.blit(textSurfaceObj, textRectObj) if dontClickMessageCounter > 0: WINDOWSURF.blit(dontClickSurf, dontClickRect) dontClickMessageCounter += 1 if dontClickMessageCounter == 60: dontClickMessageCounter = 0 for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): pygame.quit() sys.exit() elif event.type == MOUSEBUTTONDOWN and pygame.Rect(10, 10, rockImg.get_width(), rockImg.get_height()).collidepoint(event.pos): pixArr = pygame.PixelArray(rockImg) if rockImg.unmap_rgb(pixArr[event.pos[0] - 10][event.pos[1] - 10]).a != 0: dontClickMessageCounter = 1 del pixArr pygame.display.update() fpsClock.tick(FPS)