here2share

# Tk_FramesPerSec_demo.py

Aug 20th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. # Tk_FramesPerSec_demo.py
  2.  
  3. from Tkinter import *
  4. import time
  5. import random
  6. import urllib2
  7. import sys
  8. import base64
  9. from threading import Thread
  10. from PIL import ImageTk
  11.  
  12. width = 800
  13. height = 600
  14. radius = 19
  15. root = Tk()
  16. root.geometry('800x600+0+0')
  17. root.title("FPS Demo")
  18. go = True
  19. ballrack = [((400,300),(5, 8))]
  20. fps = 0
  21. sprites = 1
  22. canvas = Canvas(root, width=800, height=600)
  23. canvas.pack()
  24. ballrelease = 0
  25.  
  26. frames = 0
  27. def _FPS():
  28.     global fps,frames
  29.     fps,frames = frames,0
  30.     root.after(1000,_FPS)
  31. _FPS()
  32.  
  33. ball="38pxgreenball.png"
  34.  
  35. try:
  36.     img = ImageTk.PhotoImage(file=ball)
  37.     print 'Images copied from URL to drive'
  38. except:
  39.     URL = 'http://tinyurl.com/38pxgreenball'
  40.     u = urllib2.urlopen(URL)
  41.     raw_data = u.read()
  42.     u.close()
  43.     b64_data = base64.encodestring(raw_data)
  44.     img = ''
  45.     while img == '':
  46.         try:
  47.             img = PhotoImage(data=b64_data)
  48.         except: pass
  49.     f = open(ball,'wb')
  50.     f.write(raw_data)
  51.     f.close()
  52.  
  53. def keyrelease(event):
  54.     global go
  55.     if event.char == "q":
  56.         go = False ### to exit
  57.         root.destroy()
  58.  
  59. root.bind("<KeyRelease>", keyrelease)
  60.  
  61. while go:
  62.     time.sleep(0.025)
  63.     canvas.delete("all")
  64.     for i in range(len(ballrack)):
  65.         loc,ballvel=ballrack[i]
  66.         x=loc[0]+ballvel[0];y=loc[1]+ballvel[1]
  67.         loc=(x,y)
  68.         ball = canvas.create_image(loc[0], loc[1], image=img)
  69.         if canvas.coords(ball)[0] <= radius or canvas.coords(ball)[0] >= width - radius:
  70.             ballvel = ballvel[0]*-1,ballvel[1]
  71.         if canvas.coords(ball)[1] - 20 <= radius or canvas.coords(ball)[1] >= height - radius:
  72.             ballvel = ballvel[0],ballvel[1]*-1
  73.         ballrack[i] = ((loc),(ballvel))
  74.     ballrelease += 1
  75.     if ballrelease > 5:
  76.         ballrelease = 0
  77.         sprites += 1
  78.         ballrack.append(((400,300),(random.randrange(3,12), random.randrange(3,12))))
  79.     canvas.create_text(25, 10, text='FPS = %s' % fps)
  80.     canvas.create_text(200, 10, text='Number Of Sprites = %s' % sprites)
  81.     frames += 1
  82.     canvas.update()
  83.  
  84. root.mainloop()
Add Comment
Please, Sign In to add comment