now restartable!

master
panki27 7 years ago
parent 9f37f76db8
commit 1a623d7cd5

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#TODO: restartable, highscore (?) #TODO: highscore (?), cursor wrapping
import random, io, sys, time, os import random, io, sys, time, os
@ -82,7 +82,7 @@ if len(sys.argv) > 1:
print(param_error) print(param_error)
sys.exit(0) 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)]
#playfield = 0
#stdscr = curses.initscr() #stdscr = curses.initscr()
#curses.noecho() #curses.noecho()
@ -105,10 +105,6 @@ def setup_strings(colcount):
tailline += '───┴' tailline += '───┴'
def endgame(msg=''): def endgame(msg=''):
# curses.nocbreak()
# stdscr.keypad(False)
# curses.echo()
#curses.endwin()
if msg != '': if msg != '':
print(msg) print(msg)
sys.exit(0) sys.exit(0)
@ -167,7 +163,7 @@ def gameover(win):
SCREEN.addstr(20, 0, ' ( |(||(||)|||| )') SCREEN.addstr(20, 0, ' ( |(||(||)|||| )')
SCREEN.addstr(21, 0, ' ( //|/l|||)|\\ \ )') SCREEN.addstr(21, 0, ' ( //|/l|||)|\\ \ )')
SCREEN.addstr(22, 0, ' (/ / // /|//||||\\ \ \ \ _)') SCREEN.addstr(22, 0, ' (/ / // /|//||||\\ \ \ \ _)')
SCREEN.addstr(23, 0, ' You lose! Press q to quit.') SCREEN.addstr(23, 0, ' You lose! Press Q to quit, or R to restart!')
else: else:
now = time.time() now = time.time()
elapsed = now - STARTTIME elapsed = now - STARTTIME
@ -183,12 +179,11 @@ def gameover(win):
SCREEN.addstr(6, 0, '| $$/ \ $$| $$$$$$$| $$| $$ | $$$$$$$/| $$$$$$/| $$ | $$| $$$$$$$ /$$') SCREEN.addstr(6, 0, '| $$/ \ $$| $$$$$$$| $$| $$ | $$$$$$$/| $$$$$$/| $$ | $$| $$$$$$$ /$$')
SCREEN.addstr(7, 0, '|__/ \__/ \_______/|__/|__/ |_______/ \______/ |__/ |__/ \_______/|__/') SCREEN.addstr(7, 0, '|__/ \__/ \_______/|__/|__/ |_______/ \______/ |__/ |__/ \_______/|__/')
SCREEN.addstr(10, 0, winstr) SCREEN.addstr(10, 0, winstr)
SCREEN.addstr(11,0, 'Press q to quit!') SCREEN.addstr(11,0, 'Press Q to quit, or R to restart!')
while True: while True:
key = SCREEN.getch() key = SCREEN.getch()
if key == ord('q'): if key == ord('q'): return False
endgame() elif key == ord('r'): return True
#if key == ord('r'):
# os.execl(sys.executable, sys.executable, *sys.argv) # os.execl(sys.executable, sys.executable, *sys.argv)
def print_playfield(playfield, screen): def print_playfield(playfield, screen):
@ -257,11 +252,12 @@ def hit(x, y, recursive_call=False):
if hint == 0: if hint == 0:
hit(i,j) hit(i,j)
elif playfield[y][x] == MINE: elif playfield[y][x] == MINE:
gameover(False) #gameover(False)
return True
def check_score(): def check_score():
if FIELDS_CLEARED == (width*height)-MINECOUNT: return FIELDS_CLEARED == (width*height)-MINECOUNT
gameover(True)
def place_flag(x, y): def place_flag(x, y):
global playfield, FLAGCOUNT global playfield, FLAGCOUNT
@ -300,7 +296,8 @@ def handle_input(k):
if not firstmove: if not firstmove:
firstmove = True firstmove = True
else: else:
hit(CURSOR_POSITION[0], CURSOR_POSITION[1]) return hit(CURSOR_POSITION[0], CURSOR_POSITION[1])
return False
def print_score(screen): def print_score(screen):
scorestr = 'Mines: {} Flags: {} Difficulty: {}'.format(MINECOUNT, FLAGCOUNT, difficulty) scorestr = 'Mines: {} Flags: {} Difficulty: {}'.format(MINECOUNT, FLAGCOUNT, difficulty)
@ -322,10 +319,18 @@ def setup_colors():
curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK) curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.init_pair(5, curses.COLOR_YELLOW, curses.COLOR_BLACK) curses.init_pair(5, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(7, curses.COLOR_WHITE, curses.COLOR_WHITE) curses.init_pair(7, curses.COLOR_WHITE, curses.COLOR_WHITE)
def reset():
global firstmove, playfield, FIELD_GENERATED, FIELDS_CLEARED, CURSOR_POSITION
firstmove = False
playfield = [[UNKNOWN for x in range(width)] for y in range(height)]
FIELD_GENERATED = False
FIELDS_CLEARED = 0
CURSOR_POSITION = [0,0]
def main(stdscr): def main(stdscr):
global SCREEN global SCREEN, firstmove, FIELD_GENERATED, FIELDS_CLEARED, playfield, CURSOR_POSITION
SCREEN = stdscr SCREEN = stdscr
stdscr.clear() stdscr.clear()
setup_strings(width) setup_strings(width)
@ -333,17 +338,29 @@ def main(stdscr):
#generate mines: #generate mines:
#TODO: user input #TODO: user input
while(True): while(True):
print_playfield(playfield, stdscr) reset()
key = stdscr.getch() while(True): #game loop
handle_input(key) print_playfield(playfield, stdscr)
if (firstmove) and not (FIELD_GENERATED): key = stdscr.getch()
setup_playfield(width, height, CURSOR_POSITION[0], CURSOR_POSITION[1]) if handle_input(key):
handle_input(key) restart = gameover(False) #user hit a mine
STARTTIME = time.time() if restart:
check_score() stdscr.clear()
print_score(stdscr) break
print_controls(stdscr) else: endgame()
stdscr.refresh() if (firstmove) and not (FIELD_GENERATED):
#stdscr.getkey() setup_playfield(width, height, CURSOR_POSITION[0], CURSOR_POSITION[1])
handle_input(key)
STARTTIME = time.time()
if check_score():
restart = gameover(True) # does user want restart?
if restart:
stdscr.clear()
break
else: endgame()
print_score(stdscr)
print_controls(stdscr)
stdscr.refresh()
#stdscr.getkey()
if __name__ == "__main__": if __name__ == "__main__":
wrapper(main) wrapper(main)

Loading…
Cancel
Save