correct curses usage

master
panki27 7 years ago
parent 2aff33fa79
commit 35b88812e8

@ -1,10 +1,10 @@
#!/usr/bin/env python2 #!/usr/bin/env python3
#TODO: Color, show controls, command line param
import readline, random, io, sys, time, os import readline, random, io, sys, time, os
import curses import curses
stdscr = curses.initscr() from curses import wrapper
curses.noecho()
curses.cbreak()
NOTHING = 0 NOTHING = 0
MINE = -1 MINE = -1
@ -14,6 +14,8 @@ FLAG_MINE = -4
STARTTIME = 0 STARTTIME = 0
SCREEN = 0
firstmove = False firstmove = False
FIELD_GENERATED = False FIELD_GENERATED = False
@ -23,9 +25,20 @@ FIELDS_CLEARED = 0
width, height = 9, 9 width, height = 9, 9
MINECOUNT = 10 MINECOUNT = 10
FLAGCOUNT = 0 FLAGCOUNT = 0
#put logic for program param here if len(sys.argv) > 1:
if len(sys.argv) == 4:
width = int(sys.argv[1])
height = int(sys.argv[2])
MINECOUNT = int(sys.argv[3])
else:
print('Specify parameters as width height minecount (for example ./minebash.py 5 5 7)')
sys.exit(0)
playfield = [[UNKNOWN for x in range(width)] for y in range(height)] playfield = [[UNKNOWN for x in range(width)] for y in range(height)]
#stdscr = curses.initscr()
#curses.noecho()
#curses.cbreak()
headline = '.' headline = '.'
midline = '|' midline = '|'
tailline = '\'' tailline = '\''
@ -38,6 +51,14 @@ def setup_strings(colcount):
midline += '---|' midline += '---|'
tailline += '---\'' tailline += '---\''
def endgame(msg=''):
# curses.nocbreak()
# stdscr.keypad(False)
# curses.echo()
#curses.endwin()
if msg != '':
print(msg)
sys.exit(0)
def calculate_hint(col, row): def calculate_hint(col, row):
hint = 0 hint = 0
@ -72,58 +93,58 @@ def setup_playfield(w, h, x, y):
STARTTIME = time.time() STARTTIME = time.time()
def gameover(win): def gameover(win):
stdscr.clear() SCREEN.clear()
if not win: if not win:
stdscr.addstr(0, 0, ' ________________') SCREEN.addstr(0, 0, ' ________________')
stdscr.addstr(1, 0, ' ____/ ( ( ) ) \___') SCREEN.addstr(1, 0, ' ____/ ( ( ) ) \___')
stdscr.addstr(2, 0, ' /( ( ( ) _ )) ) )\ ') SCREEN.addstr(2, 0, ' /( ( ( ) _ )) ) )\ ')
stdscr.addstr(3, 0, ' (( ( )( ) ) ( ) )') SCREEN.addstr(3, 0, ' (( ( )( ) ) ( ) )')
stdscr.addstr(4, 0, ' ((/ ( _( ) ( _) ) ( () ) )') SCREEN.addstr(4, 0, ' ((/ ( _( ) ( _) ) ( () ) )')
stdscr.addstr(5, 0, ' ( ( ( (_) (( ( ) .((_ ) . )_') SCREEN.addstr(5, 0, ' ( ( ( (_) (( ( ) .((_ ) . )_')
stdscr.addstr(6, 0, ' ( ( ) ( ( ) ) ) . ) ( )') SCREEN.addstr(6, 0, ' ( ( ) ( ( ) ) ) . ) ( )')
stdscr.addstr(7, 0, ' ( ( ( ( ) ( _ ( _) ). ) . ) ) ( )') SCREEN.addstr(7, 0, ' ( ( ( ( ) ( _ ( _) ). ) . ) ) ( )')
stdscr.addstr(8, 0, ' ( ( ( ) ( ) ( )) ) _)( ) ) )') SCREEN.addstr(8, 0, ' ( ( ( ) ( ) ( )) ) _)( ) ) )')
stdscr.addstr(9, 0, ' ( ( ( \ ) ( (_ ( ) ( ) ) ) ) )) ( )') SCREEN.addstr(9, 0, ' ( ( ( \ ) ( (_ ( ) ( ) ) ) ) )) ( )')
stdscr.addstr(10, 0, ' ( ( ( ( (_ ( ) ( _ ) ) ( ) ) )') SCREEN.addstr(10, 0, ' ( ( ( ( (_ ( ) ( _ ) ) ( ) ) )')
stdscr.addstr(11, 0, ' ( ( ( ( ( ) (_ ) ) ) _) ) _( ( )') SCREEN.addstr(11, 0, ' ( ( ( ( ( ) (_ ) ) ) _) ) _( ( )')
stdscr.addstr(12, 0, ' (( ( )( ( _ ) _) _(_ ( (_ )') SCREEN.addstr(12, 0, ' (( ( )( ( _ ) _) _(_ ( (_ )')
stdscr.addstr(13, 0, ' (_((__(_(__(( ( ( | ) ) ) )_))__))_)___)') SCREEN.addstr(13, 0, ' (_((__(_(__(( ( ( | ) ) ) )_))__))_)___)')
stdscr.addstr(14, 0, ' ((__) \\||lll|l||/// \_))') SCREEN.addstr(14, 0, ' ((__) \\||lll|l||/// \_))')
stdscr.addstr(15, 0, ' ( /(/ ( ) ) )\ )') SCREEN.addstr(15, 0, ' ( /(/ ( ) ) )\ )')
stdscr.addstr(16, 0, ' ( ( ( ( | | ) ) )\ )') SCREEN.addstr(16, 0, ' ( ( ( ( | | ) ) )\ )')
stdscr.addstr(17, 0, ' ( /(| / ( )) ) ) )) )') SCREEN.addstr(17, 0, ' ( /(| / ( )) ) ) )) )')
stdscr.addstr(18, 0, ' ( ( ((((_(|)_))))) )') SCREEN.addstr(18, 0, ' ( ( ((((_(|)_))))) )')
stdscr.addstr(19, 0, ' ( ||\(|(|)|/|| )') SCREEN.addstr(19, 0, ' ( ||\(|(|)|/|| )')
stdscr.addstr(20, 0, ' ( |(||(||)|||| )') SCREEN.addstr(20, 0, ' ( |(||(||)|||| )')
stdscr.addstr(21, 0, ' ( //|/l|||)|\\ \ )') SCREEN.addstr(21, 0, ' ( //|/l|||)|\\ \ )')
stdscr.addstr(22, 0, ' (/ / // /|//||||\\ \ \ \ _)') SCREEN.addstr(22, 0, ' (/ / // /|//||||\\ \ \ \ _)')
stdscr.addstr(23, 0, ' You lose! Press q to quit.') SCREEN.addstr(23, 0, ' You lose! Press q to quit.')
else: else:
now = time.time() now = time.time()
elapsed = now - STARTTIME elapsed = now - STARTTIME
mins = elapsed / 60 mins = elapsed / 60
secs = elapsed % 60 secs = elapsed % 60
winstr = 'You win! It took you {}:{} to bash the field!'.format(int(mins), str(round(secs, 2)).zfill(5)) winstr = 'You win! It took you {}:{} to bash the field!'.format(int(mins), str(round(secs, 2)).zfill(5))
stdscr.addstr(0, 0, ' /$$ /$$ /$$ /$$ /$$$$$$$ /$$') SCREEN.addstr(0, 0, ' /$$ /$$ /$$ /$$ /$$$$$$$ /$$')
stdscr.addstr(1, 0, '| $$ /$ | $$ | $$| $$ | $$__ $$ | $$') SCREEN.addstr(1, 0, '| $$ /$ | $$ | $$| $$ | $$__ $$ | $$')
stdscr.addstr(2, 0, '| $$ /$$$| $$ /$$$$$$ | $$| $$ | $$ \ $$ /$$$$$$ /$$$$$$$ /$$$$$$ | $$') SCREEN.addstr(2, 0, '| $$ /$$$| $$ /$$$$$$ | $$| $$ | $$ \ $$ /$$$$$$ /$$$$$$$ /$$$$$$ | $$')
stdscr.addstr(3, 0, '| $$/$$ $$ $$ /$$__ $$| $$| $$ | $$ | $$ /$$__ $$| $$__ $$ /$$__ $$| $$') SCREEN.addstr(3, 0, '| $$/$$ $$ $$ /$$__ $$| $$| $$ | $$ | $$ /$$__ $$| $$__ $$ /$$__ $$| $$')
stdscr.addstr(4, 0, '| $$$$_ $$$$| $$$$$$$$| $$| $$ | $$ | $$| $$ \ $$| $$ \ $$| $$$$$$$$|__/') SCREEN.addstr(4, 0, '| $$$$_ $$$$| $$$$$$$$| $$| $$ | $$ | $$| $$ \ $$| $$ \ $$| $$$$$$$$|__/')
stdscr.addstr(5, 0, '| $$$/ \ $$$| $$_____/| $$| $$ | $$ | $$| $$ | $$| $$ | $$| $$_____/ ') SCREEN.addstr(5, 0, '| $$$/ \ $$$| $$_____/| $$| $$ | $$ | $$| $$ | $$| $$ | $$| $$_____/ ')
stdscr.addstr(6, 0, '| $$/ \ $$| $$$$$$$| $$| $$ | $$$$$$$/| $$$$$$/| $$ | $$| $$$$$$$ /$$') SCREEN.addstr(6, 0, '| $$/ \ $$| $$$$$$$| $$| $$ | $$$$$$$/| $$$$$$/| $$ | $$| $$$$$$$ /$$')
stdscr.addstr(7, 0, '|__/ \__/ \_______/|__/|__/ |_______/ \______/ |__/ |__/ \_______/|__/') SCREEN.addstr(7, 0, '|__/ \__/ \_______/|__/|__/ |_______/ \______/ |__/ |__/ \_______/|__/')
stdscr.addstr(10, 0, winstr) SCREEN.addstr(10, 0, winstr)
stdscr.addstr(11,0, 'Press q to quit!') SCREEN.addstr(11,0, 'Press q to quit!')
while True: while True:
key = stdscr.getch() key = SCREEN.getch()
if key == ord('q'): if key == ord('q'):
sys.exit(0) endgame()
#if key == ord('r'): #if key == ord('r'):
# os.execl(sys.executable, sys.executable, *sys.argv) # os.execl(sys.executable, sys.executable, *sys.argv)
def print_playfield(playfield): def print_playfield(playfield, screen):
currentline = 0 currentline = 0
stdscr.addstr(currentline, 0, headline) screen.addstr(currentline, 0, headline)
currentline +=1 currentline +=1
#print headline #print headline
for rowindex, row in enumerate(playfield): for rowindex, row in enumerate(playfield):
@ -152,15 +173,12 @@ def print_playfield(playfield):
else: else:
rowstring+=' |' rowstring+=' |'
#print rowstring #print rowstring
stdscr.addstr(currentline, 0, rowstring) screen.addstr(currentline, 0, rowstring)
currentline +=1 currentline +=1
if(rowindex < len(row)-1): if(rowindex < len(row)-1):
stdscr.addstr(currentline, 0, midline) screen.addstr(currentline, 0, midline)
currentline +=1 currentline +=1
#print midline screen.addstr(currentline, 0, tailline)
#print currentline
#print tailline
stdscr.addstr(currentline, 0, tailline)
currentline +=1 currentline +=1
#print tailline #print tailline
@ -226,19 +244,20 @@ def handle_input(k):
else: else:
hit(CURSOR_POSITION[0], CURSOR_POSITION[1]) hit(CURSOR_POSITION[0], CURSOR_POSITION[1])
def print_score(): def print_score(screen):
scorestr = 'Mines: {} Flags: {}'.format(MINECOUNT, FLAGCOUNT) scorestr = 'Mines: {} Flags: {}'.format(MINECOUNT, FLAGCOUNT)
stdscr.addstr(19 ,0,scorestr) screen.addstr(19 ,0,scorestr)
def main(stdscr): def main(stdscr):
global SCREEN
SCREEN = stdscr
stdscr.clear() stdscr.clear()
setup_strings(width) setup_strings(width)
#generate mines: #generate mines:
#print_playfield(playfield) #print_playfield(playfield)
#TODO: user input #TODO: user input
while(True): while(True):
print_playfield(playfield) print_playfield(playfield, stdscr)
key = stdscr.getch() key = stdscr.getch()
handle_input(key) handle_input(key)
if (firstmove) and not (FIELD_GENERATED): if (firstmove) and not (FIELD_GENERATED):
@ -246,13 +265,9 @@ def main(stdscr):
handle_input(key) handle_input(key)
STARTTIME = time.time() STARTTIME = time.time()
check_score() check_score()
print_score() print_score(stdscr)
stdscr.refresh() stdscr.refresh()
#stdscr.getkey() #stdscr.getkey()
if __name__ == "__main__": if __name__ == "__main__":
curses.wrapper(main) wrapper(main)
#finally:
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()

Loading…
Cancel
Save