correct curses usage

master
panki27 7 years ago
parent 4a063bbe26
commit 73a2eb0c83

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

Loading…
Cancel
Save