Пример #1
0
class SnakeSpriteTests(TestCase):
    def setUp(self):
        self.gf = GameFactory("data/settings.txt")
        frame = Frame(self.gf.screen, Rect(0, 0, 512, 256))
        self.snake = SnakeSprite(frame, self.gf, Vector(5, 5))

    def run_snake(self):
        while self.snake.is_moving():
            self.gf.screen.clear()
            self.snake.update()
            self.snake.draw()
            pygame.display.update()
            time.sleep(0.03)

    def test_draw_snake(self):
        """Display snake head"""
        self.snake.draw()
        pygame.display.update()
        time.sleep(0.5)

    def test_move_snake(self):
        """Snake moves into four directions."""
        for move in (UP, LEFT, DOWN, RIGHT):
            self.snake.add_move(move, [])
        self.run_snake()

    def test_add_tail(self):
        """Snake moves with tail segment."""
        self.snake.add_tail_segment()
        for move in (RIGHT, RIGHT, RIGHT, RIGHT):
            self.snake.add_move(move, [RIGHT])
        self.run_snake()

    def test_add_long_tail(self):
        """Snake moves into four directions."""
        self.snake.add_tail_segment()
        self.snake.add_move(RIGHT, [RIGHT])
        self.snake.add_tail_segment()
        self.snake.add_move(UP, [RIGHT, RIGHT])
        self.snake.add_move(UP, [UP, RIGHT])
        self.snake.add_tail_segment()
        self.snake.add_move(LEFT, [UP, UP, RIGHT])
        self.snake.add_move(LEFT, [LEFT, UP, UP])
        self.snake.add_move(LEFT, [LEFT, LEFT, UP])
        self.run_snake()
Пример #2
0
class SnakeControllerTests(TestCase):
    def setUp(self):
        self.level = SnakeLevel(box)
        self.gf = GameFactory("data/settings.txt")
        self.frame = Frame(self.gf.screen, Rect(0, 0, 512, 256))
        self.gf.tile_factory.add_tile_synonyms(TILE_SYNONYMS)
        pos = Vector(5, 5)
        self.snake = SnakeSprite(self.frame, self.gf, pos)
        self.control = SnakeController(pos, RIGHT, self.level, self.snake)

    def show(self):
        tmap = TiledMap(self.frame, self.gf.tile_factory)
        tmap.fill_map(str(self.level))
        tmap.cache_map()
        while self.snake.is_moving():
            self.snake.update()
            tmap.draw()
            self.snake.draw()
            pygame.display.update()
            time.sleep(0.03)

    def test_move(self):
        self.assertEqual(self.control.pos, Vector(5, 5))
        self.control.up()
        self.assertEqual(self.control.pos, Vector(5, 4))
        self.control.left()
        self.assertEqual(self.control.pos, Vector(4, 4))
        self.control.right()
        self.assertEqual(self.control.pos, Vector(5, 4))
        self.control.down()
        self.assertEqual(self.control.pos, Vector(5, 5))

    def test_crash(self):
        self.control.right()
        self.control.up()
        self.assertFalse(self.control.crashed)
        self.control.right()
        self.assertTrue(self.control.crashed)

    def test_grow(self):
        self.control.grow()
        self.assertEqual(len(self.control.positions), 2)
        self.assertEqual(self.control.positions[1], Vector(5, 5))
        self.control.up()
        self.assertEqual(len(self.control.positions), 2)
        self.assertEqual(self.control.positions[1], Vector(5, 5))
        self.control.up()  # there is a fruit here
        self.assertEqual(len(self.control.positions), 3)
        self.assertEqual(self.control.positions[1], Vector(5, 4))
        self.assertEqual(self.control.positions[2], Vector(5, 5))
        self.control.up()
        self.assertEqual(len(self.control.positions), 3)
        self.assertEqual(self.control.positions[2], Vector(5, 4))
        self.control.left()
        self.assertEqual(self.control.positions[0], Vector(4, 2))
        self.assertEqual(self.control.positions[1], Vector(5, 2))
        self.assertEqual(self.control.positions[2], Vector(5, 3))
        self.show()

    def test_run_into_tail(self):
        pass