def main(): global FPSCLOCK, DISPLAYSURF, GEMIMAGES, GAMESOUNDS, BASICFONT, BOARDRECTS # Initial set up. pygame.init() FPSCLOCK = pygame.time.Clock() DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) pygame.display.set_caption('Gemgem') BASICFONT = pygame.font.Font('freesansbold.ttf', 36) # Load the images GEMIMAGES = [scale_image(x,GEMSIZE) for x in load_images("gem[0-9].png")] if len(GEMIMAGES)< 5: raise AssertionError("game needs at least 5 types of gems to work, only %d were found"%len(GEMIMAGES)) # Load the sounds. GAMESOUNDS = {} GAMESOUNDS['bad swap'] = load_sound('badswap.wav') GAMESOUNDS['match'] = load_sounds('match[0-9].wav') # Create pygame.Rect objects for each board space to # do board-coordinate-to-pixel-coordinate conversions. BOARDRECTS = [] for x in range(BOARDWIDTH): BOARDRECTS.append([]) for y in range(BOARDHEIGHT): r = pygame.Rect((XMARGIN + (x * GEMIMAGESIZE), YMARGIN + (y * GEMIMAGESIZE), GEMIMAGESIZE, GEMIMAGESIZE)) BOARDRECTS[x].append(r) while True: runGame()
def main(): global FPSCLOCK, DISPLAYSURF, BASICFONT, BEEPS pygame.init() FPSCLOCK = pygame.time.Clock() DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) pygame.display.set_caption('Simulate') BASICFONT = pygame.font.Font('freesansbold.ttf', 16) infoSurf = BASICFONT.render('Match the pattern by clicking on the button or using the Q, W, A, S keys.', 1, DARKGRAY) infoRect = infoSurf.get_rect() infoRect.topleft = (10, WINDOWHEIGHT - 25) # load the sound files BEEPS = load_sounds("beep[0-9].ogg") # Initialize some variables for a new game pattern = [] # stores the pattern of colors currentStep = 0 # the color the player must push next lastClickTime = 0 # timestamp of the player's last button push score = 0 # when False, the pattern is playing. when True, waiting for the player to click a colored button: waitingForInput = False while True: # main game loop clickedButton = None # button that was clicked (set to YELLOW, RED, GREEN, or BLUE) DISPLAYSURF.fill(bgColor) drawButtons() scoreSurf = BASICFONT.render('Score: ' + str(score), 1, WHITE) scoreRect = scoreSurf.get_rect() scoreRect.topleft = (WINDOWWIDTH - 100, 10) DISPLAYSURF.blit(scoreSurf, scoreRect) DISPLAYSURF.blit(infoSurf, infoRect) checkForQuit() for event in pygame.event.get(): # event handling loop if event.type == MOUSEBUTTONUP: mousex, mousey = event.pos clickedButton = getButtonClicked(mousex, mousey) elif event.type == KEYDOWN: if event.key == K_q: clickedButton = YELLOW elif event.key == K_w: clickedButton = BLUE elif event.key == K_a: clickedButton = RED elif event.key == K_s: clickedButton = GREEN if not waitingForInput: # play the pattern pygame.display.update() pygame.time.wait(1000) pattern.append(random.choice((YELLOW, BLUE, RED, GREEN))) for button in pattern: flashButtonAnimation(button) pygame.time.wait(FLASHDELAY) waitingForInput = True else: # wait for the player to enter buttons if clickedButton and clickedButton == pattern[currentStep]: # pushed the correct button flashButtonAnimation(clickedButton) currentStep += 1 lastClickTime = time.time() if currentStep == len(pattern): # pushed the last button in the pattern changeBackgroundAnimation() score += 1 waitingForInput = False currentStep = 0 # reset back to first step elif (clickedButton and clickedButton != pattern[currentStep]) or (currentStep != 0 and time.time() - TIMEOUT > lastClickTime): # pushed the incorrect button, or has timed out gameOverAnimation() # reset the variables for a new game: pattern = [] currentStep = 0 waitingForInput = False score = 0 pygame.time.wait(1000) changeBackgroundAnimation() pygame.display.update() FPSCLOCK.tick(FPS)