Peileppe

curses-pen-test

Mar 27th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. #!/usr/local/bin/python
  2.  
  3. """
  4. Program using curses library
  5.  
  6. draw 2 boxes
  7. then a character that can be used as a pen to draw
  8. pressing space bar will toggle the pen to draw or to erase
  9. =====================================================================
  10. work on python 2.3 - 2.7
  11. www.peileppe.com
  12.  
  13. """
  14.  
  15. import curses
  16.  
  17. w=curses.initscr()
  18. w.clear()
  19. yy, xx=w.getmaxyx()
  20. w.keypad(1)
  21.  
  22. little_box = [40,7,50,9,'-']
  23. big_box=[1,1,xx-2,yy-2,'#']
  24.  
  25. def display_box(bx,ws=w):
  26.     x,y,x1,y1,char=bx
  27.     if x<1:
  28.      x=1
  29.     if x1>xx:
  30.      x1=xx-2
  31.     if y<1:
  32.      y=1
  33.     if y1>yy:
  34.      y1=yy-2
  35.     for i in range(x,x1):
  36.         ws.addch(y,i,char)
  37.     for i in range(x,x1):
  38.         ws.addch(y1,i,char)
  39.     for i in range(y,y1+1):
  40.         ws.addch(i,x,char)
  41.         ws.addch(i,x1,char)
  42.     ws.addch(y,x,"+")
  43.     ws.addch(y,x1,"+")
  44.     ws.addch(y1,x,"+")
  45.     ws.addch(y1,x1,"+")
  46.     return
  47.  
  48. display_box(big_box,w)
  49. display_box(little_box,w)
  50. w.move(0,0)
  51.  
  52. action = ''
  53. ax, ox=10, 11
  54. ay, oy =10, 11
  55. draw_char=' '
  56. exit_loop=False
  57.  
  58. while exit_loop==False:
  59.  action=w.getch()
  60.  ox,oy=ax,ay
  61.  if action == curses.KEY_UP and ay > 2:
  62.   ay-=1
  63.  elif action == curses.KEY_DOWN and ay < yy-3:
  64.   ay+=1
  65.  elif action == curses.KEY_RIGHT and ax < xx-3:
  66.   ax+=1
  67.  elif action == curses.KEY_LEFT and ax > 2:
  68.   ax-=1
  69.  elif action == ord('q') or action == 27 :
  70.   exit_loop=True
  71.  elif action == ord(' '):
  72.   if draw_char == ' ':
  73.    draw_char='.'
  74.   else:
  75.    draw_char=' '
  76.  w.addch(oy,ox,draw_char)  
  77.  w.addch(ay,ax,'@')
  78.  w.move(yy-1,xx-1) # set the cursor out of the way
  79.  w.refresh()
  80.  
  81. curses.endwin()
Advertisement
Add Comment
Please, Sign In to add comment