示例#1
0
    def test_is_dead(self):

        my_snake = Snake(20, 60)
        self.assertEquals(my_snake.is_dead(), False)
        center = [ceil(my_snake.height / 2), ceil(my_snake.width / 2)]
        my_snake.cells.append([center[0], center[1]])
        self.assertEquals(my_snake.is_dead(), True)
示例#2
0
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)
示例#3
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)
示例#4
0
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
    isEnd = False

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

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quitHelper()
            elif event.type == pygame.KEYDOWN:
                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)
        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]

        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)
        screen.blit(avatar_image, (20, 615))
        show_message(playSurface, player_name, whiteColour, 40, 100, 630)
        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)
        pygame.display.flip()
        # speed is changeable
        fpsClock.tick(my_snake.speed)
    game_end(total_score)
示例#5
0
def standard_mode():
    # set up local player data
    addPlayerData(player_name)

    screen_size = [700, 700]
    pygame.init()
    fpsClock = pygame.time.Clock()
    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]
    worm_loc = []
    draw_surface(playSurface, redColour, [my_board.food], 10,
                 100)  # draw first food
    pygame.display.set_caption('Food Snake')
    total_score = 0
    level = 1
    score = 0
    isEnd = False
    while True:
        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)
        status = my_snake.tick(my_board.food, my_board.wormhole)

        #if all snake body disappeared, go to next level!
        if my_snake.get_cells() == []:
            my_snake.cells = [[1, 1], [1, 2],
                              [1, 3]]  # reassign snake's position!
            level += 1
            score = 0
            if level == 2:
                for i in range(20, 30, 1):
                    my_snake.obstacles.append([i, 25])
            elif level == 3:
                for i in range(20, 30, 1):
                    my_snake.obstacles.append([i, 15])
                    my_snake.obstacles.append([i, 35])
            worm_loc = []
            my_board.new_food(my_new_cells, my_snake.obstacles, [])
            food_loc = [my_board.food]

        my_snake.teleport_wall()
        my_new_cells = my_snake.get_cells()
        if my_snake.is_dead([]) or isEnd:
            break
        if status == 1:
            score += my_board.foodWeight
            total_score += 1
            if score > 1:
                my_board.new_wormhole(my_new_cells, my_snake.obstacles, [])
                worm_loc = [my_board.wormhole]
                food_loc = []
            else:
                my_board.new_food(my_new_cells, my_snake.obstacles, [])
                food_loc = [my_board.food]

        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(avatar_image, (20, 615))
        show_message(playSurface, player_name, whiteColour, 40, 100, 630)
        draw_surface(playSurface, redColour, food_loc, 10, 100)
        draw_surface(playSurface, yellowColour, worm_loc, 10, 100)
        draw_surface(playSurface, greenColour, my_snake.obstacles, 10, 100)
        draw_surface(playSurface, whiteColour, my_new_cells, 10, 100)
        pygame.display.flip()
        # speed is changeable
        fpsClock.tick(my_snake.speed)
    game_end(total_score)
示例#6
0
class State:
    LEFT = [-1, 0]
    RIGHT = [1, 0]
    UP = [0, -1]
    DOWN = [0, 1]
    # clock-wise
    DIRS = [UP, RIGHT, DOWN, LEFT]

    len = 20

    scale = 4
    scale_num = int(len / scale)  #5

    tile_n = scale_num * scale_num

    # snake head * tail * food * direction
    # 25*25*16*4+1
    total_state_num = 9 * 9 * 9 * 9 * 4 + 1
    # 3 actions keep, turn right, turn left
    turn = 0

    def __init__(self):
        self.snake = Snake(self.len, self.len)
        self.board = Board(self.len, self.len)

    def get_index(self):

        tile_n = self.tile_n
        scale_num = self.scale_num
        scale = self.scale
        # 0 - 19
        # 0,1,2,3,4
        self.head_x = self.snake.cells[-1][0] // 4
        self.head_y = self.snake.cells[-1][1] // 4
        self.tail_x = self.snake.cells[0][0] // 4
        self.tail_y = self.snake.cells[0][1] // 4
        self.food_x = self.board.food[0] // 4
        self.food_y = self.board.food[1] // 4

        # -4321, 0 , 1234
        self.diff_x = int(self.head_x - self.food_x + 4)
        self.diff_y = int(self.head_y - self.food_y + 4)

        self.ht_x = int(self.head_x - self.tail_x + 4)
        self.ht_y = int(self.head_y - self.tail_y + 4)

        size = scale_num * 2

        self.dir = self.snake.direction

        if self.snake.is_dead():
            index = self.total_state_num - 1
        else:
            index =  int( self.diff_x + 9*self.diff_y +\
                            self.ht_x*9*9+ self.ht_y*9*9*9 +\
                        #self.head_x + size*self.head_y + \
                         #tile_n*self.food_x + tile_n*size*self.food_y + \
                        #tile_n*tile_n*self.tail_x + tile_n*tile_n*size*self.tail_y +\
                         self.DIRS.index(self.dir)*9*9*9*9 )
        return index

    def turn_right(self):
        index = self.DIRS.index(self.dir)
        self.dir = self.DIRS[(index + 1) % 4]
        self.snake.turn(self.dir)

    def turn_left(self):
        index = self.DIRS.index(self.dir)
        self.dir = self.DIRS[(index - 1) % 4]
        self.snake.turn(self.dir)

    def move_get_rewards(self):
        # return 0 if nothing happen
        # return 1 if got food
        self.turn += 1
        self.snake.teleport_wall()
        reward = self.snake.tick(self.board.food, None)

        if reward == 0:
            reward = -1 / (self.len * 2)  # negative if in loop
        elif reward == 1:
            self.board.new_food(self.snake.obstacles, [])

        if self.snake.is_dead():
            reward = -1

        return reward
