EDA365电子工程师网
标题:
python写的屏保程序
[打印本页]
作者:
hasky
时间:
2016-5-30 17:38
标题:
python写的屏保程序
3 w- G1 e9 k. i) ~
__author__ = 'ChenYan'
" n' v' N5 G9 e7 y. x0 e6 X
" p; u: L4 U9 ?, s4 m& u
from random import randint
' U/ j- u# N, Z* y7 r
from tkinter import *
1 Y7 I; W% P9 V- G
& d7 M0 T+ [" m; t7 a, Q# M& g' a; ^
class Randball():
& [! r* H! l7 E2 `+ c* x
def __init__(self, canvas, scrnwidth, scrnheight):
7 C8 v3 Z) |; Y& W: p2 k9 J( p5 R
#初始化画布
% Y# }4 F+ L4 z7 ^1 M& X
self.canvas = canvas
$ O) _" M) D& H9 r6 f2 I6 Z! W
#初始化球的圆心坐标
" e) g" I$ E! B/ d
self.x_pos = randint(80, int(scrnwidth))
* [2 o6 O+ N8 S( T5 _
self.y_pos = randint(80, int(scrnheight))
2 j% r2 z1 Q; l2 s0 a! l
#球移动的距离
& o3 g' p, h) I, q* l
self.x_move = 10
+ B7 L* s, S7 j( j4 g
self.y_move = 10
+ f1 \; K3 R L5 Y' x8 C) R
#整个屏幕的宽和高
7 h& b. ]( I4 ^* C8 F
self.scrnwidth = scrnwidth
3 V6 }' m. r5 D
self.scrnheight = scrnheight
* R8 f" w/ M" J9 d
#初始化球的半径
$ X5 @: x' `' q. r
self.radius = randint(40, 70)
5 x( [& k+ D, k
#随机产生球的颜色
" e9 t) e) o2 t. `
rc = lambda :randint(0,255)
5 h' c, D$ H7 q( Q- ^
self.color = '#%02x%02x%02x' % (rc(), rc(), rc())
5 F% V7 y5 @7 _3 M) R
" T! A8 h7 o7 t/ J0 J
def create_ball(self):
. u/ \/ g& D s/ D* X0 Q+ X% S
#计算得到用于创建球的四个坐标
- V: B; D+ T: {6 h8 x
x1 = self.x_pos - self.radius
) H. j. i8 L! V! X" J/ d# a
y1 = self.x_pos - self.radius
' E4 j* Q1 y# B# A3 }: J# l# T/ o
x2 = self.x_pos + self.radius
- i3 z0 w; f' c) B) F& x
y2 = self.x_pos + self.radius
4 a1 Z- {7 q9 A. n' x
#画球
; m h6 z3 b5 D5 w# k' O% c
self.item = self.canvas.create_oval(x1, y1, x2, y2, fill=self.color, outline=self.color)
. R0 k9 h+ W0 N) s
4 z- [/ n& b" o6 n) a; f
def move_ball(self):
3 m2 }: K' k$ ?
'''按指定的距离移动球,如果球碰到障碍向相反的方向移动'''
: G. R" b* k: H" Y. h+ r
self.x_pos += self.x_move
0 j2 |& c) d& s6 O' D% H
self.y_pos += self.y_move
) L& w; V e4 s, _# s. j
2 G; ?. m- i4 \: d8 N8 z. I
if self.x_pos >= self.scrnwidth - self.radius:
6 [ a5 ^2 b4 j9 n% t
self.x_move = -self.x_move
5 _' N! g+ ~& m) x7 H# s
if self.y_pos >= self.scrnheight - self.radius:
' Q. k* j5 T3 ?! `
self.y_move = -self.y_move
2 G' G# y& z# N& P4 T( V
if self.x_pos < self.radius:
% p. ^! ?6 `5 T" x
self.x_move = abs(self.x_move)
9 Y* ^6 Q6 U/ V3 V
if self.y_pos < self.radius:
- R5 E# A/ p8 G5 f) L; I
self.y_move = abs(self.y_move)
& u3 _- |% R1 z, T
self.canvas.move(self.item, self.x_move, self.y_move)
y2 j5 k! M# D# ?; K) d( ^
+ d0 R& ?# E* Y3 c7 o b
class Screensaver():
2 C. r4 W" W; n! P; n! ~+ t8 @
balls = []
+ h# L7 i) \, s; y* A ~
def __init__(self, ball_nums):
! P- t+ i3 S( y1 G5 P
self.win = Tk()
1 M4 R- _, |+ c% v# [1 J; Z
self.width = self.win.winfo_screenwidth()
. a* N& \9 L# s
self.height = self.win.winfo_screenheight()
& U* m$ v! f- m
self.win.overrideredirect(True)
7 W* w; M3 E/ T5 T6 s% g
self.win.attributes('-alpha', 0.3)
. C/ D+ r+ A: h& H) x* R$ B6 U
#绑定事件,有任何动作退出屏保
, A& {7 [! ~ r2 |
self.win.bind('<Any-Button>', self.exit_screensaver)
9 c+ }. R, M5 q4 v" ]
self.win.bind('<Motion>', self.exit_screensaver)
& a( D/ y" E' p1 I- o, p! u
self.canvas = Canvas(self.win, width=self.width, height=self.height, bg='#00FFFF')
+ D" |9 N, G4 v4 u
self.canvas.pack()
0 x! P% A+ k7 J* W, m# N; M& l
1 T! u9 N. |2 Q
for i in range(0,ball_nums):
+ J: d( y1 ~6 K
ball = Randball(self.canvas, scrnwidth=self.width, scrnheight=self.height)
. ? _2 k) P4 e. j: _8 x0 E$ z
ball.create_ball()
& d+ b0 b5 {7 [3 K3 T/ _
self.balls.append(ball)
& a8 A$ R( a- J: g+ ?; N, b
self.run_screensaver()
: l4 Y. J( W3 \& A5 Q; z6 L
self.win.mainloop()
! e5 C' r! _1 k% Y! U. S# c$ k
% o7 s2 ^+ t$ j/ r8 x! e
def run_screensaver(self):
* \& W/ U0 F1 B, S/ c0 x' W& T
for ball in self.balls:
* Z& T" [' P# h
ball.move_ball()
# A+ V7 g( F0 G
self.canvas.after(20, self.run_screensaver)
6 l: n7 h; i. [; L, r
# X6 w+ n1 K- S" J
def exit_screensaver(self, event):
5 O/ x q! t2 \% |, q6 W6 `
self.win.destroy()
" p G8 }4 q6 s3 w
3 Y' N4 Y n1 t/ G, `
: f9 C n* j1 M, ^
def main():
4 X7 A. C8 s3 ]7 ^
Screensaver(15)
( t4 D2 H" A3 I/ J5 _: o- {
8 i% d3 ]" l; i: p
" ~4 b, d- E. b% @# G
if __name__=='__main__':
# @3 I2 C3 g$ C9 F6 [" @1 J2 E8 O9 p
main()
) H y) i: u+ s' h8 S( f( J+ M
复制代码
作者:
Haiting32451
时间:
2016-6-1 16:29
" ]% U2 D6 R2 c# i& b
学习了 挺好的资料 谢谢分享啊
作者:
cyxs
时间:
2016-6-2 15:29
谢谢O(∩_∩)O哈哈~谢谢O(∩_∩)O
作者:
zjfc78154
时间:
2016-9-21 15:58
牛逼,刚接触
欢迎光临 EDA365电子工程师网 (http://bbs.elecnest.cn/)
Powered by Discuz! X3.2