예제 #1
0
파일: gui_game.py 프로젝트: yzhan189/Snake
def single_ai_mode():
    # set up local player data
    addPlayerData(player_name)

    screen_size = [700, 700]
    pygame.init()
    fpsClock = pygame.time.Clock()
    # create pyGame screen
    playSurface = pygame.display.set_mode((screen_size[0], screen_size[1]))
    my_board = Board(50, 50)
    my_snake = Snake(50, 50)

    # set up food and snake
    food_loc = [my_board.food]
    draw_surface(playSurface, redColour, [my_board.food], 10,
                 100)  # draw first food
    pygame.display.set_caption('Food Snake')
    total_score = 0
    score = 0
    level = 1
    isEnd = False

    while True:
        # give AI input, and let AI control snake
        dir = AI1(my_board, my_snake)
        my_snake.turn(dir)

        # catch event and control snake
        eventHelper(my_snake, False)

        # increase along the moving direction
        draw_surface(playSurface, blackColour, my_snake.get_cells(), 10, 100)
        status = my_snake.tick(my_board.food, [])
        my_snake.teleport_wall()
        my_new_cells = my_snake.get_cells()

        if my_snake.is_dead([]) or isEnd:
            break

        # When snake eats the food
        if status == 1:
            score += my_board.foodWeight
            total_score += 1
            my_board.new_food(my_new_cells, my_snake.obstacles, [])
            food_loc = [my_board.food]

            if score - level > level / 2:
                level += 1
            if level % 2 == 0:
                my_snake.add_obstcles(level)
            if level % 3 == 0:
                my_snake.speed += 1

        displayHelper(playSurface, fpsClock, level, food_loc, my_snake,
                      total_score, player_name)

    game_end(total_score)
예제 #2
0
def duo_ai_mode():
    global player1_name, player2_name, avatar1_image, avatar2_image
    init_name()
    final_score1 = 0
    player1_name = player_name
    avatar1_image = avatar_image
    addPlayerData(player_name)

    init_name()
    final_score2 = 0
    player2_name = player_name
    avatar2_image = avatar_image
    addPlayerData(player2_name)

    screen_size = [700, 700]
    pygame.init()
    fpsClock = pygame.time.Clock()
    # create pyGame screen
    level = 1
    playSurface = pygame.display.set_mode((screen_size[0], screen_size[1]))
    my_board = Board(50, 50)
    my_snake = Snake(50, 50)
    op_snake = Snake(50, 50)
    op_snake.obstacles = my_snake.obstacles

    # set up food and snake
    food_loc = [my_board.food]
    draw_surface(playSurface, redColour, [my_board.food], 10,
                 100)  # draw first food
    pygame.display.set_caption('Food Snake')
    total_score = 0
    score = 0
    isEnd = False

    # check input and go as the direction
    while True:
        # give AI input, and let AI control snake
        dir = AI1(my_board, my_snake)
        my_snake.turn(dir)

        # op is pure AI
        dir = AI12(my_board, op_snake)
        op_snake.turn(dir)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quitHelper()

            elif event.type == pygame.KEYDOWN:
                # determine the event of keyBoard
                if event.key == pygame.K_RIGHT or event.key == ord('d'):
                    my_snake.turn(RIGHT)
                if event.key == pygame.K_LEFT or event.key == ord('a'):
                    my_snake.turn(LEFT)
                if event.key == pygame.K_UP or event.key == ord('w'):
                    my_snake.turn(UP)
                if event.key == pygame.K_DOWN or event.key == ord('s'):
                    my_snake.turn(DOWN)
                if event.key == ord('q'):
                    isEnd = True
                if event.key == pygame.K_ESCAPE:
                    pygame.event.post(pygame.event.Event(pygame.QUIT))

        # increase along the moving direction
        draw_surface(playSurface, blackColour, my_snake.get_cells(), 10, 100)
        status1 = my_snake.tick(my_board.food, [])
        my_snake.teleport_wall()

        # increase along the moving direction
        draw_surface(playSurface, blackColour, op_snake.get_cells(), 10, 100)
        status2 = op_snake.tick(my_board.food, [])
        op_snake.teleport_wall()

        my_new_cells = my_snake.get_cells()
        op_new_cells = op_snake.get_cells()

        if my_snake.is_dead(op_new_cells) or op_snake.is_dead(
                my_new_cells) or isEnd:
            break

        # When my snake eats the food
        if status1 == 1 or status2 == 1:
            score += my_board.foodWeight
            if status1 == 1:
                final_score1 += 1
            else:
                final_score2 += 1
            my_board.new_food(my_new_cells, my_snake.obstacles, op_new_cells)
            food_loc = [my_board.food]
            if score - level > level / 2:
                level += 1
            if level % 2 == 0:
                my_snake.add_obstcles(level)
            if level % 3 == 0:
                my_snake.speed += 1

        playSurface.fill(blackColour)
        pygame.draw.polygon(playSurface, greenColour,
                            [[99, 99], [99, 601], [601, 601], [601, 99]], 1)
        show_message(playSurface, 'Score: ' + str(total_score), whiteColour,
                     40, 10, 10)
        show_message(playSurface, 'Level: ' + str(level), whiteColour, 40, 10,
                     50)

        screen.blit(avatar1_image, (20, 615))
        show_message(playSurface, player1_name, whiteColour, 40, 100, 630)
        screen.blit(avatar2_image, (20, 650))
        show_message(playSurface, player2_name, whiteColour, 40, 100, 665)

        draw_surface(playSurface, redColour, food_loc, 10, 100)
        draw_surface(playSurface, greenColour, my_snake.obstacles, 10, 100)

        draw_surface(playSurface, whiteColour, my_new_cells, 10, 100)
        draw_surface(playSurface, greyColour, op_new_cells, 10, 100)
        pygame.display.flip()
        # speed is changeable
        fpsClock.tick(my_snake.speed)
    game_end(final_score1, final_score2)
