def playGame(): game = Tetris(numColors=MAXCOLORS, numColumns=10, numRows=10) ai = AI(function) # loop count variables: numTicks = 0 while not game.lost: # game loop ends when game is lost ai.ai(game, display=False) game.incrementTime() numTicks += 1 return game.numLines
def playGame(): game = Tetris(numColors=MAXCOLORS) ai = AI(function) # loop count variables: numPieces = 0 sumHeights = 0 while not game.lost and game.numPieces < 200: # game loop ends when game is lost ai.ai(game) game.incrementTime() if game.numPieces > numPieces: sumHeights += height(game) numPieces += 1 return (game.numLines, sumHeights / numPieces)
def playScreen(window): # Play screen: game = Tetris(20, 10) display(game, window) counter = time.time() delay = 1 acceleration = 1 while 1: try: key = window.getkey() if key == 'q': quit() if key == 's' or key == 'KEY_DOWN': game.incrementTime() if key == 'a' or key == 'KEY_LEFT': game.translateActiveLeft() if key == 'd' or key == 'KEY_RIGHT': game.translateActiveRight() if key == 'w' or key == 'KEY_UP': game.rotateActiveClockwise() if key == ' ': game.hardDrop() if key in ['{}'.format(i) for i in range(8)]: if key == '0': game.autoChoice = True game.next = randint(1, 7) else: game.autoChoice = False game.next = int(key) display(game, window) except Exception as e: # No input current = time.time() if counter + delay < current: game.incrementTime() display(game, window) counter = current delay = 10**(5 - float(acceleration)) / ( game.numTurns + 10**(5 - float(acceleration)))
def playGame(): game = Tetris(numColors=MAXCOLORS) ai = AI() # a dictionary of pygame buttons and their functions: global AI_BUTTON AI_BUTTON = pygame.Rect( (WINDOWWIDTH - 122, 300, 50, 30)) # ai enable button render(game) # loop count variables: numTicks = 0 timeSinceIncrement = 0 pressedKeys = [-1, -1, -1, -1] # up, down, left, right while not game.lost: # game loop ends when game is lost if INPUT: handleInput(game, pressedKeys, numTicks) else: for event in pygame.event.get(): # event handling loop if event.type == pygame.QUIT: terminate() # exit game # increment time (move blocks down) if timeSinceIncrement > DELAY: # number of ticks between time increments if not INPUT: ai.ai(game) game.incrementTime() timeSinceIncrement = 0 render(game) FPSCLOCK.tick(FPS) numTicks += 1 timeSinceIncrement += 1 return "Game Over"
def playGame(): seed = int(time.time()) game = Tetris(numColors=MAXCOLORS, seed=seed) aiGame = Tetris(numColors=MAXCOLORS, seed=seed) ai = AI() render(game, aiGame) DELAY = 20 # delay between each incrementTime # loop count variables: numTicks = 0 timeSinceIncrement = 0 level = 0 pressedKeys = [-1, -1, -1, -1] # up, down, left, right while not game.lost or aiGame.lost: # game loop ends when game is lost updated = handleInput(game, pressedKeys, numTicks) while aiGame.numTurns < game.numTurns: ai.ai(aiGame) aiGame.incrementTime() if game.numTurns - aiGame.numTurns % 10 == 0 or game.numTurns - aiGame.numTurns % 5 < 10: render(game, aiGame) if updated: render(game, aiGame) if timeSinceIncrement > DELAY: # number of ticks between time increments game.incrementTime() timeSinceIncrement = 0 render(game, aiGame) if game.numLines // 10 > level: level = game.numLines // 10 DELAY = DELAY * 0.8 FPSCLOCK.tick(FPS) numTicks += 1 timeSinceIncrement += 1 return "Game Over" if game.lost else "You Win!"