Advertisement
Guest User

pygame animation code. don't laugh.

a guest
Dec 9th, 2012
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. import pickle, sys
  2. import pygame
  3. from pygame.locals import *
  4. from time import sleep
  5. from math import sin, cos, pi, sqrt
  6.  
  7. vial = open('dat/cash_timeline.dat','r')
  8. cash = pickle.load(vial)
  9. vial.close()
  10.  
  11. vial = open('dat/flux_timeline.dat','r')
  12. flux = pickle.load(vial)
  13. vial.close()
  14.  
  15.  
  16. centerx, centery = 300,300#of the megacircle
  17. radius = 175#of the megacircle
  18. kay = 85**2/6855.#chips to radius conversion
  19.  
  20. if True:#define colors
  21.  
  22.     red = pygame.Color('#FF486C')
  23.     orange = pygame.Color('#FF7148')
  24.     yellow = pygame.Color('#FFF748')
  25.     green = pygame.Color('#95FF48')
  26.     blue = pygame.Color('#48C9FF')
  27.     violet = pygame.Color('#486CFF')
  28.     grey = pygame.Color('#A3A3A3')
  29.     graph = pygame.Color('#3A3A3A')
  30.     black = pygame.Color(0,0,0)
  31.     white = pygame.Color(255,255,255)#Color definition
  32.     colors=[red, orange, yellow, green, blue, violet, grey]
  33.  
  34. allPlayers = ['Brontosaur', 'Mongoose', 'Aardvark', 'FruitBat', 'MantaRay', 'Tapeworm', 'Ebola']#all together now!
  35.  
  36. def arr(x):#given x chips, calculate the radius of the circle;
  37.     return int(sqrt(kay*x))#holdings proportional to area
  38. def winner():#who wins the tournament?
  39.     for player in allPlayers:
  40.         if flux[player][hand] > 0:
  41.             return player
  42.  
  43. color_dict = {}
  44. place_dict = {}
  45. for i in range(0, len(allPlayers)):#give each player a color and a location.
  46.     color_dict[allPlayers[i]] = colors[i]#colors for everyone!
  47.     place_dict[allPlayers[i]] = (centerx+int(radius*cos(2*pi*i/len(allPlayers))), centery+int(radius*sin(2*pi*i/len(allPlayers))))
  48.  
  49.  
  50. pygame.init()
  51.  
  52. windowSurfObj = pygame.display.set_mode([700,700])
  53. pygame.display.set_caption('Poker Face')
  54. fontObj = pygame.font.Font('freesansbold.ttf', 24)
  55.  
  56.  
  57. hand=0#slight abuse of notation; counter to iterate thru time series
  58. while hand < 675:
  59.     windowSurfObj.fill(white)
  60.     win = winner()
  61.  
  62.     for playa in allPlayers:#draw your lines in the sand...
  63.         if flux[playa][hand] < 0.0:
  64.             wid = arr(abs(flux[playa][hand]))#does this correctly represent flow? ie, is area ~ flux? oh wellz...
  65.             pygame.draw.line(windowSurfObj, black, place_dict[playa], place_dict[win], wid)
  66.  
  67.     for playa in allPlayers:#circleround.
  68.         rad = arr(cash[playa][hand])#radius of circle
  69.         pygame.draw.circle(windowSurfObj, color_dict[playa], place_dict[playa], rad)#draw circles.
  70.  
  71.     for playa in allPlayers:#his name was robert paulson.
  72.         msgSurfObj = fontObj.render(playa, False, graph)
  73.         msgRectObj = msgSurfObj.get_rect()
  74.         msgRectObj.topleft = place_dict[playa]
  75.         windowSurfObj.blit(msgSurfObj, msgRectObj)
  76.        
  77.     for event in pygame.event.get():#event.get calls up all the events that have happened since the last time get was called.
  78.         if event.type == QUIT:#GTFO
  79.             pygame.quit()
  80.             sys.exit() 
  81.         elif event.type == KEYDOWN:#interact with the keyboard
  82.             if event.key == K_ESCAPE:
  83.                 pygame.event.post(pygame.event.Event(QUIT))#GTFO
  84.  
  85.     pygame.display.update()#update the display!
  86.     pygame.image.save(windowSurfObj, 'frm/frame%d.jpeg'%(hand))
  87.  
  88.     hand += 1#animate
  89.     #sleep(0.07)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement