python -屏保

预览图

预览图

源码

import tkinter
import random
import operator
from PIL import Image, ImageTk
# 小球类
class Ball():
    def __init__(self, canvas, scrwidth, scrheight):
        self.canvas = canvas
        self.scrwidth = scrwidth
        self.scrheight = scrheight
        # 球心初始坐标
        self.posx = random.randint(0, self.scrwidth-100)
        self.posy = random.randint(0, self.scrheight-100)
        # 球速度
        self.x_vel = random.randint(10, 25)
        self.y_vel = random.randint(10, 25)
        # 球半径
        self.radius = random.randint(2, 10)
        # 球颜色
        self.ball_color = lambda: random.randint(0, 255)
        # 取0-255rgb颜色 16进制输出
        self.color = '#%02x%02x%02x' % (self.ball_color(), self.ball_color(), self.ball_color())  
	# 绘制小球
    def creat_ball(self):
        # 椭圆化圆: 左上(x1, y1)点   右下(x2, y2)点   两点将oval转化为 round
        x1 = self.posx - self.radius
        y1 = self.posy - self.radius
        x2 = self.posx + self.radius
        y2 = self.posy + self.radius
        self.round_ball = self.canvas.create_oval(x1, y1, x2, y2, fill = self.color, outline = self.color)
	# 小球移动
    def move_ball(self):
        self.posx += self.x_vel
        self.posy += self.y_vel       
        right1 = self.posx + self.radius >= self.scrwidth
        under = self.posy + self.radius >= self.scrheight
        left = self.posx - self.radius - 40 <= 0
        top = self.posy - self.radius - 40 <= 0
        if right1:
            self.x_vel *= -1
            if under:
                self.y_vel *= -1
        elif under:
            self.y_vel *= -1
        elif left:
            self.x_vel *= -1
            if top:
                self.y_vel *= -1
        elif top:
            self.y_vel *= -1
        else: pass
        self.canvas.move(self.round_ball, self.x_vel, self.y_vel)
# 屏幕类
class Screen(Ball):
    def __init__(self):
        self.ball = list()
        self.ball_num = random.randint(100, 200)
        self.base = tkinter.Tk()
        #取消窗口边框
        self.base.overrideredirect(1)   
        width = 1920
        height = 1080
        self.canvas = tkinter.Canvas(self.base, width = width, height = height)
        self.canvas.pack()
        # 背景图片
        images = ImageTk.PhotoImage(Image.open("img1.jpg"))
        # 取屏幕中心
        self.canvas.create_image(960, 540, image = images)  
        super().__init__(self.canvas, width, height)
        x = y = 0
        for i in range(self.ball_num):
            new_ball = Ball(self.canvas, width, height)
            new_ball.creat_ball()
            self.ball.append(new_ball)
            if x == new_ball.posx:
                new_ball.ball_color = lambda: random.randint(0, 255)
            if y == self.posy:
                new_ball.ball_color = lambda: random.randint(0, 255)
            x = new_ball.posx
            y = new_ball.posy       
        self.run_ball()
        self.bind()
        self.base.mainloop()

    def run_ball(self):
        for ball in self.ball:
            ball.move_ball()
            # after 200毫秒后启动一个函数,需要启动的函数是第二个参数

        self.canvas.after(200, self.run_ball)   
    def bind(self):
            self.base.bind('<Motion>', self.myquit)
	#事件处理机制
    def myquit(self, quit):
        self.base.destroy()     
    
if __name__ == "__main__":
    while not Screen():
        pass
        


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

影修

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值