happy (tkinter) easter 2007 folks!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kudos
    Recognized Expert New Member
    • Jul 2006
    • 127

    #1

    happy (tkinter) easter 2007 folks!

    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....

    Code:
    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
    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
Working...