The second stage of the fun-learning Python pinball game

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.