예제 #3
0
파일: gui_game.py 프로젝트: yzhan189/Snake
def duo_ai_mode(two_AI):
    global player1_name, player2_name, avatar1_image, avatar2_image
    init_name()
    final_score1 = 0
    player1_name = player_name
    avatar1_image = avatar_image
    addPlayerData(player_name)

    # init_name()
    final_score2 = 0
    player2_name = player_name
    avatar2_image = avatar_image
    addPlayerData(player2_name)

    screen_size = [700, 700]
    pygame.init()
    fpsClock = pygame.time.Clock()
    # create pyGame screen
    level = 1
    playSurface = pygame.display.set_mode((screen_size[0], screen_size[1]))
    my_board = Board(50, 50)
    my_snake = Snake(50, 50)
    op_snake = Snake(50, 50)
    op_snake.obstacles = my_snake.obstacles

    # set up food and snake
    food_loc = [my_board.food]
    draw_surface(playSurface, redColour, [my_board.food], 10,
                 100)  # draw first food
    pygame.display.set_caption('Food Snake')
    total_score = 0
    score = 0
    isEnd = False

    # check input and go as the direction
    while True:
        # give AI input, and let AI control snake
        # o.w human VS AI
        if two_AI:
            dir = AI1(my_board, my_snake)
            my_snake.turn(dir)

        # op is pure AI
        dir = AI12(my_board, op_snake)
        op_snake.turn(dir)

        # catch event and control snake
        eventHelper(my_snake, True)

        # increase along the moving direction
        draw_surface(playSurface, blackColour, my_snake.get_cells(), 10, 100)
        status1 = my_snake.tick(my_board.food, [])
        my_snake.teleport_wall()

        # increase along the moving direction
        draw_surface(playSurface, blackColour, op_snake.get_cells(), 10, 100)
        status2 = op_snake.tick(my_board.food, [])
        op_snake.teleport_wall()

        my_new_cells = my_snake.get_cells()
        op_new_cells = op_snake.get_cells()

        # two player mode
        if my_snake.is_dead(op_new_cells) or op_snake.is_dead(
                my_new_cells) or isEnd:
            break
        # if AI is dead
        # if op_snake.is_dead(my_new_cells):
        #     score += 10
        #     op_snake = Snake(50, 50)
        #     op_snake.obstacles = my_snake.obstacles

        # When my snake eats the food
        if status1 == 1 or status2 == 1:
            score += my_board.foodWeight
            if status1 == 1:
                final_score1 += 1
            else:
                final_score2 += 1
            my_board.new_food(my_new_cells, my_snake.obstacles, op_new_cells)
            food_loc = [my_board.food]
            if score - level > level / 2:
                level += 1
            if level % 2 == 0:
                my_snake.add_obstcles(level)
            if level % 3 == 0:
                my_snake.speed += 1
        # update display
        displayHelper(playSurface, fpsClock, level, food_loc, my_snake,
                      total_score, player1_name, 601, op_new_cells)

    game_end(final_score1)