Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_FramesPerSec_demo.py
- from Tkinter import *
- import time
- import random
- import urllib2
- import sys
- import base64
- from threading import Thread
- from PIL import ImageTk
- width = 800
- height = 600
- radius = 19
- root = Tk()
- root.geometry('800x600+0+0')
- root.title("FPS Demo")
- go = True
- ballrack = [((400,300),(5, 8))]
- fps = 0
- sprites = 1
- canvas = Canvas(root, width=800, height=600)
- canvas.pack()
- ballrelease = 0
- frames = 0
- def _FPS():
- global fps,frames
- fps,frames = frames,0
- root.after(1000,_FPS)
- _FPS()
- ball="38pxgreenball.png"
- try:
- img = ImageTk.PhotoImage(file=ball)
- print 'Images copied from URL to drive'
- except:
- URL = 'http://tinyurl.com/38pxgreenball'
- u = urllib2.urlopen(URL)
- raw_data = u.read()
- u.close()
- b64_data = base64.encodestring(raw_data)
- img = ''
- while img == '':
- try:
- img = PhotoImage(data=b64_data)
- except: pass
- f = open(ball,'wb')
- f.write(raw_data)
- f.close()
- def keyrelease(event):
- global go
- if event.char == "q":
- go = False ### to exit
- root.destroy()
- root.bind("<KeyRelease>", keyrelease)
- while go:
- time.sleep(0.025)
- canvas.delete("all")
- for i in range(len(ballrack)):
- loc,ballvel=ballrack[i]
- x=loc[0]+ballvel[0];y=loc[1]+ballvel[1]
- loc=(x,y)
- ball = canvas.create_image(loc[0], loc[1], image=img)
- if canvas.coords(ball)[0] <= radius or canvas.coords(ball)[0] >= width - radius:
- ballvel = ballvel[0]*-1,ballvel[1]
- if canvas.coords(ball)[1] - 20 <= radius or canvas.coords(ball)[1] >= height - radius:
- ballvel = ballvel[0],ballvel[1]*-1
- ballrack[i] = ((loc),(ballvel))
- ballrelease += 1
- if ballrelease > 5:
- ballrelease = 0
- sprites += 1
- ballrack.append(((400,300),(random.randrange(3,12), random.randrange(3,12))))
- canvas.create_text(25, 10, text='FPS = %s' % fps)
- canvas.create_text(200, 10, text='Number Of Sprites = %s' % sprites)
- frames += 1
- canvas.update()
- root.mainloop()
Add Comment
Please, Sign In to add comment