icode4peace

Stopwatch: The game (Mini project)

Apr 19th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. # template for "Stopwatch: The Game"
  2. import simplegui
  3.  
  4. # define global variables
  5.  
  6. tenths_of_seconds = 0
  7. minutes = 0
  8. seconds = 0
  9. tens_of_seconds = 0
  10. width = 500
  11. height = 500
  12. total = 0
  13. x = 0
  14. y = 0
  15. z = 0.0
  16.  
  17. # define helper function format that converts time
  18. # in tenths of seconds into formatted string A:BC.D
  19. def format(t):
  20.     global tenths_of_seconds, minutes, tens_of_seconds, seconds
  21.     minutes = t//600
  22.     tenths_of_seconds = t%10
  23.     seconds = ((t//10)%60)//10
  24.     tens_of_seconds = ((t//10)%60)%10
  25.        
  26.     return str(minutes) + ":" + str(seconds) + str(tens_of_seconds)+ "." + str(tenths_of_seconds)
  27.    
  28.    
  29. # define event handlers for buttons; "Start", "Stop", "Reset"
  30.  
  31. def start():
  32.     timer.start()
  33.  
  34.    
  35. def stop():
  36.     global x, y, tenths_of_seconds, z
  37.     if timer.is_running():
  38.         x += 1
  39.         if tenths_of_seconds == 0:
  40.             y += 1
  41.     z = (float(y)/float(x))*100
  42.     timer.stop()    
  43.    
  44. def reset():
  45.     timer.stop()
  46.     global total, x, y, z
  47.     total = 0
  48.     x = 0
  49.     y = 0
  50.     z = 0.0
  51.    
  52.    
  53. # define event handler for timer with 0.1 sec interval
  54. def t_h():
  55.     global total
  56.     total += 1
  57.    
  58. # define draw handler
  59. def draw_handler(canvas):
  60.     canvas.draw_text(format(total), [200, height/2], 64, "White")
  61.     canvas.draw_text(str(x) + "/" + str(y), [445, 50], 24, "Yellow")
  62.     canvas.draw_text("Win percentage: " + str(round(z, 2)) + "%", [130, 450], 29, "Magenta")
  63.                      
  64. # create frame
  65. frame = simplegui.create_frame("Stopwatch: The Game", width, height)
  66.  
  67.  
  68. # register event handlers
  69.  
  70. frame.set_draw_handler(draw_handler)
  71. timer = simplegui.create_timer(100, t_h)
  72. button1 = frame.add_button("Start", start, 100)
  73. frame.add_label(" ")
  74. button1 = frame.add_button("Stop", stop, 100)
  75. frame.add_label(" ")
  76. button2 = frame.add_button("Reset", reset, 100)
  77.  
  78. # start frame
  79. frame.start()
Add Comment
Please, Sign In to add comment