Ejemplo n.º 1
0
 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)
Ejemplo n.º 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