Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import random;random.seed()
  2. import json
  3. import time
  4. import pygame, pygame.gfxdraw
  5.  
  6. data = {}
  7. hue_shift = 1.5
  8. cycle_time = 10 * 1000
  9. saturation = 70
  10. lightness = 85
  11. stroke_size = 5
  12. cell_padding_width = 1.2
  13. cell_padding_height = 1.1
  14. txt_color = (255,255,255)
  15.  
  16. instructors = []
  17. actions = []
  18.  
  19. with open("pie.json") as f:
  20. data = json.load(f)
  21.  
  22. data["instructors"] = list(map(str.title,data.get('instructors',instructors)))
  23. instructors = data["instructors"]
  24. actions = list(map(str.capitalize,data.get("actions",actions)))
  25. data["actions"] = actions
  26. scores = data.get("scores",{})
  27. data["scores"] = scores
  28. for instructor in instructors:
  29. scores[instructor] = scores.get(instructor,{})
  30. for action in data["actions"]:
  31. scores[instructor][action] = scores[instructor].get(action,0)
  32.  
  33. def generate_actions():
  34. while True:
  35. axn = random.choice(actions)
  36. victim = random.choice(instructors)
  37. yield axn, victim
  38.  
  39. pygame.display.init()
  40. pygame.font.init()
  41.  
  42. font = pygame.font.SysFont(pygame.font.get_default_font(),54)
  43. clock = pygame.time.Clock()
  44. trigger = pygame.time.Clock()
  45. window = pygame.display.set_mode((0,0),pygame.FULLSCREEN|pygame.HWSURFACE)
  46. gen = generate_actions()
  47. width, height = window.get_size()
  48. center_x = width // 2
  49. center_y = width // 2
  50. delay = 30
  51. wait = 5 * 1000
  52. passed = wait
  53. alive = True
  54. fmtstr = "{} {}.".format
  55. color_rate = hue_shift // ((360//hue_shift))
  56. color_passed = 0
  57.  
  58. bg_color = pygame.Color(0,0,0)
  59. bg_color.hsla = (0,saturation,lightness,100)
  60. overlay_surf = pygame.Surface((width, height),pygame.SRCALPHA)
  61. overlay_surf.fill((0,0,0,60))
  62.  
  63. window.fill(bg_color)
  64. instructor = ""
  65. action = ""
  66. nsurf = None
  67.  
  68.  
  69.  
  70. action_surfs = {axn:font.render(axn,True,txt_color) for axn in actions+["Instructor"]}
  71. instructor_surfs = {person:font.render(person,True,txt_color) for person in instructors}
  72. twidth = 0
  73. theight = 0
  74. for action in action_surfs.values():
  75. twidth += action.get_width() * cell_padding_width + stroke_size
  76. theight = max(theight,action.get_height() * cell_padding_height + stroke_size)
  77. for instructor in instructor_surfs.values():
  78. theight += instructor.get_height() * cell_padding_height
  79.  
  80. #table = pygame.Surface((twidth * cell_padding_width, theight * cell_padding_height),pygame.SRCALPHA)
  81. #table.blit(action_surfs["Instructor"],())
  82. #pygame.gfxdraw.vline(table,action_surfs["Instructor"].get_width(),0,table.get_height(),txt_color)
  83.  
  84. while alive:
  85. nt = clock.tick(delay)
  86. passed += nt
  87. color_passed += nt
  88. if passed >= wait:
  89. passed = 0
  90. action,instructor = next(gen)
  91. msg = fmtstr(action,instructor)
  92. nsurf = font.render(msg,True,txt_color)
  93. overlay_surf = pygame.Surface((width, nsurf.get_height()*2),pygame.SRCALPHA)
  94. overlay_surf.fill((0,0,0,60))
  95. if color_passed >= color_rate:
  96. h,s,l,a = bg_color.hsla
  97. color_passed = 0
  98. h += hue_shift
  99. if h>=360:
  100. h = 0
  101. bg_color.hsla = (h,s,l,a)
  102. window.fill(bg_color)
  103. #window.blit(overlay_surf,(0,0))
  104. for event in pygame.event.get():
  105. if event.type == pygame.QUIT:
  106. alive = False
  107. if event.type == pygame.KEYDOWN:
  108. if event.key == pygame.K_ESCAPE:
  109. alive = False
  110. if event.key == pygame.K_SPACE:
  111. scores[instructor][action] += 1
  112. # draw the present action.
  113. x = (width-nsurf.get_width()) // 2
  114. y = (height-nsurf.get_height()) // 2
  115. window.blit(overlay_surf,(0,y-(nsurf.get_height())//2))
  116. window.blit(nsurf,(x,y))
  117. #window.blit(table,(0,0))
  118. pygame.display.flip()
  119.  
  120. with open("pie.json","w") as f:
  121. json.dump(data,f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement