这是我到目前为止的代码.我似乎无法解决的问题是,我需要使球以不同的速度运动,并且尺寸也不同.我需要做的最后一步是使球到达顶部,然后从底部返回.因此,最终产品应该是多种尺寸的球,它们以不同的速度移动到画布的顶部,然后又出现在底部.感谢您的帮助!
This is the code I have so far. The problem I can't seem to solve is I need the balls to move a different speeds and also have different sizes. The final step I need to do is have the balls go to the top and come back up from the bottom. So the final product should be balls of multiple sizes traveling at different speeds to the top of the canvas then appearing back at the bottom. Thanks for any help!
推荐答案一种解决方案是创建一个代表一个球的自定义类.在该类中有移动球的代码.每个球可以有自己的速度和方向.
One solution is to create a custom class that represents one ball. Have the code to move the ball be inside that class. Each ball can then have its own velocity and direction.
这是一个掉落的球的非常简单的例子.您将需要添加用于弹跳,跌落到不同方向等的逻辑.
Here's a really simple example for a ball that falls. You would need to add logic for bouncing, falling in different directions, etc.
class Ball(object): def __init__(self, canvas, x=10, y=10, radius=10, color="red", speed=1): self.canvas = canvas self.speed = speed self.canvas_id = canvas.create_oval(x-radius, y-radius, x+radius, y+radius, outline=color, fill=color) def move(self, on=True): self.canvas.move(self.canvas_id, self.speed, self.speed)您可以将其与以下内容一起使用:
You would use this with something like this:
self.canvas = Canvas(...) self.balls = [] for i in range(10): <compute x, y, color, speed, ...> ball = Ball(self.canvas, x, y, radius, color, speed) self.balls.append(ball)要设置它们的动画,请设置一个简单的循环:
To animate them, set up a simple loop:
def animate(self): for ball in self.balls: ball.move() self.canvas.after(30, self.animate)更多推荐
如何在Tkinter中以不同的速度为球设置动画?
发布评论