如何在Tkinter中以不同的速度为球设置动画?

编程入门 行业动态 更新时间:2024-10-21 14:19:48
本文介绍了如何在Tkinter中以不同的速度为球设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 import time, random from tkinter import * class Box( Frame ): def __init__( self ): # __init__ runs when Box() executes Frame.__init__( self ) # Frame is the top level component self.pack() self.master.title( "Canvas animation" ) self.master.geometry( "400x400" ) # size in pixels label = Label(self, text=" Bubbles ") label.grid(row=1, column=1) # create Canvas component self.myCanvas = Canvas( self ) self.myCanvas.grid(row=2, column=1) self.myCanvas.config(bg = 'cyan', height = 350, width = 350) self.balls = [] #list of balls belongs to the Box self.paint() self.animate() def paint( self ): #create a list of balls for i in range(35): x1, y1 = random.randint(35,315), random.randint(35,315) x2, y2 = x1 + 30 , y1 + 30 #size ballObjectId = self.myCanvas.create_oval\ ( x1, y1, x2, y2, fill = '') self.balls.append([ballObjectId, x1, y1]) self.myCanvas.update() print ("paint done") def animate( self ): #animate the list of balls for i in range(1000): for i in range(len(self.balls)): self.myCanvas.delete(self.balls[i][0]) #delete and redraw to move self.balls[i][1] += random.randint(-2,2) #change coordinates self.balls[i][2] += random.randint(-10,0) x1, y1 = self.balls[i][1], self.balls[i][2] x2, y2 = x1 + random.randint(30,31) , y1 + random.randint(30,31) self.balls[i][0]=(self.myCanvas.create_oval\ ( x1, y1, x2, y2, fill = '')) self.myCanvas.update() def main(): Box().mainloop() if __name__ == "__main__": main()

这是我到目前为止的代码.我似乎无法解决的问题是,我需要使球以不同的速度运动,并且尺寸也不同.我需要做的最后一步是使球到达顶部,然后从底部返回.因此,最终产品应该是多种尺寸的球,它们以不同的速度移动到画布的顶部,然后又出现在底部.感谢您的帮助!

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中以不同的速度为球设置动画?

本文发布于:2023-07-18 04:40:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141105.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中以   速度   动画   如何在   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!