# Koleksi Kode Sumber (24) : Tetris Sederhana dalam Bahasa Python

Sumber : http://defaultset.blogspot.com/

[sourcecode languange=”python” wraplines=”no”]
import curses
from random import randrange
curses.initscr()
curses.curs_set(0)
win = curses.newwin(18,18,0,0) # Create window and draw border
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]]
def chkFig(crds,s): # collision detection
chk = all([win.inch(c,c) & 255 == 32 for c in crds])
for c in crds: win.addch(c,c,’X’ if s==1 else 32) if ((chk and s == 1) or s == 0) else None
return True if s == 0 else chk
def putFig(FP,s): # decode and put figure on the screen
c = lambda el,n: -1 if (n >> el & 3) == 3 else 1 if (n >> el & 3) == 1 else 0
pos = [ c(i ,f[ FP ][ FP ] ) for i in range(0,15,2)[::-1]]
return chkFig([map(lambda x,y: x+y, FP[0:2]*4,pos)[i-2:i] for i in range(2,9,2)],s)
def MoveFig(FP,key,d): #figure moving function
FP = FP – d if key == curses.KEY_LEFT else FP + d if key == curses.KEY_RIGHT else FP
FP = FP + d if key in [curses.KEY_DOWN, -1] else FP
if key == curses.KEY_UP: FP = 0 if FP + d > 3 else 3 if FP + d < 0 else FP + d
def chkBoard(score): #kill full lines and increase score
for i in range(17):
if all([chr(win.inch(i,x)) == ‘X’ for x in range(1,17)]):
win.deleteln()
win.move(1,1)
win.insertln()
score = score + 1
if score % 10 == 0: win.timeout(300-(score*2))
return score
FigPos = [8,3,0,randrange(0,6,1)] # x,y,rotation,figure
score = putFig(FigPos,1) ^ 1
win.timeout(300)
while 1: # main loop
win.border(‘|’,’|’,’-‘,’-‘,’+’,’+’,’+’,’+’)
key = win.getch()
if key == 27: break
putFig(FigPos,0)
MoveFig(FigPos,key,1)
if not putFig(FigPos,1):
MoveFig(FigPos,key, -1)
putFig(FigPos,1)
if FigPos==3: break
if key in [curses.KEY_DOWN,-1]:
score = chkBoard(score)
FigPos = [8,3,0,randrange(0,6,1)]
putFig(FigPos,1)
curses.endwin() # back to console
print ‘\n Tetris-50l (by K0x21),\n Thanks for playing, your score: ‘+str(score)+’\n’
[/sourcecode]