def main():
game = Game(16)
while True:
game.refresh_screen()
new_color = game.get_key()
if new_color > 0 and new_color <= COLORS and game.status!='win':
old_color = game.arena[0][0]
if new_color != old_color:
game.moves+=1
game.paint( [0,0], old_color, new_color)
elif new_color==-2:
break
elif new_color==-1:
game.display_help()
game.screen.keypad(False)
curses.nocbreak()
curses.echo()
curses.endwin()
print('Thanks for playing!')
exit()
python类nocbreak()的实例源码
def top(args):
scr = curses.initscr()
#scr.start_color()
curses.noecho()
curses.cbreak()
hx, wm = scr.getmaxyx()
scr.keypad(True)
try:
header = curses.newwin(HEADER_SIZE, wm, 0, 0)
body = curses.newwin(BODY_SIZE, wm, HEADER_SIZE, 0)
while True:
draw_header(header)
draw_body(body)
draw_footer(scr)
sleep(0.2)
except KeyboardInterrupt:
curses.nocbreak()
scr.keypad(False)
curses.echo()
curses.endwin()
def leave(self):
'''Leave curses mode.'''
curses.nocbreak()
curses.echo()
self.window.keypad(False)
try:
curses.curs_set(self.old_cursor)
except:
pass
curses.endwin()
def __exit__(self, exc_type, exc_value, traceback):
""" reset terminal settings and de-init curses """
self.stdscr.keypad(0)
curses.nocbreak()
curses.echo()
curses.endwin()
def finalize(self):
curses.nocbreak()
curses.echo()
curses.endwin()
def cleanup():
curses.nocbreak()
curses.echo()
curses.endwin()
pi.stop()
def wrapper(func, *args, **kwds):
"""Wrapper function that initializes curses and calls another function,
restoring normal keyboard/screen behavior on error.
The callable object 'func' is then passed the main window 'stdscr'
as its first argument, followed by any other arguments passed to
wrapper().
"""
try:
# Initialize curses
stdscr = curses.initscr()
# Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input
curses.noecho()
curses.cbreak()
# In keypad mode, escape sequences for special keys
# (like the cursor keys) will be interpreted and
# a special value like curses.KEY_LEFT will be returned
stdscr.keypad(1)
# Start color, too. Harmless if the terminal doesn't have
# color; user can test with has_color() later on. The try/catch
# works around a minor bit of over-conscientiousness in the curses
# module -- the error return from C start_color() is ignorable.
try:
curses.start_color()
except:
pass
return func(stdscr, *args, **kwds)
finally:
# Set everything back to normal
if 'stdscr' in locals():
stdscr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
def _stopWindow(stdscr):
stdscr.erase()
stdscr.refresh()
stdscr.keypad(0)
curses.echo()
curses.curs_set(1)
curses.nocbreak()
curses.endwin()
def _cleanup():
curses.echo()
curses.curs_set(1)
curses.nocbreak()
curses.endwin()
traceback.print_exc()
def teardown(win):
"""
returns console to normal state
:param win: the window
"""
# tear down the console
curses.nocbreak()
if win: win.keypad(0)
curses.echo()
curses.endwin()
def __exit__(self, exc_type, exc_val, exc_tb):
self.window.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
def __exit__(self, exc_type, exc_val, exc_tb):
self.window.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
def cleanup(gui, poll, injector, ts, tests, command_line, args):
ts.run = False
if gui:
gui.stop()
if poll:
poll.stop()
if injector:
injector.stop()
'''
# doesn't work
if gui:
for (i, c) in enumerate(gui.orig_colors):
curses.init_color(i, c[0], c[1], c[2])
'''
curses.nocbreak();
curses.echo()
curses.endwin()
dump_artifacts(tests, injector, command_line)
if args.save:
with open(LAST, "w") as f:
f.write(hexlify(cstr2py(tests.r.raw_insn)))
sys.exit(0)
def stop(self):
curses.nocbreak();
curses.echo()
curses.endwin()
def __del__(self):
"""
Reset terminal before quit.
"""
curses.nocbreak()
curses.echo()
curses.endwin()
def run(self):
'''
Runs all the windows added to the application, and returns a `Result`
object.
'''
result = Result()
try:
self.scr = curses.initscr()
self.MAX_HEIGHT, self.MAX_WIDTH = self.scr.getmaxyx()
curses.noecho()
curses.cbreak()
curses.start_color()
curses.use_default_colors()
self.window = self.scr.subwin(0, 0)
self.window.keypad(1)
self.window.nodelay(1)
self._run_windows()
self.threads += [gevent.spawn(self._input_loop)]
gevent.joinall(self.threads)
for thread in self.threads:
if thread.exception:
result._extract_thread_exception(thread)
except KeyboardInterrupt:
result._extract_exception()
except Exception:
result._extract_exception()
finally:
if self.scr is not None:
self.scr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
return result
def wrapper(func, *args, **kwds):
"""Wrapper function that initializes curses and calls another function,
restoring normal keyboard/screen behavior on error.
The callable object 'func' is then passed the main window 'stdscr'
as its first argument, followed by any other arguments passed to
wrapper().
"""
try:
# Initialize curses
stdscr = curses.initscr()
# Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input
curses.noecho()
curses.cbreak()
# In keypad mode, escape sequences for special keys
# (like the cursor keys) will be interpreted and
# a special value like curses.KEY_LEFT will be returned
stdscr.keypad(1)
# Start color, too. Harmless if the terminal doesn't have
# color; user can test with has_color() later on. The try/catch
# works around a minor bit of over-conscientiousness in the curses
# module -- the error return from C start_color() is ignorable.
try:
curses.start_color()
except:
pass
return func(stdscr, *args, **kwds)
finally:
# Set everything back to normal
if 'stdscr' in locals():
stdscr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
def __del__(self):
curses.nocbreak()
curses.echo()
curses.endwin()
# print('done')
def close_curses():
mainwindow.keypad(0)
curses.echo()
curses.nocbreak()
curses.curs_set(2)
curses.endwin()
def on_finish(self):
global interpreter
if self.debug and not self.compat_debug:
curses.nocbreak()
self.stdscr.keypad(False)
curses.echo()
curses.endwin()
# sys.exit(0)
def cpstop(self):
if self.use_curses:
curses.nocbreak()
self.scrn.keypad(0)
curses.echo()
curses.endwin()
def wrapper(func, *args, **kwds):
"""Wrapper function that initializes curses and calls another function,
restoring normal keyboard/screen behavior on error.
The callable object 'func' is then passed the main window 'stdscr'
as its first argument, followed by any other arguments passed to
wrapper().
"""
try:
# Initialize curses
stdscr = curses.initscr()
# Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input
curses.noecho()
curses.cbreak()
# In keypad mode, escape sequences for special keys
# (like the cursor keys) will be interpreted and
# a special value like curses.KEY_LEFT will be returned
stdscr.keypad(1)
# Start color, too. Harmless if the terminal doesn't have
# color; user can test with has_color() later on. The try/catch
# works around a minor bit of over-conscientiousness in the curses
# module -- the error return from C start_color() is ignorable.
try:
curses.start_color()
except:
pass
return func(stdscr, *args, **kwds)
finally:
# Set everything back to normal
if 'stdscr' in locals():
stdscr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
def create_screen(fn):
"""
Initializes curses and passes a Screen to the main loop function `fn`.
Based on curses.wrapper.
"""
try:
# Make escape key more responsive
os.environ['ESCDELAY'] = '25'
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)
stdscr.nodelay(1)
curses.curs_set(0)
curses.mousemask(curses.BUTTON1_CLICKED)
if curses.has_colors():
curses.start_color()
curses.use_default_colors()
screen = Screen(stdscr)
fn(screen)
finally:
# Set everything back to normal
if 'stdscr' in locals():
curses.use_default_colors()
curses.curs_set(1)
stdscr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
def wrapper(func, *args, **kwds):
"""Wrapper function that initializes curses and calls another function,
restoring normal keyboard/screen behavior on error.
The callable object 'func' is then passed the main window 'stdscr'
as its first argument, followed by any other arguments passed to
wrapper().
"""
try:
# Initialize curses
stdscr = curses.initscr()
# Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input
curses.noecho()
curses.cbreak()
# In keypad mode, escape sequences for special keys
# (like the cursor keys) will be interpreted and
# a special value like curses.KEY_LEFT will be returned
stdscr.keypad(1)
# Start color, too. Harmless if the terminal doesn't have
# color; user can test with has_color() later on. The try/catch
# works around a minor bit of over-conscientiousness in the curses
# module -- the error return from C start_color() is ignorable.
try:
curses.start_color()
except:
pass
return func(stdscr, *args, **kwds)
finally:
# Set everything back to normal
if 'stdscr' in locals():
stdscr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
def close(self):
if not self.enable:
return
curses.nocbreak()
self.stdscr.keypad(0)
curses.echo()
curses.endwin()
def deinit(self):
curses.nocbreak()
self.screen.keypad(False)
curses.echo()
curses.endwin()
def wrapper(func, *args, **kwds):
"""Wrapper function that initializes curses and calls another function,
restoring normal keyboard/screen behavior on error.
The callable object 'func' is then passed the main window 'stdscr'
as its first argument, followed by any other arguments passed to
wrapper().
"""
try:
# Initialize curses
stdscr = curses.initscr()
# Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input
curses.noecho()
curses.cbreak()
# In keypad mode, escape sequences for special keys
# (like the cursor keys) will be interpreted and
# a special value like curses.KEY_LEFT will be returned
stdscr.keypad(1)
# Start color, too. Harmless if the terminal doesn't have
# color; user can test with has_color() later on. The try/catch
# works around a minor bit of over-conscientiousness in the curses
# module -- the error return from C start_color() is ignorable.
try:
curses.start_color()
except:
pass
return func(stdscr, *args, **kwds)
finally:
# Set everything back to normal
if 'stdscr' in locals():
stdscr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
def screen_curses_exit():
global STDSCR
STDSCR.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
def start_gui(process):
"""
A function that takes care of starting the GUI and stops the Scrapy crawler process when exited from program.
:param CrawlerProcess process: The scrapy crawler process that is used to scrape the web. The instance is used for stopping the process.
"""
def create_ui(screen):
"""
A function passes to curses wrapper for safe execution of terminal GUI.
:param screen: The screen parameter to run the GUI. Sent from the curses wrapper.
"""
GUI.screen = screen # All the statis variables of the GUI class is initialized
GUI.strings = [] # the list of songs is empty initially
GUI.init_display() # init the variables required for GUI
GUI.update_on_key() # Starts a loop that waits for key input and acts accordingly
curses.nocbreak()
curses.echo()
curses.endwin()
GUI.gui_stopped = True
curses.wrapper(create_ui)
process.stop() # Stopping the scrapy crawler process
def shutdown(self):
curses.echo()
curses.nocbreak()
self.screen.keypad(False)
curses.endwin()
return None