python写的屏保程序
__author__ = 'ChenYan' from random import randint
from tkinter import * class Randball():
def __init__(self, canvas, scrnwidth, scrnheight):
#初始化画布
self.canvas = canvas
#初始化球的圆心坐标
self.x_pos = randint(80, int(scrnwidth))
self.y_pos = randint(80, int(scrnheight))
#球移动的距离
self.x_move = 10
self.y_move = 10
#整个屏幕的宽和高
self.scrnwidth = scrnwidth
self.scrnheight = scrnheight
#初始化球的半径
self.radius = randint(40, 70)
#随机产生球的颜色
rc = lambda :randint(0,255)
self.color = '#%02x%02x%02x' % (rc(), rc(), rc()) def create_ball(self):
#计算得到用于创建球的四个坐标
x1 = self.x_pos - self.radius
y1 = self.x_pos - self.radius
x2 = self.x_pos + self.radius
y2 = self.x_pos + self.radius
#画球
self.item = self.canvas.create_oval(x1, y1, x2, y2, fill=self.color, outline=self.color) def move_ball(self):
'''按指定的距离移动球,如果球碰到障碍向相反的方向移动'''
self.x_pos += self.x_move
self.y_pos += self.y_move if self.x_pos >= self.scrnwidth - self.radius:
self.x_move = -self.x_move
if self.y_pos >= self.scrnheight - self.radius:
self.y_move = -self.y_move
if self.x_pos < self.radius:
self.x_move = abs(self.x_move)
if self.y_pos < self.radius:
self.y_move = abs(self.y_move)
self.canvas.move(self.item, self.x_move, self.y_move) class Screensaver():
balls = []
def __init__(self, ball_nums):
self.win = Tk()
self.width = self.win.winfo_screenwidth()
self.height = self.win.winfo_screenheight()
self.win.overrideredirect(True)
self.win.attributes('-alpha', 0.3)
#绑定事件,有任何动作退出屏保
self.win.bind('<Any-Button>', self.exit_screensaver)
self.win.bind('<Motion>', self.exit_screensaver)
self.canvas = Canvas(self.win, width=self.width, height=self.height, bg='#00FFFF')
self.canvas.pack() for i in range(0,ball_nums):
ball = Randball(self.canvas, scrnwidth=self.width, scrnheight=self.height)
ball.create_ball()
self.balls.append(ball)
self.run_screensaver()
self.win.mainloop() def run_screensaver(self):
for ball in self.balls:
ball.move_ball()
self.canvas.after(20, self.run_screensaver) def exit_screensaver(self, event):
self.win.destroy() def main():
Screensaver(15) if __name__=='__main__':
main()
python写的屏保程序的更多相关文章
- wpf 制作播放视频的屏保程序、而且能分屏显示
这个程序用到了WPF里 “visual_Brush”(主要是为了实现分屏显示) , “UserControl” ,这两个知识点: 在屏保状态下播放指定文件夹下的视频,而且能分屏显示: 把编译好的屏保 ...
- 用Qt写的简单屏保程序
近日老大提别人家产品都有屏保程序,貌似我们也该有,简单在qtcn.org请教了一下,写了个小程序! 晕倒,半天没找到上传功能!我已经上传到qtcn上了,地址如下: http://www.qtcn.or ...
- 用python写一个定时提醒程序
身体是革命的本钱,身体健康了我们才有更多精力做自己想做的事情,追求女神,追求梦想.然而程序员是一个苦比的职业,大部分时间都对着电脑,我现在颈椎就不好了,有时候眼睛还疼,我还没20阿,伤心...于是乎写 ...
- WinForm 屏保程序
this.ShowInTaskbar = false; this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWind ...
- 3D屏保程序:汉诺塔
学过程序的人一定记得汉诺塔.我们学的第一个程序是HelloWorld,而碰到的第一个坑就是汉诺塔,短短十几行代码,不知花费了多少时间精力去理解.我记得当年的开发环境还是蓝屏的,汉诺塔程序的输出还是一行 ...
- [archlinux][plasma][screensaver] plasma5配置屏保程序,没成功(-_-#)
plamsa用了好久,一直没有屏保.我想要玄酷的屏保! 用xscreensaver, 之前用FVWM2的时候,就用过了,很玄酷. 一,安装 pacman -S xscreensaver 二,配置 xs ...
- 用python写一个hello world程序
1,http://www.python.org/download/ 下载windows安装包, 2,python环境变量配置 (1)设置环境变量:我的电脑-右键-属性-高级-环境变量 在Path中加入 ...
- 用processing生成屏保程序
想法 利用随机数控制圆圈的大小.位置以及颜色,可以产生随机的美感. 让小球动起来,并且在屏幕边界处产生反弹效果. 代码 1: float circle_x = (float) 0.0; 2: floa ...
- Python 写了个小程序,耗时一天,结果才100多行
from selenium import webdriver import selenium.webdriver.support.ui as ui from selenium.webdriver.co ...
随机推荐
- java 多个文件打包zip
/** * 多个文件打包成zip */ public class ZipDemo { private static void create() throws Exception{ String pat ...
- win7 64位的PHP5.4安装redis扩展
先看phpinfo.php信息 可以看是 PHP5.4 VC9 TS Architecture x86 说明是x86的PHP,虽然系统是64位的,所以还是要下载x86的redis 然后Github下载 ...
- 转:js不同类型对象的比较规则
Type(x) Type(y) Result type(x)==type(y) x===y otherwise... false null undefined true undefined null ...
- java.lang.UnsupportedClassVersionError: Bad version number in .class file 解决方案
在Myeclipse中运行小应用程序时出现如下异常的解决办法 java.lang.UnsupportedClassVersionError: Bad version number in .class ...
- Suricata, to 10Gbps and beyond(X86架构)
Introduction Since the beginning of July 2012, OISF team is able to access to a server where one int ...
- UVAlive 2326 Moving Tables(贪心 + 区间问题)
The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in ...
- windows下C语言调用系统文件选择对话框
代码片段,在windows下用C语言调用文件选择对话框,以备忘 #define DEFAULT_DIR "" char extraction_path[MAX_PATH] = DE ...
- DevExpress ASP.NET 使用经验谈(4)-CriteriaOperator的使用
上一节中,我们已经介绍了,使用CriteriaOperator表达式,获取对象数据. CriteriaOperator criteria = CriteriaOperator.Parse(" ...
- 修改页面中所有TextBox控件的样式--CSS
1.HTML <div> TextBox<br /> <input type="text" name="name" value=& ...
- LeetCode 链表的插入排序
Sort a linked list using insertion sort 创建一个新的链表,将旧链表的节点插入到正确的位置 package cn.edu.algorithm.huawei; pu ...