Fun Learning Python Pinball game stage IV-Full screen strum

Source: Internet
Author: User

It has been possible to bounce the ball up and down in the vertical direction. Next, continue to follow the "fun learning python--teach children to learn programming", began to let the ball bounce around the whole picture up and down.

In the program, the x-axis is defined as a random starting value, and the positive and negative values are there, so in the transverse direction, the ball will start from the left or right.

In the program, the y-axis is defined only-3, that is, in the longitudinal direction, the beginning of the ball can only move upward, of course, the starting direction can also be in accordance with the x-axis, with a random number to change the starting direction of the upper and lower.

In the previous program, the starting pixel values were 1 and 1, and now it's changed to 3 and-3, just so that the ball can move faster each time it is redrawn.

#引入下列模块from  tkinter import *                                                    #画图模块import  random                                                            #随机数模块import  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 graphic ( SELF.ID,245,100)                               #把画好的椭圆形移动的画布中心, graphical ID representation          starts = [-3,-2,-1,1,2,3]                                       #给一串x分量的起始值 (x and y represent the horizontal and vertical components)          random.shuffle (starts)                                           #随机混排序, assign a value to the object variable x to get the random component value when it starts, causing the ball to start at different angles each time.         self.x = starts[0]                                              #对象变量x就是水平分量移动的初始值, equivalent to moving left and right , the value represents how many pixels are moved         self.y = -3                                                      #对象变量y就是垂直分量移动的初始值, equivalent to moving up and down          self.canvas_height = self.canvas.winfo_height ()                 #获取画布的高度, the canvas height is measured from top to bottom                         Self.canvas_width = selF.canvas.winfo_width ()                   #获取画布的宽度         def draw (self):                                                      #定义画图动作          self.canvas.move (SELF.ID,SELF.X,SELF.Y)                         #按照x和y定义的像素值来移动小球, such as 3, is to move 3 pixel locations.          pos = self.canvas.coords (self.id)                                #coords函数通过id返回画布球的坐标列表, Pos[x1,y1,x2,y2], representing the XY coordinates of the lower-left upper-right corner of the ellipse, respectively          if pos[1] <= 0:                                                # When the upper-left corner of the ball y1 coordinates less than or equal to 0, it touches the top of the canvas             self.y =  3                                                  #重新设置对象变量y为3, start the vertical component Move down          if pos[3] >= self.canvas_height:                                #当球右下角y2坐标超过画布高度 (bottom)              self.y = -3                                                 #重新设置对象变量y为-3, Start vertical component Move up         if pos[0] <= 0:                                                  #当球左上角x1坐标小于等于0, which touches the left border of the canvas              self.x = 3                                                  #重新设置对象变量x为3, start horizontal component Move right          if pos[2] >= self.canvas_width:                                 #当球右下角x2坐标超过画布有边框              self.x = -3                                                  #重新设置对象变量x为-3, start horizontal component shift leftMove              #创建游戏的桌布tk  = tk ()                                                                  #用Tk () class creates a Tk object, which 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 ()                                                             #按照上面一行指定的宽度高度参数调整其自身大小tk. Update ()                                                               #update强制更新屏幕, Re-draw # Draw a red ball ball = ball (canvas, ' red ')                                               # Draw a red ball on the canvas with the balls # main loop, let Tkinter keep redrawing the screen While 1:    ball.draw ()                                                           #调用Ball类的作画函数     tk.update_idletasks ()                                                 #updata_idletasks和updata这两个命令     tk.update ()                                                         # Get Tkinter to paint something on the canvas.     time.sleep (0.01)                                                     # Delay to slow down the animation effect

Fun Learning Python Pinball game stage IV-Full screen strum

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.