手动调速的弹球

tkinter bounce ball class 手动调速度的弹球
tkinter bounce ball class 手动调速度的弹球

"""
   手动调速的弹球.py
"""
from tkinter import *

class BounceBall(Frame):
    
    def __init__(self, master=None):
        
        Frame.__init__(self, master)
        Pack.config(self)
        self.createWidgets()
        self.master.title("手动调速的弹球")
        self.after(10, self.moveBall)

    def quit(self):
        self.master.destroy()
        
    def createWidgets(self):

        # 退出按钮
        self.QUIT = Button(self, text='关\n\n闭\n\n窗\n\n口', foreground='red',command=self.quit)
        self.QUIT.pack(side=LEFT, fill=BOTH)

        # 新建画布
        self.canvas = Canvas(self, width=640, height=480,bg='dodger blue')

        # 调整速度的尺度条
        self.speed = Scale(self, orient=HORIZONTAL, label="球的速度:",from_=-100, to=100)

        self.speed.pack(side=BOTTOM, fill=X)

        # 小球对象,只是一个item,整数
        self.ball = self.canvas.create_oval(0, 0, 100, 100, fill="pink")
        self.x = 0
        self.y = 0
        self.velocity_x = 0.3
        self.velocity_y = 0.5

        self.canvas.pack(side=LEFT)

    def moveBall(self, *args):
        if (self.x > 540) or (self.x < 0.0):
            self.velocity_x = -1.0 * self.velocity_x
        if (self.y > 380) or (self.y < 0.0):
            self.velocity_y = -1.0 * self.velocity_y

        deltax = (self.velocity_x * self.speed.get() )
        deltay = (self.velocity_y * self.speed.get() )
        self.x = self.x + deltax
        self.y = self.y + deltay

        self.canvas.move(self.ball, deltax,deltay)
        self.after(10, self.moveBall)


game = BounceBall()

game.mainloop()

扫码后加李兴球微信号:scratch8,获赠100Python创意程序及大量Scratch作品。 本站所有作品,教程等皆为原创,盗版必究。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。扫码付款后的朋友可以加QQ: 406273900,提供技术支持及发送作品图片与配音素材。亦可直接向微信号scratch8付款购买。
李兴球的博客_Python创意编程技术前沿 » 手动调速的弹球

学本领,探索更大的世界!

李兴球博客 风火轮编程主页