Now, according to the "Fun learning python--teach children to learn programming" content, to let the ball move up.
from tkinter import *import randomimport time# Create Ball class class ball: def __init__ (Self,canvas,color): #初始化函数 with canvas and color parameters self.canvas = canvas #把参数canvas赋值给对象变量canvas self.id = canvas.create_oval (10,10,25,25,fill=color) #创建椭圆, upper-left and lower-right XY coordinates, returns the id self.canvas.move representing the graph (self.id, 245,100) #把画好的椭圆形移动的画布中心, graphics with ID def draw (self): #定义画图动作 self.canvas.move (self.id,0,-1) #垂直方向向上移动一个像素, horizontal orientation #创建游戏的桌布tk = tk () #用Tk () class to create a Tk object, It is a basic window on which you can add something else 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 () # Adjusts its own size according to the width height parameter specified in the previous line tk.update () #画一个红色的球ball = ball (canvas, ' red ') #用Ball类在画布上画一个红色的球 # main loop, let Tkinter constantly re-painting the Screen While 1: ball.draw () #调用Ball类的作画函数 tk.update_idletasks () #updata_idletasks和updata这两个命令 tk.update () #让tkinter快一点把画布上的东西画出来 time.sleep (0.01) # Delay to slow down the animation effect
The second stage of the fun-learning Python pinball game