Example #1
0
def example():
    windows = tkinter.Tk()
    windows.title('snake')
    windows.geometry('320x240')
    canvas = tkinter.Canvas(windows, bg='black', width=320, height=240)
    canvas.pack()

    snake = GuiSnake(canvas)

    bodyA = Body(BodyGeometry(15, 5, 10, 10))  #10,0
    bodyB = Body(BodyGeometry(25, 5, 10, 10))  #20,0
    bodyC = Body(BodyGeometry(35, 5, 10, 10))  #30,0
    bodyD = Body(BodyGeometry(45, 5, 10, 10))  #40,0
    snake.add_head(bodyA)
    snake.add_head(bodyB)
    snake.add_head(bodyC)
    snake.add_head(bodyD)

    def move(event):
        print(event.keysym)
        if 'Up' == event.keysym:
            snake.move_up(10)
        elif 'Down' == event.keysym:
            snake.move_down(10)
        elif 'Left' == event.keysym:
            snake.move_left(10)
        elif 'Right' == event.keysym:
            snake.move_right(10)

    windows.bind('<Up>', move)
    windows.bind('<Down>', move)
    windows.bind('<Left>', move)
    windows.bind('<Right>', move)

    windows.mainloop()
Example #2
0
    def test_is_intersect(self):
        bodyA = Body(BodyGeometry(15, 15, 10, 10))  #10,10 20,20
        bodyB = Body(BodyGeometry(15, 15, 10, 10))

        bodyC = Body(BodyGeometry(15, 5, 10, 10))  #10,0 20,10
        bodyD = Body(BodyGeometry(15, 25, 10, 10))  #10,20 20,30
        bodyE = Body(BodyGeometry(5, 15, 10, 10))  #0,10 10,20
        bodyF = Body(BodyGeometry(25, 15, 10, 10))  #20,10 30,20

        bodyG = Body(BodyGeometry(15, 6, 10, 10))  #10,1 20,11
        bodyH = Body(BodyGeometry(15, 24, 10, 10))  #10,19 20,29
        bodyI = Body(BodyGeometry(6, 15, 10, 10))  #1,10 11,20
        bodyJ = Body(BodyGeometry(24, 15, 10, 10))  #19,10 29,20

        self.assertTrue(bodyA.is_intersect(bodyB))

        self.assertFalse(bodyA.is_intersect(bodyC))
        self.assertFalse(bodyA.is_intersect(bodyD))
        self.assertFalse(bodyA.is_intersect(bodyE))
        self.assertFalse(bodyA.is_intersect(bodyF))

        self.assertTrue(bodyA.is_intersect(bodyG))
        self.assertTrue(bodyA.is_intersect(bodyH))
        self.assertTrue(bodyA.is_intersect(bodyI))
        self.assertTrue(bodyA.is_intersect(bodyJ))
Example #3
0
    def test_count(self):
        body = Body(BodyGeometry(15, 15, 10, 10))
        baseSnake = BaseSnake(body)
        count = 1
        self.assertEqual(count, baseSnake.count())

        body2 = Body(BodyGeometry(15, 15, 10, 10))
        baseSnake.add_head(body2)
        count += 1
        self.assertEqual(count, baseSnake.count())
Example #4
0
 def setUp(self):
     body1 = Body(BodyGeometry(1, 1, 0, 0))
     body2 = Body(BodyGeometry(2, 1, 0, 0))
     body3 = Body(BodyGeometry(3, 1, 0, 0))
     body4 = Body(BodyGeometry(4, 1, 0, 0))
     self._bodys = [body4, body3, body2, body1]
     self._baseSnake = BaseSnake()
     index = len(self._bodys) - 1
     while index >= 0:
         #    self._baseSnake.add_head(self._bodys[index]) 这样添加到_baseSnake的body跟_bodys里面的body是相同的对象
         self._baseSnake.add_head(copy.deepcopy(self._bodys[index]))
         index -= 1
Example #5
0
    def test_will_dead_loop(self):
        """
          12345
        1   +++ 
        2   + +
        3   +o+
        """
        body1 = Body(BodyGeometry(3, 3, 1, 1))
        body2 = Body(BodyGeometry(3, 2, 1, 1))
        body3 = Body(BodyGeometry(3, 1, 1, 1))
        body4 = Body(BodyGeometry(4, 1, 1, 1))
        body5 = Body(BodyGeometry(5, 1, 1, 1))
        body6 = Body(BodyGeometry(5, 2, 1, 1))
        body7 = Body(BodyGeometry(5, 3, 1, 1))
        body8 = Body(BodyGeometry(4, 3, 1, 1))
        snake = BaseSnake()
        snake.add_head(body1)
        snake.add_head(body2)
        snake.add_head(body3)
        snake.add_head(body4)
        snake.add_head(body5)
        snake.add_head(body6)
        snake.add_head(body7)
        snake.add_head(body8)

        self.assertFalse(snake.is_will_dead_loop(Direction.DOWN))
        self.assertTrue(snake.is_will_dead_loop(Direction.UP))
