如何在Tkinter中以不同的速度为球设置动画? [英] How to animate balls with varying speeds in Tkinter?

查看:55
本文介绍了如何在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中以不同的速度为球设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