糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > 泡泡屏保

泡泡屏保

时间:2021-06-12 08:35:46

相关推荐

泡泡屏保

import randomimport tkinter'''要求,1 建立屏保程序,2. 屏保是随机球,自己运动3. 鼠标移动则清除分析:1, 需要一个ScreenSaver类,一个Ball的类2. ScreenSaver:1.1: 需要一个canvas, 大小与屏幕一致1.2:程序已启动,就开始在屏幕上画小球,小球位置随机,会动,速度,方向随机1.3: 鼠标一移动,则程序退出1.4: 多少个小球,建议允许输入2. RandomBall:2.1: 大小,颜色,出现位置,运动方向,速度,完全随机2.2: 球就有移动功能,可以被调用'''class RandomBall:def __init__(self, canvas, scrnwidth, scrnheight):self.canvas = canvasself.xpos = random.randint(10, int(scrnwidth))self.ypos = random.randint(10, int(scrnheight))self.xvelocity = random.randint(4,20)self.yvelocity = random.randint(4,20)self.scrnwidth = scrnwidthself.scrnheight = scrnheightself.radius = random.randint(20,120)c = lambda : random.randint(0,255)self.color = '#%02x%02x%02x'%(c(),c(),c())def create_ball(self):x1 = self.xpos - self.radiusy1 = self.ypos - self.radiusx2 = self.xpos + self.radiusy2 = self.ypos + self.radiusself.item = self.canvas.create_oval(x1,y1,x2,y2, fill=self.color, outline=self.color)def move_ball(self):self.xpos += self.xvelocityself.ypos += self.yvelocityif self.ypos >= self.scrnheight - self.radius:self.yvelocity = - self.yvelocityif self.ypos <= self.radius:self.yvelocity = abs(self.yvelocity)if self.xpos >= self.scrnwidth - self.radius or self.xpos <= self.radius:self.xvelocity = - self.xvelocityself.canvas.move(self.item, self.xvelocity, self.yvelocity)class ScreenSaver:balls = []def __init__(self,num_balls):self.root = tkinter.Tk()# 取消边框self.root.overrideredirect(1)# 任何鼠标移动self.root.bind('<Motion>', self.myquit)w,h = self.root.winfo_screenwidth(), self.root.winfo_screenheight()self.canvas = tkinter.Canvas(self.root, width=w, height=h )self.canvas.pack()for i in range(num_balls ):ball = RandomBall(self.canvas, scrnwidth=w, scrnheight=h)ball.create_ball()self.balls.append(ball)self.run_screen_saver()self.root.mainloop()def run_screen_saver(self):for ball in self.balls:ball.move_ball()self.canvas.after(200, self.run_screen_saver)def myquit(self,event):self.root.destroy()if __name__ == "__main__":ScreenSaver(12)

如果觉得《泡泡屏保》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。