示例#7
0
        while key != ord(' '):
            key = win.getch()
        key = prevKey
        continue
    if key not in [KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, 27]:  # If an invalid key is pressed
        key = prevKey
    if key == KEY_LEFT:
        my_snake.turn(LEFT)
    elif key == KEY_RIGHT:
        my_snake.turn(RIGHT)
    elif key == KEY_UP:
        my_snake.turn(UP)
    elif key == KEY_DOWN:
        my_snake.turn(DOWN)

    prev_cells = my_snake.get_cells()
    draw_chr(prev_cells, win, ' ')
    is_eat = my_snake.tick(my_board.food)
    my_snake.teleport_wall()
    if my_snake.is_dead():
        break
    new_cells  = my_snake.get_cells()

    if is_eat:  # When snake eats the food
        score += 1
        my_board.new_food(my_snake.cells)
        draw_chr([my_board.food], win, '*')
    draw_chr(new_cells, win, '#')

curses.endwin()
print("YOUR SCORE:", score)
示例#8
0
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)
示例#9
0
def standard_mode():
    # set up local player data
    addPlayerData(player_name)

    screen_size = [700, 700]
    pygame.init()
    fpsClock = pygame.time.Clock()
    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]
    worm_loc = []
    draw_surface(playSurface, redColour, [my_board.food], 10,
                 100)  # draw first food
    pygame.display.set_caption('Food Snake')
    total_score = 0
    level = 1
    score = 0
    isEnd = False
    while True:
        # catch event and control snake
        eventHelper(my_snake, True)

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

        #if all snake body disappeared, go to next level!
        if my_snake.get_cells() == []:
            my_snake.cells = [[1, 1], [1, 2],
                              [1, 3]]  # reassign snake's position!
            level += 1
            score = 0
            if level == 2:
                for i in range(20, 30, 1):
                    my_snake.obstacles.append([i, 25])
            elif level == 3:
                for i in range(20, 30, 1):
                    my_snake.obstacles.append([i, 15])
                    my_snake.obstacles.append([i, 35])
            worm_loc = []
            my_board.new_food(my_new_cells, my_snake.obstacles, [])
            food_loc = [my_board.food]

        my_snake.teleport_wall()
        my_new_cells = my_snake.get_cells()

        # ending condition
        if my_snake.is_dead([]) or isEnd:
            break
        if status == 1:
            score += my_board.foodWeight
            total_score += 1
            if score > 1:
                my_board.new_wormhole(my_new_cells, my_snake.obstacles, [])
                worm_loc = [my_board.wormhole]
                food_loc = []
            else:
                my_board.new_food(my_new_cells, my_snake.obstacles, [])
                food_loc = [my_board.food]

        # update display each round
        displayHelper(playSurface, fpsClock, level, food_loc, my_snake)

    game_end(total_score)
示例#10
0
class State:

    turn = 0

    LEFT = [-1, 0]
    RIGHT = [1, 0]
    UP = [0, -1]
    DOWN = [0, 1]
    # clock-wise
    DIRS = [UP,RIGHT,DOWN,LEFT]

    len = 20
    tile_n = 400

    # snake head * tail * food * direction
    # 400*4*4+1
    total_state_num = tile_n*4 * 4 + 1
    # 3 actions keep, turn right, turn left

    def __init__(self):
        self.snake = Snake(self.len, self.len)
        self.board = Board(self.len, self.len)

    def get_index(self):
        size = self.len
        tile_n = self.tile_n

        self.head_x = self.snake.cells[-1][0]
        self.head_y = self.snake.cells[-1][1]
        self.tail_x = self.snake.cells[0][0]
        self.tail_y = self.snake.cells[0][1]
        self.food_x = self.board.food[0]
        self.food_y = self.board.food[1]

        self.diff_x = self.head_x-self.food_x+size
        self.diff_y = self.head_y-self.food_y+size

        self.ht_x = self.head_x-self.tail_x
        self.ht_y = self.head_y-self.tail_y

        self.dir = self.snake.direction

        if self.snake.is_dead():
            index = self.total_state_num - 1
        else:
            index =  int( self.diff_x + size*2*self.diff_y +\
                        #self.head_x + size*self.head_y + \
                         #tile_n*self.food_x + tile_n*size*self.food_y + \
                        #tile_n*tile_n*self.tail_x + tile_n*tile_n*size*self.tail_y +\
                # self.ht_x*tile_n*4*4 + tile_n*4*4*size * 2 * self.ht_y
                         self.DIRS.index(self.dir)*tile_n*4  )
        return index

    def turn_right(self):
        index = self.DIRS.index(self.dir)
        self.dir = self.DIRS[(index+1)%4]
        self.snake.turn(self.dir)

    def turn_left(self):
        index = self.DIRS.index(self.dir)
        self.dir = self.DIRS[(index - 1) % 4]
        self.snake.turn(self.dir)

    def move_get_rewards(self):
        # return 0 if nothing happen
        # return 1 if got food
        self.snake.teleport_wall()
        reward = self.snake.tick(self.board.food,None)


        if reward == 0:
            reward = - 1 / self.len*2 # negative if in loop
        elif reward == 1:
            self.board.new_food(self.snake.obstacles,[])

        if self.snake.is_dead():
            reward = -1



        return reward