Skip to content

Instantly share code, notes, and snippets.

@leif
Created September 9, 2012 07:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leif/3683230 to your computer and use it in GitHub Desktop.
Save leif/3683230 to your computer and use it in GitHub Desktop.
timekeeperville
"timekeeperville is a game where you must keep time"
# by Leif Ryge, September 2012, WTFPL
from time import time
from collections import deque
results = []
while True:
adj = raw_input("Use default settings? [Yn] ") == "n"
bpm = int(adj and raw_input("Initial BPM? [120] ") or 120)
level = int(adj and raw_input("Initial level? [1] ") or 1 )
limit = int(adj and raw_input("Initial time limit? [15] ") or 15 )
base = int(adj and raw_input("Window size base? [2] ") or 2 )
pause = int(adj and raw_input("Inactivity threshold? [2] ") or 2 )
accel = int(adj and raw_input("Time to accelerate? [180] ") or 180)
invrt = adj and raw_input("Invert row length? [yN] ") == "y"
alt = adj and raw_input("Alternate display mode? [yN] ") == "y" or 0
prev, clock, streak, best, score, clear, data = 0, 0, 0, 0, 0, "\x1b[H\x1b[2J\x0d", [ deque([1], 1) ]
while True:
print clear,
delta = time()-prev
prev += delta
bps = bpm/60.0
if delta > pause:
print "\nInstructions: hit Enter %s times/minute to keep all rows at the target length."%bpm
raw_input("The rows represent your accuracy averaged over increasingly lengthy periods. \n")
continue
for window in data: window.append( invrt and 1.0/(delta*bps) or delta*bps )
if len(window) == base**(len(data)-1): data.append(deque(window, base**len(data)))
widths = [ int(level*sum(window)/len(window)+0.5) for window in data[:level] ]
win = len(data)>1 and [level]==list(set(widths))
streak = win and streak+1 or 0
best = max(streak, best)
limit = win and limit+level+streak or max(0, limit-delta)
clock += delta
if limit == 0: break
if win: score+=(limit+best)*streak
if win and streak<2 and limit>accel: bpm += 20
print "Level %-3s|%s| %s"%(level, "=!"[win]*level, streak>1 and "%s streak!"%streak or win and ":)" or "%s BPM is %s"%(bpm,('slower','faster')[delta*bps>1]))
print "\n".join("%%9s|%%%ds| %%s"%((alt or level>29) and level)%(len(window)," *.,-+:;% "[win or 2+((level/4)%8)]*width,len(window)==1 and "%d BPM"%(60/delta) or "<>="[width<level or width==level and 2 or 0]) for window,width in zip(data,widths))
if "q"==raw_input("%-6s%4s%s| Score: %d "%("%2d:%.2d"%(limit/60,limit%60),"[%s]"%best,"=!"[win]*level,score)): break
if win: level+=1
results.append( "%3s BPM, %dm %.2ds of play, level %02d, best streak was %s, score %d\n" % (bpm,clock/60,clock%60,level,best,score) )
while "y"!=raw_input("%s%s\n y to play again, or ctrl-c to quit\n " % (clear, "".join(reversed(results)))): pass
@EmbeddedLinuxGuy
Copy link

Nice, not sure if you can pull from my fork but here is my "pause game" feature:

diff --git a/timekeeperville.py b/timekeeperville.py
index 6dc2a8f..0fa06cd 100644
--- a/timekeeperville.py
+++ b/timekeeperville.py
@@ -36,4 +36,10 @@ while True:
minutes = (clock - seconds)/60
print "Level %s. After %dm %ds, cumulative mean speed is %.3f%% of target" % (level,minu
tes,seconds, 100/(1.0/(i/clock/rate)))
i += 1

  • raw_input()
  • if raw_input() == "z":
  •    print "Paused. Hit ENTER again to continue."
    
  •    before = time()
    
  •    raw_input()
    
  •    pause = time() - before
    
  •    start += pause
    
  •    prev += pause
    

@max-mapper
Copy link

a minimalist version of this game is something I like to call timecat. you time time cat into your terminal and then CTRL+C at exactly 5 seconds. whoever gets closest to 5 seconds wins

@leif
Copy link
Author

leif commented Sep 11, 2012

Thanks! I added your pause feature, but using "prev" instead of calling time again so that any time spent on the turn where z was typed also doesn't count.

@leif
Copy link
Author

leif commented Sep 11, 2012

New features in latest version:

  • Automatic pause
  • Configuration options
  • New display

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment