Skip to content

Instantly share code, notes, and snippets.

@waynew
Created December 23, 2015 20:25
Show Gist options
  • Save waynew/5446da9ce372fa127d85 to your computer and use it in GitHub Desktop.
Save waynew/5446da9ce372fa127d85 to your computer and use it in GitHub Desktop.
import sys
import curses
import time
import logging
log = logging.getLogger(__name__)
log.addHandler(logging.FileHandler('test.log'))
log.setLevel(logging.DEBUG)
def menu(window, dims):
window.nodelay(0) # not sure what this does, lol
window.clear() # clear window
selection = -1 # the menu code is from the tutorial
option = 0 # so most of this is foreign to me
while selection < 0: # loop
graphics = [0]*2 # point to list
graphics[option] = curses.A_REVERSE # set graphic, reverse BKGND color
window.border() # border
window.addstr(1, dims[1]/2-2, "Menu")
window.addstr(3, dims[1]/2-2, "Play", graphics[0])
window.addstr(4, dims[1]/2-2, "Exit", graphics[1])
window.refresh() # refresh the window
action = window.getch() # get input
log.debug('Key: %s', action)
if action == curses.KEY_UP: # if key is up
option = (option - 1) # go up?
elif action == curses.KEY_DOWN: # key is down
option = (option + 1) # go down?
elif action == ord('\n'): # if RETURN
selection = option # make selection
window.clear() # clear all
if selection == 0: # if is 0, do nothing right now.
return # it's not changing selection
elif selection == 1: # so waiting for that to add here
return
def main(): # main function // ran first
window = curses.initscr() # making new window
dims = window.getmaxyx() # getting window dimensions
if not curses.has_colors(): # color test
curses.endwin() # color test
print "no colors" # color test
sys.exit() # color test
else: # color test
curses.start_color() # color test
curses.noecho() # don't echo the keys on the screen
curses.cbreak() # don't wait enter for input
curses.curs_set(0) # don't show cursor
main_loop(window, dims) # go into main loop
def main_loop(window, dims):
while menu(window, dims):
pass
window.clear()
window.addstr(dims[0]/2, (dims[1]-4)/2, "Exiting...")
window.refresh()
time.sleep(1)
curses.noecho()
curses.cbreak()
curses.curs_set(1)
curses.endwin()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment