コード例 #1
0
ファイル: main.py プロジェクト: jphan345/SnakeGame
def draw_board(dis: pygame.display, size: int, my_snake: Snake, my_snack: Snack):
    dis.fill((30, 32, 40))  # dark blue

    snake_pos_list = my_snake.get_pos()
    snack_pos = my_snack.get_pos()

    increment = min(display.get_size()) / size

    for y in range(size):
        for x in range(size):
            tile_size = increment - 2
            tile_pos = (x * increment, (-y + size - 1) * increment)
            tile = Tile(dis, tile_size, tile_pos)

            if (x, y) in snake_pos_list:
                tile.change_state('SNAKE')
            elif (x, y) == snack_pos:
                tile.change_state('SNACK')
            else:
                tile.change_state('BLANK')
            tile.draw()

    pygame.display.update()
コード例 #2
0
ファイル: main.py プロジェクト: jphan345/SnakeGame
                elif event.key == pygame.K_DOWN:
                    directions.append("DOWN")
                elif event.key == pygame.K_RIGHT:
                    directions.append("RIGHT")
                elif event.key == pygame.K_LEFT:
                    directions.append("LEFT")

        while len(directions) != 0 and directions[0] == snake.get_direction():
            directions.pop(0)

        if len(directions) != 0:
            snake.change_direction(directions[0])
            directions.pop(0)
        snake.move()

        if snake.get_head() == snack.get_pos():
            snake.grow()
            snack.change_pos(snake.get_pos())

        if snake.touching_self() or snake.touching_edge():
            running = False

        draw_board(display, board_size, snake, snack)

    # print_game(snake, snack, board_size)
    #
    # running = True
    # while running:
    #     action = input('Input action: ')
    #     action = action.strip().upper()
    #     if action == 'LEFT':