By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,155 Members | 952 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

happy (tkinter) easter 2007 folks!

kudos
Expert 100+
P: 126
Hi, a couple of months ago, I made a xmas chistmas "card" to this community(http://www.thescripts.com/forum/thread580722.html) Now its soon easter, and I hacked together something for easter as well....

Expand|Select|Wrap|Line Numbers
  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.  
Note, I didn't include any kind of timer for this application, so if its slow/fast then increase/decrease ballvx and ballvy (or even better, write some code than handles timing :-)

-kudos
Mar 30 '07 #1
Share this Article
Share on Google+