Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- """
- A paddle, 6 balls bouncing around and 4 boxes
- =====================================================================
- tested with python 2.3 - 2.7
- www.peileppe.com
- """
- import curses
- import time
- w=curses.initscr()
- w.clear()
- yy, xx=w.getmaxyx()
- w.keypad(1)
- w.nodelay(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-2:
- x1=xx-2
- if y<1:
- y=1
- if y1>yy-2:
- 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
- def display_box_filled(bx,ws=w):
- x,y,x1,y1,char=bx
- if x<1:
- x=1
- if x1>xx-2:
- x1=xx-2
- if y<1:
- y=1
- if y1>yy-2:
- y1=yy-2
- for iy in range(y,y1+1):
- for jx in range(x,x1):
- ws.addch(iy,jx,char)
- return
- def bouncing_ball(ball,ws):
- dir_x = ball[2]
- dir_y = ball[3]
- new_x = ball[0]+dir_x
- new_y = ball[1]+dir_y
- sane = 0
- while ws.inch(new_y,new_x) != ord(' ') and sane < 2 :
- if sane % 2 != 0:
- dir_x = -dir_x
- else:
- dir_y = -dir_y
- new_x = ball[0]+dir_x
- new_y = ball[1]+dir_y
- sane += 1
- ws.addch(ball[1],ball[0],' ')
- ws.addch(new_y,new_x,ball[4])
- ball[0]=new_x
- ball[1]=new_y
- ball[2]=dir_x
- ball[3]=dir_y
- display_box_filled(little_box,w)
- display_box(big_box,w)
- b1 = [30,17,35,19,';']
- b2 = [20,17,25,19,':']
- b3 = [10,17,15,19,'?']
- b4 = [40,17,45,19,'+']
- b5 = [50,17,55,19,'=']
- boxes = [b1,b2,b3, b4, b5]
- for bx in boxes:
- display_box(bx,w)
- w.move(0,0)
- action = ''
- dir_x = 1
- dir_y = 1
- ball1 = [4,4, dir_x, dir_y,'@']
- ball2 = [8,2, dir_x, dir_y,'o']
- ball3 = [20,2, dir_x, dir_y,'O']
- ball4 = [21,3, dir_x,dir_y,'X']
- ball5 = [22,6, dir_x,dir_y,'x']
- ball6 = [24,8, dir_x,dir_y,'#']
- balls = [ball1,ball2,ball3,ball4, ball5, ball6]
- draw_char=' '
- exit_loop=False
- w.timeout(100)
- while exit_loop==False:
- action=w.getch()
- for b in balls:
- bouncing_ball(b,w)
- ghost_box = little_box[:]
- ax =little_box[0] ; ay =little_box[1]
- ax1=little_box[2] ; ay1=little_box[3]
- ox = ghost_box[0] ; oy = ghost_box[1]
- ox1= ghost_box[2] ; oy1= ghost_box[3]
- if action == curses.KEY_UP and ay > 2:
- ay -= 1 ; ay1 -= 1
- elif action == curses.KEY_DOWN and ay < yy-3:
- ay += 1 ; ay1 += 1
- elif action == curses.KEY_RIGHT and ax < xx-3:
- ax += 1 ; ax1 += 1
- elif action == curses.KEY_LEFT and ax > 2:
- ax -= 1 ; ax1 -= 1
- elif action == ord('q') or action == 27 :
- exit_loop=True
- ghost_box[0]=ox ; ghost_box[1]=oy
- ghost_box[2]=ox1; ghost_box[3]=oy1
- ghost_box[4]=' '
- display_box_filled(ghost_box,w)
- little_box[0]=ax ; little_box[1]=ay
- little_box[2]=ax1; little_box[3]=ay1
- little_box[4]='*'
- display_box_filled(little_box,w)
- w.move(yy-1,xx-1)
- w.refresh()
- curses.endwin()
Advertisement
Add Comment
Please, Sign In to add comment