Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- """
- Program using curses library
- draw 2 boxes
- then a character that can be used as a pen to draw
- pressing space bar will toggle the pen to draw or to erase
- =====================================================================
- work on python 2.3 - 2.7
- www.peileppe.com
- """
- import curses
- w=curses.initscr()
- w.clear()
- yy, xx=w.getmaxyx()
- w.keypad(1)
- little_box = [40,7,50,9,'-']
- big_box=[1,1,xx-2,yy-2,'#']
- def display_box(bx,ws=w):
- x,y,x1,y1,char=bx
- if x<1:
- x=1
- if x1>xx:
- x1=xx-2
- if y<1:
- y=1
- if y1>yy:
- y1=yy-2
- for i in range(x,x1):
- ws.addch(y,i,char)
- for i in range(x,x1):
- ws.addch(y1,i,char)
- for i in range(y,y1+1):
- ws.addch(i,x,char)
- ws.addch(i,x1,char)
- ws.addch(y,x,"+")
- ws.addch(y,x1,"+")
- ws.addch(y1,x,"+")
- ws.addch(y1,x1,"+")
- return
- display_box(big_box,w)
- display_box(little_box,w)
- w.move(0,0)
- action = ''
- ax, ox=10, 11
- ay, oy =10, 11
- draw_char=' '
- exit_loop=False
- while exit_loop==False:
- action=w.getch()
- ox,oy=ax,ay
- if action == curses.KEY_UP and ay > 2:
- ay-=1
- elif action == curses.KEY_DOWN and ay < yy-3:
- ay+=1
- elif action == curses.KEY_RIGHT and ax < xx-3:
- ax+=1
- elif action == curses.KEY_LEFT and ax > 2:
- ax-=1
- elif action == ord('q') or action == 27 :
- exit_loop=True
- elif action == ord(' '):
- if draw_char == ' ':
- draw_char='.'
- else:
- draw_char=' '
- w.addch(oy,ox,draw_char)
- w.addch(ay,ax,'@')
- w.move(yy-1,xx-1) # set the cursor out of the way
- w.refresh()
- curses.endwin()
Advertisement
Add Comment
Please, Sign In to add comment