First ever lines of code in Py. For testing go to cloud www.codesculptor.org and paste the code in canvas, then press run.
# template for "Stopwatch: The Game" import simplegui import random # define global variables message="0:00.0" position=[50,120] width=200 height=200 interval=100 counter=0 started=False counter_won=0 counter_stopped=0 # define helper function format that converts time # in tenths of seconds into formatted string A:BC.D def format_look(t): #pass t=t D=t-(t//10)*10 C=t//10-(t//100)*10 B=t//100 - (t//600)*6 A=t//600 if started == True: res_forman=str(A) + ':' + str(B) + str(C) + '.' + str(D) else: res_forman=message return res_forman # define event handlers for buttons; "Start", "Stop", "Reset" def update(text): #handler for text box global message message=text def start_handler(): global started #if started != True: #counter_started += 1 started=True timer.start() def stop_handler(): global started, counter_stopped, counter_won, counter timer.stop() if started != False: counter_stopped +=1 if counter//10==counter/10: counter_won +=1 counter -=1 started=False timer.stop() def reset_handler(): global started,message,counter,counter_won,counter_stopped counter_stopped=0 counter_won=0 started=False timer.stop() message="0:00.0" counter=0 #counter_started=counter # Handler to draw on canvas def draw(canvas): global counter_won, counter_stopped canvas.draw_text(message, position, 36, "Red") msg = str(counter_won)+ '/' + str(counter_stopped) canvas.draw_text(msg, (width-20,height-0.9*height), 10, "Red") # define event handler for timer with 0.1 sec interval def timer_handler(): #handler for timer global counter, message #x=random.randrange(0, width) #y=random.randrange(0, height) #position[0]=x #position[1]=y #print x,y counter +=1 message = format_look(counter) # define draw handler # Create a frame and assign callbacks to event handlers frame = simplegui.create_frame("Home", width, height) timer = simplegui.create_timer(interval, timer_handler) start = frame.add_button("Start", start_handler,100) stop = frame.add_button("Stop", stop_handler,100) reset = frame.add_button("Reset", reset_handler,100) frame.set_draw_handler(draw) #text = frame.add_input("Message:", update, 150) # Start the frame animation frame.start() timer.start()
One comment
Leave a Comment