#!/usr/bin/env python3 # Based on my TI-89 game of the same name, # which was based on a Flash game called Tontie. # Use the numpad to play! # Tested on macOS and Linux. And Windows. # Feel free to increase zoom in your terminal! #Scoring... # 10 points for a hit once # 25 points for hit twice # 20 points for 1-9 # https://docs.python.org/2/library/curses.html (macOS and Linux) # https://pypi.org/project/windows-curses/ (Windows) import curses import random import time ## start up curses screen = curses.initscr() curses.noecho() curses.cbreak() curses.curs_set(False) # turn off cursor # due to curses messing up the terminal if code not closed properly, # put the code in in a try block so that errors don't mess up terminal try: ## show start screen and setup screen screen.addstr(0,0,"'Hit' by Bradley Knockel") screen.addstr(1,0,"Press any key to continue") screen.addstr(3,0,"Press q to quit") screen.addstr(4,0,"Press 1-9 to hit") screen.addstr(5,0," O hit once") screen.addstr(6,0," o hit twice") screen.addstr(7,0," @ don't hit") screen.addstr(8,0," 1-9 hit number") screen.refresh() screen.getch() screen.clear() screen.addstr(0,0,"SCORE LEVEL") screen.addstr(1,0,"0") screen.addstr(1,11,"0") for i in [3,4,5]: screen.addstr(i,3,"[ ] [ ] [ ]") screen.refresh() ## initialize variables x=0 # counter used for leveling up y=0 # score d="123456789Oo@ " # string that stores the various symbols that can appear e=-1 # location to be cleared or e-9 is the location for a "o" to reappear g=10 # gives the level via 10-g offset = 49 # so that screen.getch()-offset returns number-key pressed sleepTime = 0.1 # in seconds # list of times left for each of the 9 locations z=[-1,-1,-1,-1,-1,-1,-1,-1,-1] # list for the type of symbol in each of 9 locations t=[0,0,0,0,0,0,0,0,0] # 0 is a blank location # 1 through 12 is as variable d shows # 13 is an 11 that needs just one more hit #list of locations of the 1-9 symbols for each of the 9 symbols f=[-1,-1,-1,-1,-1,-1,-1,-1,-1] # list storing rows and columns of the 9 locations on screen c = [5,5,5,4,4,4,3,3,3,4,8,12,4,8,12,4,8,12] ## setup a few things screen.nodelay(True) random.seed() ## start main loop (ends upon "break" statement) while True: # check if you've lost or should remove a "@" if 0 in z: for b in range(9): # find location of 0 if z[b]==0: a=b if t[a]!=12: # you lose! screen.addstr(2,0,"waited too long!") break else: # remove a "@" screen.addstr(c[a],c[a+9]," ") t[a]=0 for b in range(8,-1,-1): # possibly find location of another 0 if z[b]==0: a=b if t[a]!=12 and t[a]!=0: # you lose! screen.addstr(2,0,"waited too long!") break else: # remove a "@" screen.addstr(c[a],c[a+9]," ") t[a]=0 # refresh screen and sleep screen.refresh() time.sleep(sleepTime) # possibly add a new symbol to the screen a=random.randint(0,38+3*g) if a<9: if z[a]<-1: z[a]=30+g t[a]=random.randint(1,4)+9 if t[a]>12: # add a 1-9 symbol b=random.randint(0,8) if f[b]==-1 and a!=b: t[a]=b+1 f[b]=a else: # do not create any symbol if didn't work z[a]=-1 t[a]=13 screen.addstr(c[a],c[a+9],d[t[a]-1]) if t[a]==13: t[a]=0 # update a "*" using variable e if e>-1 and e<9: screen.addstr(c[e],c[e+9]," ") if e>8: screen.addstr(c[e-9],c[e],"o") e=-1 # check if a level-up is in order x+=1 if x==50 and g!=-10: g=g-1 screen.addstr(1,11,str(10-g)) x=0 # decrease the timer z[:] = [i-1 for i in z] # check for a key press and deal with the consequences a=screen.getch()-offset if a==ord('q')-offset: break # quit if "q" was pressed if a>-1 and a<9: b=f[a] if b>-1: # deal with 1-9 symbols first y+=20 screen.addstr(c[b],c[b+9],"*") f[a]=-1 e=b t[b]=0 z[b]=-1 elif t[a]==10 or t[a]==13: # a "o" that needs one more hit or an "O" y+=10 if t[a]==13: y+=15 screen.addstr(c[a],c[a+9],"*") e=a t[a]=0 z[a]=-1 elif t[a]==11: # a "o" that needs two more hits screen.addstr(c[a],c[a+9],"*") t[a]=13 e=a+9 else: # you lose! screen.addstr(2,0,"pressed wrong key!") break screen.addstr(1,0,str(y)) ## end things screen.nodelay(False) if a!=ord('q')-offset: screen.addstr(6,0,"GAME OVER") screen.addstr(7,0,"press q to quit") screen.addstr(c[a],c[a+9]-1,"(") screen.addstr(c[a],c[a+9]+1,")") screen.refresh() while screen.getch()!=ord('q'): pass except: pass ## close down curses curses.curs_set(True) curses.nocbreak() curses.echo() curses.endwin()