def game_loop(stdscr, columns, rows, num_mines):
game = minesweeper.Game.create_random(columns, rows, num_mines)
Point = namedtuple('Point', ['x', 'y'], verbose=True)
cursor_pos = Point(0, 0)
while True:
stdscr.clear()
game_rect = draw_screen(stdscr, game)
# restrict cursor to the game field
cursor_pos = Point(
clamp(cursor_pos.x, game_rect.x, game_rect.x+game_rect.width-1),
clamp(cursor_pos.y, game_rect.y, game_rect.y+game_rect.height-1)
)
stdscr.move(cursor_pos.y, cursor_pos.x)
stdscr.refresh()
c = stdscr.getch()
if c == curses.KEY_LEFT:
cursor_pos = Point(cursor_pos.x-2, cursor_pos.y)
if c == curses.KEY_RIGHT:
cursor_pos = Point(cursor_pos.x+2, cursor_pos.y)
if c == curses.KEY_UP:
cursor_pos = Point(cursor_pos.x, cursor_pos.y-1)
if c == curses.KEY_DOWN:
cursor_pos = Point(cursor_pos.x, cursor_pos.y+1)
if c == curses.KEY_ENTER or c == 10:
game.toggle_mark(*cursor_to_index(cursor_pos, game_rect))
if c == " " or c == 32:
game.reveal(*cursor_to_index(cursor_pos, game_rect))
if c == 27: # Escape
selected = open_menu(stdscr, ["Continue", "New Game", "Exit"])
if selected == "Exit":
return
elif selected == "New Game":
columns, rows, num_mines = open_difficulty_menu(stdscr)
return game_loop(stdscr, columns, rows, num_mines)
if game.is_lost() or game.is_solved():
# reveal the complete solution
game.reveal_all()
stdscr.clear()
draw_screen(stdscr, game)
# wait for user to press any key
curses.curs_set(False)
c = stdscr.getch()
curses.curs_set(True)
break
评论列表
文章目录