import sys import pygame from pygame import * from pygame.locals import * from pygame.sprite import * from random import * class Mole(Sprite): # constructor def __init__(self): Sprite.__init__(self) self.image = image.load("mole.gif") self.rect = self.image.get_rect() #move to a new random position def flee(self): randX = randint(0, 600) randY = randint(0, 400) self.rect.center = (randX, randY) #main pygame.init() mole = Mole() sprites = Group(mole) # header for the window display.set_caption("Whack-a-mole") screen = display.set_mode((640, 480)) while True: ev = event.wait() if ev.type == QUIT: pygame.quit() break # mole moves positions when clicked on elif ev.type == MOUSEBUTTONDOWN: if mole.rect.collidepoint(mouse.get_pos()): mole.flee() screen.fill((255, 255, 255)) sprites.update() sprites.draw(screen) display.update()