Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. import curses; from random import randrange; curses.initscr(); curses.curs_set(0); win = curses.newwin(18,18,0,0); win.keypad(1); win.nodelay(1); f = [ [0x315,0x4cd,0x13f,0xc47],[0x31d,0x4cf,0x137,0xc45],[0x374,0x374,0x374,0x374],[0x741,0x51c,0xdc3,0xf34],[0xfc1,0x73c,0x543,0xd14],[0x311,0x4cc,0x133,0xc44],[0xc34,0x341,0x41c,0x1c3]]
  2. def chkFig(crds,s):
  3.     chk = all([win.inch(c[1],c[0]) & 255 == 32 for c in crds])
  4.     for c in crds: win.addch(c[1],c[0],'X' if s==1 else 32) if ((chk and s == 1) or s == 0) else None
  5.     return True if s == 0 else chk
  6. def putFig(FP,s):
  7.     c = lambda el,n: -1 if (n >> el & 3) == 3 else 1 if (n >> el & 3) == 1 else 0; pos = [ c(i ,f[ FP[3] ][ FP[2] ] ) for i in range(0,15,2)[::-1]]
  8.     return chkFig([map(lambda x,y: x+y, FP[0:2]*4,pos)[i-2:i] for i in range(2,9,2)],s)
  9. def MoveFig(FP,key,d): #figure moving function
  10.     FP[0] = FP[0] - d if key == curses.KEY_LEFT else FP[0] + d if key == curses.KEY_RIGHT else FP[0]; FP[1] = FP[1] + d if key in [curses.KEY_DOWN, -1] else FP[1]  
  11.     if key == curses.KEY_UP: FP[2] = 0 if FP[2] + d > 3 else 3 if FP[2] + d < 0 else FP[2] + d
  12. def chkBoard(score): #kill full lines and increase score
  13.     for i in range(17):
  14.         if all([chr(win.inch(i,x)) == 'X' for x in range(1,17)]):
  15.             win.deleteln(); win.move(1,1); win.insertln(); score = score + 1;
  16.             if score % 10 == 0: win.timeout(300-(score*2))
  17.     return score
  18. FigPos = [8,3,0,randrange(0,6,1)]; score = putFig(FigPos,1) ^ 1; win.timeout(300)
  19. while 1: # main loop
  20.     win.border('|','|','-','-','+','+','+','+'); win.addstr(0,2,' Score: '+str(score)+' '); key = win.getch()
  21.     if key == 27: break
  22.     putFig(FigPos,0); MoveFig(FigPos,key,1)
  23.     if not putFig(FigPos,1):
  24.         MoveFig(FigPos,key, -1); putFig(FigPos,1)
  25.         if FigPos[1]==3: break
  26.         if key in [curses.KEY_DOWN,-1]:
  27.             score = chkBoard(score); FigPos = [8,3,0,randrange(0,6,1)]; putFig(FigPos,1)
  28. curses.endwin(); print '\n  Tetris-28l (by Kris Cieslak),\n  Thanks for playing, your score: '+str(score)+'\n'
  29.  
  30. """
  31. TETRIS-28lines by Kris Cieslak (defaultset.blogspot.com)
  32.        
  33. Controls:  Left,Right,Up,Down - move/rotate
  34.           ESC -quit        
  35.  
  36. Linux/python 2.6.5
  37. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement