Esempio n. 1
0
def main():

    # create game
    pygame.init()
    board = np.zeros([20, 15])
    screen_size = (board.shape[0] * 30, board.shape[1] * 30)

    # create screen
    DISPLAY = pygame.display.set_mode(screen_size, 0, 32)
    DISPLAY.fill(helpers.BLACK)

    # create board and randomly place food
    env = Board(DISPLAY, board)

    # draw environment
    env.draw()

    # hyperparameters
    num_episodes = 100
    batch_size = 4  #32
    discount_factor = 0.8
    learn_rate = 1e-3
    memory = ReplayMemory(1000)
    num_hidden = 128
    df = 8

    # create model
    model = QNetwork(num_hidden)

    # train
    episode_durations = run_episodes(train, model, memory, env, num_episodes,
                                     batch_size, discount_factor, learn_rate,
                                     df)
Esempio n. 2
0
def run(board: Board, clock):
    snake = Snake(board.start, length=2)
    food = random_food(board, exclude=snake.squares)
    while True:
        board.fill('black')
        handle_events(snake)
        if food_consumed(snake, food):
            food = random_food(board, exclude=snake.squares)
            snake.grow()
        if overlaps(snake):
            board.draw(snake, color='green')
        else:
            board.draw(snake, color='white')
        board.draw(food, color='red')
        pygame.display.update()
        clock.tick(60)