Python基于Tkinter实现圣诞贺卡

嗨,几个月前,我给这个社区做了一张圣诞贺卡(
Http://www.thescripts.com/forum/thread580722.html
)现在复活节快到了,我也为复活节准备了一些东西。

选择 | 换行 | 行号
  1. import Tkinter
  2. import random
  3. import colorsys
  4. import time
  5.  
  6. # Happy easter 2007 folks! from kudos@spray.no
  7.  
  8. playerx=170
  9. bar = 0
  10. ball = 0
  11. ballvx = ballvy = 0.1
  12. ballx = 200;
  13. bally = 150;
  14.  
  15. def motion(event):
  16.  global playerx
  17.  playerx = event.x
  18.  if(playerx < 400-60):
  19.   w.coords(bar,playerx,250,playerx+60,250)
  20. root = Tkinter.Tk()
  21. w = Tkinter.Canvas(root, width=400, height=300, background="#000000")
  22. w.bind("<Motion>", motion)
  23. ball = w.create_oval(ballx-3,bally-3,ballx+3,bally+3,fill="#cccccc") # ball
  24. blocks = []
  25. k = 0.0
  26. for j in range(10):
  27.  for i in range(10):
  28.   c =  colorsys.hsv_to_rgb(k,0.5,0.4)
  29.   d = hex(int(c[0]*256)<<16 | int(c[1]*256)<<8 | int(c[2]*256));
  30.   d = "#"+d[2:len(d)]
  31.   k+=0.01
  32.   blocks.append(w.create_rectangle(40*i,(j*10)+10,40+(40*i),20+(j*10),fill=d))
  33. bar = w.create_line(playerx,250,playerx+60,250,fill="#ffffff")
  34. w.pack()
  35. try:
  36.  while 1:
  37.   w.coords(ball,ballx-3,bally-3,ballx+3,bally+3)
  38.   if(ballx+ballvx > 400 or ballx+ballvx < 0):
  39.    ballvx*=(-1)
  40.   if(bally+ballvy < 0):
  41.    ballvy*=(-1)
  42.   for block in blocks:
  43.     crash = 0
  44.     co = w.coords(block)
  45.     if(ballx+ballvx > co[0] and ballx+ballvx < co[2] and bally > co[1] and bally < co[3]):
  46.      crash=1
  47.      ballvx*=(-1)
  48.     if(ballx > co[0] and ballx < co[2] and bally+ballvy > co[1] and bally+ballvy < co[3]):
  49.      crash=1
  50.      ballvy*=(-1)
  51.     if(crash == 1):
  52.      w.coords(block,-10,-10,-20,-20)
  53.   if( (bally > 248 and bally < 250) and (ballx > playerx and ballx < playerx + 60)):
  54.    ballvy*=-1
  55.   if(bally > 406):
  56.    ballvy = ballvx = 0.1
  57.    ballx = 200
  58.    bally = 150
  59.   ballx+=ballvx
  60.   bally+=ballvy
  61.   root.update_idletasks()
  62.   root.update()
  63. except Tkinter.TclError:
  64.  pass
  65.  

注意,我没有为这个应用程序包括任何类型的计时器,所以如果它慢/快,那么增加/减少Ballvx和Ballvy(或者更好的是,编写一些代码来处理计时:-)
-赞誉

标签: python, tkinter

评论已关闭