Example #6
0
    def test_food_in_bodys(self):
        bodyA = Body(BodyGeometry(15, 5, 10, 10))  #10,0
        bodyB = Body(BodyGeometry(25, 5, 10, 10))  #20,0
        bodyC = Body(BodyGeometry(35, 5, 10, 10))  #30,0
        bodyD = Body(BodyGeometry(15, 5, 10, 10))  #40,0 50,10

        bodys = deque()
        bodys.appendleft(bodyA)
        bodys.appendleft(bodyB)
        self.assertTrue(bodyA in bodys)
        self.assertFalse(bodyC in bodys)
        self.assertTrue(bodyD in bodys)

        bodys.remove(bodyA)
        self.assertFalse(bodyD in bodys)
Example #7
0
 def test_will_loopback(self):
     bodyA = Body(BodyGeometry(15, 5, 10, 10))  #10,0
     bodyB = Body(BodyGeometry(25, 5, 10, 10))  #20,0
     bodyC = Body(BodyGeometry(35, 5, 10, 10))  #30,0
     bodyD = Body(BodyGeometry(45, 5, 10, 10))  #40,0
     snake = BaseSnake()
     snake.add_head(bodyA)
     snake.add_head(bodyB)
     snake.add_head(bodyC)
     snake.add_head(bodyD)
     self.assertFalse(snake.is_will_loopback(Direction.DOWN, 10))
     snake.move_down(10)
     self.assertFalse(snake.is_will_loopback(Direction.LEFT, 10))
     snake.move_left(10)
     self.assertTrue(snake.is_will_loopback(Direction.UP, 10))
Example #8
0
    def test_will_out_of_border(self):
        bodyA = Body(BodyGeometry(15, 5, 10, 10))  #10,0
        bodyB = Body(BodyGeometry(25, 5, 10, 10))  #20,0
        bodyC = Body(BodyGeometry(35, 5, 10, 10))  #30,0
        bodyD = Body(BodyGeometry(45, 5, 10, 10))  #40,0 50,10
        snake = BaseSnake()
        snake.add_head(bodyA)
        snake.add_head(bodyB)
        snake.add_head(bodyC)
        snake.add_head(bodyD)
        self.assertFalse(snake.is_will_out_of_border(Direction.RIGHT, 10))
        self.assertFalse(snake.is_will_out_of_border(Direction.DOWN, 10))

        snake.set_border(0, 0, 50, 20)
        self.assertTrue(snake.is_will_out_of_border(Direction.RIGHT, 10))
        self.assertFalse(snake.is_will_out_of_border(Direction.DOWN, 10))
Example #9
0
    def is_will_loopback(self, direction, distance):
        """
        检查回环,假设旧的头坐标为x1,y1,新的头坐标为x2,y2
        则检查头移动但身体还没移动时,其他身体的坐标是否在x1,x2,y1,y2形成的矩形区域上(可以当成一个巨大的身体块来看待)
        """
        if len(self._bodys) < 3:
            return True

        newHead = copy.deepcopy(self._bodys[0])
        newHead.move(direction, distance)

        oldHead = self._bodys[0]
        x = (newHead.geometry.x + oldHead.geometry.x)/2
        y = (newHead.geometry.y + oldHead.geometry.y)/2
        w = abs(newHead.geometry.x - oldHead.geometry.x) + newHead.geometry.w
        h = newHead.geometry.h
        hugeBody = Body(BodyGeometry(x, y, w, h))

        index = 2
        while index < len(self._bodys):
            if hugeBody.is_intersect(self._bodys[index]):
                return True
            index += 1

        return False
