Peileppe

curses-paddle-test1

Mar 27th, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None | 0 0
  1. #!/usr/local/bin/python
  2.  
  3. """
  4. A paddle, 6 balls bouncing around and 4 boxes
  5. =====================================================================
  6. tested with python 2.3 - 2.7
  7. www.peileppe.com
  8.  
  9. """
  10.  
  11. import curses
  12. import time
  13.  
  14. w=curses.initscr()
  15. w.clear()
  16. yy, xx=w.getmaxyx()
  17. w.keypad(1)
  18. w.nodelay(1)
  19.  
  20. little_box = [40,7,50,9,'*']
  21. big_box=[1,1,xx-2,yy-2,'#']
  22.  
  23. def display_box(bx,ws=w):
  24.     x,y,x1,y1,char=bx
  25.     if x<1:
  26.      x=1
  27.     if x1>xx-2:
  28.      x1=xx-2
  29.     if y<1:
  30.      y=1
  31.     if y1>yy-2:
  32.      y1=yy-2
  33.     for i in range(x,x1):
  34.         ws.addch(y,i,char)
  35.     for i in range(x,x1):
  36.         ws.addch(y1,i,char)
  37.     for i in range(y,y1+1):
  38.         ws.addch(i,x,char)
  39.         ws.addch(i,x1,char)
  40.     ws.addch(y,x,"+")
  41.     ws.addch(y,x1,"+")
  42.     ws.addch(y1,x,"+")
  43.     ws.addch(y1,x1,"+")
  44.     return
  45.  
  46. def display_box_filled(bx,ws=w):
  47.     x,y,x1,y1,char=bx
  48.     if x<1:
  49.      x=1
  50.     if x1>xx-2:
  51.      x1=xx-2
  52.     if y<1:
  53.      y=1
  54.     if y1>yy-2:
  55.      y1=yy-2
  56.     for iy in range(y,y1+1):
  57.      for jx in range(x,x1):
  58.         ws.addch(iy,jx,char)
  59.     return
  60.  
  61. def bouncing_ball(ball,ws):
  62.     dir_x = ball[2]
  63.     dir_y = ball[3]
  64.     new_x = ball[0]+dir_x
  65.     new_y = ball[1]+dir_y
  66.     sane = 0
  67.     while ws.inch(new_y,new_x) != ord(' ') and sane < 2 :
  68.         if sane % 2 != 0:
  69.             dir_x = -dir_x
  70.         else:
  71.             dir_y = -dir_y
  72.         new_x = ball[0]+dir_x
  73.         new_y = ball[1]+dir_y
  74.         sane += 1
  75.     ws.addch(ball[1],ball[0],' ')
  76.     ws.addch(new_y,new_x,ball[4])
  77.     ball[0]=new_x
  78.     ball[1]=new_y
  79.     ball[2]=dir_x
  80.     ball[3]=dir_y
  81.  
  82.  
  83. display_box_filled(little_box,w)
  84. display_box(big_box,w)
  85.  
  86. b1 = [30,17,35,19,';']
  87. b2 = [20,17,25,19,':']
  88. b3 = [10,17,15,19,'?']
  89. b4 = [40,17,45,19,'+']
  90. b5 = [50,17,55,19,'=']
  91.  
  92. boxes = [b1,b2,b3, b4, b5]
  93.  
  94. for bx in boxes:
  95.  display_box(bx,w)
  96.  
  97. w.move(0,0)
  98.  
  99. action = ''
  100. dir_x = 1
  101. dir_y = 1
  102. ball1 = [4,4, dir_x, dir_y,'@']
  103. ball2 = [8,2, dir_x, dir_y,'o']
  104. ball3 = [20,2, dir_x, dir_y,'O']
  105. ball4 = [21,3, dir_x,dir_y,'X']
  106. ball5 = [22,6, dir_x,dir_y,'x']
  107. ball6 = [24,8, dir_x,dir_y,'#']
  108. balls = [ball1,ball2,ball3,ball4, ball5, ball6]
  109.  
  110. draw_char=' '
  111. exit_loop=False
  112.  
  113. w.timeout(100)
  114. while exit_loop==False:
  115.  action=w.getch()
  116.  for b in balls:
  117.   bouncing_ball(b,w)
  118.  ghost_box = little_box[:]
  119.  ax =little_box[0] ;  ay =little_box[1]
  120.  ax1=little_box[2] ;  ay1=little_box[3]
  121.  ox = ghost_box[0] ;  oy = ghost_box[1]
  122.  ox1= ghost_box[2] ;  oy1= ghost_box[3]
  123.  if action == curses.KEY_UP and ay > 2:
  124.   ay -= 1 ; ay1 -= 1
  125.  elif action == curses.KEY_DOWN and ay < yy-3:
  126.   ay += 1 ; ay1 += 1
  127.  elif action == curses.KEY_RIGHT and ax < xx-3:
  128.   ax += 1 ; ax1 += 1
  129.  elif action == curses.KEY_LEFT and ax > 2:
  130.   ax -= 1 ; ax1 -= 1
  131.  elif action == ord('q') or action == 27 :
  132.   exit_loop=True
  133.  
  134.  ghost_box[0]=ox ; ghost_box[1]=oy
  135.  ghost_box[2]=ox1; ghost_box[3]=oy1
  136.  ghost_box[4]=' '
  137.  display_box_filled(ghost_box,w)
  138.  
  139.  little_box[0]=ax ; little_box[1]=ay
  140.  little_box[2]=ax1; little_box[3]=ay1
  141.  little_box[4]='*'
  142.  display_box_filled(little_box,w)
  143.  w.move(yy-1,xx-1)
  144.  w.refresh()
  145.  
  146. curses.endwin()
Advertisement
Add Comment
Please, Sign In to add comment