Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # template for "Stopwatch: The Game"
- # Run this project here:
- # define global variables
- import simplegui
- counter = 0
- gamecounter = 0
- gamewins = 0
- running = False
- # define helper function format that converts time
- # in tenths of seconds into formatted string A:BC.D
- def format():
- global counter
- A = (counter // 600)
- B = ((counter % 600) / 100)
- C = (counter % 100) / 10
- D = counter % 10
- return str(A) + ":" + str(B) + str(C) + ":" + str(D)
- # define event handlers for buttons; "Start", "Stop", "Reset"
- def start():
- timer.start()
- global running
- running = True
- def stop():
- global gamecounter
- global gamewins
- global running
- global counter
- if counter%10 == 0 and running:
- gamecounter = gamecounter + 1
- gamewins = gamewins + 1
- timer.stop()
- elif running:
- gamecounter = gamecounter + 1
- timer.stop()
- running = False
- def reset():
- global counter
- global gamecounter
- global gamewins
- counter = 0
- gamecounter = 0
- gamewins = 0
- timer.stop()
- def gamecounter_format():
- global gamecounter
- global gamewins
- return str(gamewins) + " / " + str(gamecounter)
- def draw(canvas):
- canvas.draw_text(format(), [100,100], 36, "White")
- canvas.draw_text(gamecounter_format(), [250, 20], 18, "Green")
- # define event handler for timer with 0.1 sec interval
- def timer_handler():
- global counter
- print counter
- counter += 1
- str(counter)
- # create frame
- frame = simplegui.create_frame("Stopwatch Game", 300, 150)
- frame.add_button("Start", start, 200)
- frame.add_button("Stop", stop, 200)
- frame.add_button("Reset", reset, 200)
- frame.set_draw_handler(draw)
- timer = simplegui.create_timer(100, timer_handler)
- # start frame
- frame.start()
Add Comment
Please, Sign In to add comment