Guest User

Untitled

a guest
Mar 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. # template for "Stopwatch: The Game"
  2. # Run this project here:
  3. # define global variables
  4.  
  5. import simplegui
  6.  
  7. counter = 0
  8. gamecounter = 0
  9. gamewins = 0
  10. running = False
  11.  
  12.  
  13. # define helper function format that converts time
  14. # in tenths of seconds into formatted string A:BC.D
  15.  
  16. def format():
  17. global counter
  18. A = (counter // 600)
  19. B = ((counter % 600) / 100)
  20. C = (counter % 100) / 10
  21. D = counter % 10
  22. return str(A) + ":" + str(B) + str(C) + ":" + str(D)
  23.  
  24.  
  25. # define event handlers for buttons; "Start", "Stop", "Reset"
  26. def start():
  27. timer.start()
  28. global running
  29. running = True
  30.  
  31. def stop():
  32. global gamecounter
  33. global gamewins
  34. global running
  35. global counter
  36. if counter%10 == 0 and running:
  37. gamecounter = gamecounter + 1
  38. gamewins = gamewins + 1
  39. timer.stop()
  40. elif running:
  41. gamecounter = gamecounter + 1
  42. timer.stop()
  43. running = False
  44.  
  45. def reset():
  46. global counter
  47. global gamecounter
  48. global gamewins
  49. counter = 0
  50. gamecounter = 0
  51. gamewins = 0
  52. timer.stop()
  53.  
  54. def gamecounter_format():
  55. global gamecounter
  56. global gamewins
  57. return str(gamewins) + " / " + str(gamecounter)
  58.  
  59. def draw(canvas):
  60.  
  61. canvas.draw_text(format(), [100,100], 36, "White")
  62. canvas.draw_text(gamecounter_format(), [250, 20], 18, "Green")
  63.  
  64. # define event handler for timer with 0.1 sec interval
  65.  
  66. def timer_handler():
  67. global counter
  68. print counter
  69. counter += 1
  70. str(counter)
  71.  
  72. # create frame
  73. frame = simplegui.create_frame("Stopwatch Game", 300, 150)
  74. frame.add_button("Start", start, 200)
  75. frame.add_button("Stop", stop, 200)
  76. frame.add_button("Reset", reset, 200)
  77.  
  78. frame.set_draw_handler(draw)
  79.  
  80.  
  81. timer = simplegui.create_timer(100, timer_handler)
  82.  
  83. # start frame
  84. frame.start()
Add Comment
Please, Sign In to add comment