Recently, I read a book called "Fun Learning python--teach children to learn programming", which is very suitable for me who never write code.
Today began to follow the book, learn to write the first game of life: Pinball.
The code is just the beginning, just a window and a red ball 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0057.gif "alt=" j_0057.gif "/ >
from tkinter import *import randomimport time# Create Ball class class ball: def __init__ (Self,canvas,color): #初始化函数 with canvas and color parameters self.canvas = canvas # Assign the parameter canvas to the object variable Canvas self.id = canvas.create_oval ( 10, 10, 25, 25, fill=color) #创建椭圆, upper-left and lower-right XY coordinates, returning id representing the graphic self.canvas.move (self.id, 245,100) # The center of the canvas in which the painted oval is moved, the graphic is represented by the ID def draw (self): pass #创建游戏的桌布tk = tk () # Create a Tk object with the TK () class, a Tk object that is a basic window that can add something else to the window 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类在画布上画一个红色的球 # main loop, let Tkinter continue to redraw the screen while 1: tk.update_idletasks () tk.update () time.sleep (0.01)
The first stage of the fun-learning Python pinball game