Example #10
0
def produce_food(foodLimit):
    x = random.randint(0, setting.screen_width - setting.size_of_snake)
    y = random.randint(0, setting.screen_height - setting.size_of_snake)
    # 生成的食物坐标中心与蛇身体的中心保持可以重合
    step = setting.size_of_snake
    x = (x // step) * step + step // 2
    y = (y // step) * step + step // 2
    food = Body(
        BodyGeometry(x, y, setting.size_of_snake, setting.size_of_snake))
    snake.try_produce_food(foodLimit, food)
Example #11
0
    def test_is_move_able(self):
        body1 = Body(BodyGeometry(0, 1, 0, 0))
        body2 = Body(BodyGeometry(0, 2, 0, 0))
        body3 = Body(BodyGeometry(3, 0, 0, 0))
        body4 = Body(BodyGeometry(4, 0, 0, 0))

        # 头部在下,不能向上移动
        snakeHeadDown = BaseSnake()
        snakeHeadDown.add_head(body1)
        snakeHeadDown.add_head(body2)
        self.assertFalse(snakeHeadDown.is_movable(Direction.UP))
        self.assertTrue(snakeHeadDown.is_movable(Direction.DOWN))
        self.assertTrue(snakeHeadDown.is_movable(Direction.LEFT))
        self.assertTrue(snakeHeadDown.is_movable(Direction.RIGHT))

        # 头部在上,不能向下移动
        snakeHeadUp = BaseSnake()
        snakeHeadUp.add_head(body2)
        snakeHeadUp.add_head(body1)
        self.assertTrue(snakeHeadUp.is_movable(Direction.UP))
        self.assertFalse(snakeHeadUp.is_movable(Direction.DOWN))
        self.assertTrue(snakeHeadUp.is_movable(Direction.LEFT))
        self.assertTrue(snakeHeadUp.is_movable(Direction.RIGHT))

        # 头部在左,不能向右移动
        snakeHeadLeft = BaseSnake()
        snakeHeadLeft.add_head(body4)
        snakeHeadLeft.add_head(body3)
        self.assertTrue(snakeHeadLeft.is_movable(Direction.UP))
        self.assertTrue(snakeHeadLeft.is_movable(Direction.DOWN))
        self.assertTrue(snakeHeadLeft.is_movable(Direction.LEFT))
        self.assertFalse(snakeHeadLeft.is_movable(Direction.RIGHT))

        # 头部在右,不能向左移动
        snakeHeadRight = BaseSnake()
        snakeHeadRight.add_head(body3)
        snakeHeadRight.add_head(body4)
        self.assertTrue(snakeHeadRight.is_movable(Direction.UP))
        self.assertTrue(snakeHeadRight.is_movable(Direction.DOWN))
        self.assertFalse(snakeHeadRight.is_movable(Direction.LEFT))
        self.assertTrue(snakeHeadRight.is_movable(Direction.RIGHT))
Example #12
0
    def test_nearest_body_distance(self):
        """
          12345
        1   +++ 
        2     +
        3    o+
        """
        body1 = Body(BodyGeometry(3, 1, 0, 0))
        body2 = Body(BodyGeometry(4, 1, 0, 0))
        body3 = Body(BodyGeometry(5, 1, 0, 0))
        body4 = Body(BodyGeometry(5, 2, 0, 0))
        body5 = Body(BodyGeometry(5, 3, 0, 0))
        body6 = Body(BodyGeometry(4, 3, 0, 0))
        snake = BaseSnake()
        snake.add_head(body1)
        snake.add_head(body2)
        snake.add_head(body3)
        snake.add_head(body4)
        snake.add_head(body5)
        snake.add_head(body6)

        distance, index = snake._nearest_body_distance(Direction.LEFT)
        self.assertEqual(distance, -1)
        distance, index = snake._nearest_body_distance(Direction.RIGHT)
        self.assertEqual(distance, 1)
        self.assertEqual(index, 1)
        distance, index = snake._nearest_body_distance(Direction.UP)
        self.assertEqual(distance, 2)
        self.assertEqual(index, 4)
Example #13
0
 def test_compare(self):
     bodyA = Body(BodyGeometry(0, 0, 10, 10))
     bodyB = Body(BodyGeometry(0, 0, 10, 10))
     bodyC = Body(BodyGeometry(10, 0, 10, 10))
     self.assertEqual(bodyA, bodyB)
     self.assertNotEqual(bodyB, bodyC)
Example #14
0
 def setUp(self):
     self._x = 0
     self._y = 1
     self._w = 10
     self._h = 10
     self._body = Body(BodyGeometry(self._x, self._y, self._w, self._h))
Example #15
0
 def test_move_to_other(self):
     bodyA = Body(BodyGeometry(0, 0, 10, 10))
     bodyB = Body(BodyGeometry(10, 10, 10, 10))
     bodyA.move_to_other(bodyB)
     self.assertEqual(bodyA.geometry.x, bodyB.geometry.x)
     self.assertEqual(bodyA.geometry.y, bodyB.geometry.y)
Example #16
0
    snake.try_produce_food(foodLimit, food)


setting = Setting()
windows = tkinter.Tk()
windows.title('snake')
windows.geometry(setting.screen_geometry)
canvas = tkinter.Canvas(windows,
                        bg='black',
                        width=setting.screen_width,
                        height=setting.screen_height)
canvas.pack()

snake = GuiSnake(canvas)

bodyA = Body(BodyGeometry(15, 5, setting.size_of_snake,
                          setting.size_of_snake))  #10,0
bodyB = Body(BodyGeometry(25, 5, setting.size_of_snake,
                          setting.size_of_snake))  #20,0
bodyC = Body(BodyGeometry(35, 5, setting.size_of_snake,
                          setting.size_of_snake))  #30,0
bodyD = Body(BodyGeometry(45, 5, setting.size_of_snake,
                          setting.size_of_snake))  #40,0
snake.add_head(bodyA)
snake.add_head(bodyB)
snake.add_head(bodyC)
snake.add_head(bodyD)
snake.set_border(0, 0, setting.screen_width, setting.screen_height)

autoRunThread = threading.Thread(target=auto_move_thread, name='autoRun')
autoRunThread.setDaemon(True)
autoRunThread.start()