嗨,几个月前,我向这个社区制作了圣诞礼物“卡片”(
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(或者甚至更好,编写一些代码来处理计时:-)
-荣誉
From: https://bytes.com/topic/python/insights/625365-happy-tkinter-easter-2007-folks