Mostrando entradas con la etiqueta stopWatch. Mostrar todas las entradas
Mostrando entradas con la etiqueta stopWatch. Mostrar todas las entradas

lunes, 13 de octubre de 2014

Para el reloj!

Juego "parar el reloj" cuando los milisegundos lleguen a 0. En python, probar codigo en CodeSkulptor, Numero de aciertos/Numero de intentos. try it!



# template for "Stopwatch: The Game"
import simplegui

# define global variables
output = "0:00.0"
record = "0/0"
interval=100
t=0
aciertos=0
count=0
flag = False

# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
    minutes = 0
    seconds = 0
    
    tenthS = (int(t)%10)
    if t>9:
        seconds = (int(t)/10)%60
    if t>599:
        minutes = (int(t)/60)/10
    if seconds <= 9:
        seconds=str(0)+str(seconds)
    
    return str(minutes)+":"+str(seconds)+"."+str(tenthS)
    
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
    timer.start()
    
def stop():
    global output,record,count,aciertos,flag
    
    timer.stop()
    
    if flag == False:
        if output[5] == "0":
            aciertos=aciertos+1
           
        count=count+1
        record=str(aciertos)+"/"+str(count)
    flag=True
    
def reset():
    global record,output,t
    
    timer.stop()
    record="0/0"
    output=format("0")
    t=0
    
# define event handler for timer with 0.1 sec interval
def timer():
    global t,output,flag
    flag=False
    t=t+1;
    
    output=format(t)
    

# define draw handler
def draw(canvas):
    global output,record
    canvas.draw_text(output,[100,100],24,"White")
    canvas.draw_text(record,[366,24],24,"Green")
    
# create frame
frame  = simplegui.create_frame("StopWatch!",400,200)

# register event handlers
frame.set_draw_handler(draw)
frame.add_button("Start",start,100)
frame.add_button("Stop",stop,100)
frame.add_button("Reset",reset,100)
timer=simplegui.create_timer(interval,timer)
# start frame
frame.start()