现在,照着《趣学Python--教孩子学编程》上的内容,要让球向上运动起来了。

from tkinter import *import randomimport time#创建ball类class Ball:    def __init__(self,canvas,color):                                #初始化函数,包含画布canvas和颜色color参数        self.canvas = canvas                                        #把参数canvas赋值给对象变量canvas        self.id = canvas.create_oval(10,10,25,25,fill=color)        #创建椭圆,左上角和右下角xy坐标,返回代表图形的ID        self.canvas.move(self.id,245,100)                           #把画好的椭圆形移动的画布中心,图形用id表示    def draw(self):                                                 #定义画图动作        self.canvas.move(self.id,0,-1)                              #垂直方向向上移动一个像素,水平方向不动     #创建游戏的桌布tk = Tk()                                                           #用Tk()类创建一个tk对象,它就是一个基本窗口,可以在其上增加其他东西tk.title("Game")                                                    #给Tk对象窗口加一个标题tk.resizable(0,0)                                                   #tk窗口大小不可调整tk.wm_attributes("-topmost",1)                                      #告诉tkinter把窗口放到最前面canvas = Canvas(tk,width=500,heigh=400,bd=0,highlightthickness=0)   #Canvas是一个画布类canvas.pack()                                                       #按照上面一行指定的宽度高度参数调整其自身大小tk.update()#画一个红色的球ball = Ball(canvas,'red')                                           #用Ball类在画布上画一个红色的球#主循环,让tkinter不停地重画屏幕while 1:    ball.draw()                                                     #调用Ball类的作画函数    tk.update_idletasks()                                           #updata_idletasks和updata这两个命令    tk.update()                                                     #让tkinter快一点把画布上的东西画出来    time.sleep(0.01)                                                #延时让动画效果慢一点