以流畅的运动绘制椭圆形只是停止

时间:2019-06-17 17:42:56

标签: python python-3.x tkinter tkinter-canvas

我目前正在使用tkinter编写四连体游戏的代码。我设法创建了圆滑的运动来模拟类似光盘的特征。不幸的是,圆的绘制在中途停止(我相信是由于后面的命令)。这是我的代码:

def game_loop(player_1, player_2, guii, game):
    if game.get_current_player() == 1:  # PLAYER 1
        player = player_1
        current = 1
    else:
        player = player_2
        current = 2
    if isinstance(player, Ai) and current == 1:
        # the Ai finds a move, irrelevant to the problem. Lets assume the move is column #2 (so, the number will be 1)
        self.draw_circle(1)

    guii.return_root().after(100, game_loop, player_1, player_2, guii, game)# game_loop(player_1, player_2, guii, game))
    guii.mainloop()

该函数在AI轮到他时播放。仅在画完一个圆后才添加转弯-意思是,它撞到了地板。因此,当人类在思考转弯或圆圈还在绘制时-AI无法播放。

这是移动椭圆的功能:

## self.NUM_ROWS = 6
def smooth_motion(self, column, x_1, x_2, color):
    target_location = [x_1-100, 100 * (self.NUM_ROWS-1),
                       x_2-100, 100 * (self.NUM_ROWS-1) -100]
    while self.__canvas.coords(self.__disc) <= target_location:
        if color == "red":
            print(self.__canvas.coords(self.__disc))
        self.__canvas.update()
        coords_list = self.__canvas.coords(self.__disc)
        y_1 = coords_list[1]
        y_2 = coords_list[3]
        new_disc = self.__canvas.create_oval(x_1, y_1+0.85, x_2, y_2+0.85, fill=color)
        self.__canvas.delete(self.__disc)
        self.__disc = new_disc

当我分别测试GUI时,它运行良好。这就是为什么我确定问题是.after()命令的原因。我可以提供更多代码示例,但是我不想将所有代码都转储掉,因为那样效率不高,但是任何人需要的任何内容都会很高兴地发布。谢谢您的投入。

圆的计算方式:

def draw_circle(self, column):
    x_1 = 100 + (column * 100)
    x_2 = column * 100
    if self.game.get_turn() % 2 == 0:
        disc = self.__canvas.create_oval(100 + (column * 100), 0, column * 100,
                                     self.NUM_ROWS - self.col_dict[column] - 1 * 100,
                                     fill='yellow')
        color = "yellow"
    else:  # colors the discs accordingly
        disc = self.__canvas.create_oval(100 + (column * 100), 0, column * 100,
                                     self.NUM_ROWS - self.col_dict[column] - 1 * 100,
                                     fill='red')
        color = "red"
    self.__disc = disc
    self.col_dict[column] += 1
    self.smooth_motion(column, x_1, x_2, color)

列是从0到6。 col_dict [column]从0开始,每当球落入列中时,都会添加1。我从一开始就遇到了这个问题,所以可以用0代替 game_binder只需计算该圆并将其发送给smooth_motion。

0 个答案:

没有答案