Python基于Tkinter实现圣诞贺卡
嗨,几个月前,我给这个社区做了一张圣诞贺卡(
Http://www.thescripts.com/forum/thread580722.html
)现在复活节快到了,我也为复活节准备了一些东西。
选择 | 换行 | 行号
- import Tkinter
- import random
- import colorsys
- import time
- # Happy easter 2007 folks! from kudos@spray.no
- playerx=170
- bar = 0
- ball = 0
- ballvx = ballvy = 0.1
- ballx = 200;
- bally = 150;
- def motion(event):
- global playerx
- playerx = event.x
- if(playerx < 400-60):
- w.coords(bar,playerx,250,playerx+60,250)
- root = Tkinter.Tk()
- w = Tkinter.Canvas(root, width=400, height=300, background="#000000")
- w.bind("<Motion>", motion)
- ball = w.create_oval(ballx-3,bally-3,ballx+3,bally+3,fill="#cccccc") # ball
- blocks = []
- k = 0.0
- for j in range(10):
- for i in range(10):
- c = colorsys.hsv_to_rgb(k,0.5,0.4)
- d = hex(int(c[0]*256)<<16 | int(c[1]*256)<<8 | int(c[2]*256));
- d = "#"+d[2:len(d)]
- k+=0.01
- blocks.append(w.create_rectangle(40*i,(j*10)+10,40+(40*i),20+(j*10),fill=d))
- bar = w.create_line(playerx,250,playerx+60,250,fill="#ffffff")
- w.pack()
- try:
- while 1:
- w.coords(ball,ballx-3,bally-3,ballx+3,bally+3)
- if(ballx+ballvx > 400 or ballx+ballvx < 0):
- ballvx*=(-1)
- if(bally+ballvy < 0):
- ballvy*=(-1)
- for block in blocks:
- crash = 0
- co = w.coords(block)
- if(ballx+ballvx > co[0] and ballx+ballvx < co[2] and bally > co[1] and bally < co[3]):
- crash=1
- ballvx*=(-1)
- if(ballx > co[0] and ballx < co[2] and bally+ballvy > co[1] and bally+ballvy < co[3]):
- crash=1
- ballvy*=(-1)
- if(crash == 1):
- w.coords(block,-10,-10,-20,-20)
- if( (bally > 248 and bally < 250) and (ballx > playerx and ballx < playerx + 60)):
- ballvy*=-1
- if(bally > 406):
- ballvy = ballvx = 0.1
- ballx = 200
- bally = 150
- ballx+=ballvx
- bally+=ballvy
- root.update_idletasks()
- root.update()
- except Tkinter.TclError:
- pass
注意,我没有为这个应用程序包括任何类型的计时器,所以如果它慢/快,那么增加/减少Ballvx和Ballvy(或者更好的是,编写一些代码来处理计时:-)
-赞誉
评论已关闭