Example #1
0
    def test_teleport_wall(self):
        my_snake = Snake(20, 60)
        my_snake.cells = [[0, 30]]
        self.assertEquals(my_snake.teleport_wall(), 0)
        my_snake.cells = [[10, 0]]
        self.assertEquals(my_snake.teleport_wall(), 0)
        my_snake.cells = [[my_snake.height - 1, 0]]
        self.assertEquals(my_snake.teleport_wall(), 0)
        my_snake.cells = [[0, my_snake.width - 1]]
        self.assertEquals(my_snake.teleport_wall(), 0)

        self.assertEquals(True, True)
Example #2
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)
Example #3
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)