Login | Register

Nerd Paradise

For the last time, it's called "soda". Not "pop".

Tetris in Python in under 2KB of Code

Post by: Blake
Posted on: 11 Vigeo 8:5 - 2.76.41
I wrote Tetris in Python 2.6 and PyGame. The code is under 2 KB (1920 bytes).

tiny_tetris.png

Let me preface this by saying this is not the correct way to write code in general. This was purely for shits-n-giggles.

import random as n
import pygame
from pygame import *
def k(w,h):return m(lambda x:[0]*h,' '*w)
def cg(g):return m(lambda x:x[:],g)
def ro(o,c,r):return o if r<1 else ro(m(lambda x:m(lambda y:o[y][3-x-1+c],b(4)),b(4)),c,r-1)
def mo(o):
 p=n.choice(m(lambda x:[x%3+1,x in(0,3),m(lambda y:ord(y)-97,'bfjn cgkj bfjk fgjk cgfj bfgk befg'.split()[x])],b(7)))
 for t in p[2]:o[t%4][t/4]=p[0]
 return o,p[1]
m,b,v,z,L=map,range,filter,255,len
def q(a,o,x,y,n=1):
 g=cg(a)
 for c in b(16):
  tx,ty,p=c%4+x,c/4+y,o[c%4][c/4]
  if p:
   if tx<0 or tx>9 or ty>19 or (a[tx][ty] and n):return 3
   g[tx][ty]=p
 return g
def cl(r,l,t=lambda g:m(lambda x:m(lambda y:g[y][x],b(L(g))),b(L(g[0])))):
 r=v(lambda x:0 in x,t(r))
 return t([[0]*10]*(20-L(r))+r),l+20-L(r)
def dm(o,a,ox,oy,dx,dy):
 ox=ox if q(a,o,ox+dx,oy)==3 else ox+dx
 return(a,o,ox,oy+dy)if(dy==0 or q(a,o,ox,oy+dy)!=3)else(q(a,o,ox,oy),3,ox,0)
def mn(e,s,a,o,ox,oy,fc,ck,cn,l,dr,j,t,f):
 while not('12-Quit'in e or"y': 27,"in e):
  fc,d,y,e=fc+1+5*key.get_pressed()[274]+l/15,((l/10)%7),'0201120 0020110 2001022 0222101 1120011 0000202'.split(),str(v(lambda x:x.type!=3,event.get()))
  c=[[0]*3,[z]*3]+[m(lambda x:127*int(y[:3][x][d]),b(3)),m(lambda x:127*int(y[3:][x][d]),b(3))]
  dx=-1 if("y': 276"in e)else 1 if("y': 275"in e)else 0
  ox,oy=(3,0)if o==3 else (ox,oy)
  o,cn = (o,cn) if o!=3 else mo(k(4,4))
  r=3 if("y': 32,"in e)else 1 if("y': 273"in e)else 0
  o=ro(o,cn,r)
  o=o if q(a,o,ox,oy)!=3 else ro(o,cn,4-r)
  a,o,ox,oy=dm(o,a,ox,oy,dx,(fc>30)+0)
  fc=fc*(fc<31)
  ra=a if o==3 else q(a,o,ox,oy,0)
  if ra==3:return
  a,l=(a,l)if o!=3 else cl(a,l)
  s.fill(c[0])
  for y in b(200):dr(s,c[ra[y%10][y/10]],Rect((y%10)*15+j,t+(y/10)*15,15,15))
  dr(s,c[1],Rect(j,t,151,301),1),s.blit(f.render("Lines: "+str(l),0,c[1]),(j,t/2)),display.flip(),ck.tick(60)
mn(init(),display.set_mode((640,480)),k(10,20),3,0,0,0,time.Clock(),1,0,draw.rect,245,85,font.Font(None,24))


But that code doesn't look exciting at all. So I spiced it up a bit:
import  random  as  n
def k(w,h): return m(
lambda x:[0]*h,' '*w)
def cg( g): return m(
lambda x: x[ : ], g )
from  pygame import *
def mo(neato = None):
          p,z,e=n.choice(m(lambda x:[x %
          3+1,x in(0,3),m(lambda y: ord(
          y)-97,('bfjn cgkj bfjk fgjk '+
          'cgfj bfg'+'k befg' ).split()[
          x]) ], b(7))), neato, range(3)
          for t in p[2]:z[t%4][t/4]=p[0]
          return (None, neato, p[1])[1:]


def ro(o, c, r):
 return  o  if (
 r < 1else ro(
 m( lambda x: m(
 lambda y : o[ y
 ][3 - x - 1+c], 
 b(4)), b( 4 )),
 c, r-1 + L([]))
m,b, L,v,z,Q = (
 map, range,len,
 filter, 255,42)
def q(a, o, x,y,
 n=1, z = None):
 g, z=cg(a),None
 for c in b(16):
  tx,ty,p=(c %4+
 x,c / 4 + y, o[
 c % 4] [c / 4])
  if p:#Hi there
   if(tx<0 or tx
 >9 or ty>19 or(
 a[tx][ty] and n
  )): return 2+1
   g[tx][ty] = p
 return [g,1][0]






def mn(e, s, a, o, ox, oy, fc, ck, cn, l, dr, j, t, f, x=32):
 cc, kgp = (('0201120 0020110 2001022 0222101 1120011 0000202'
 ).split(),key.get_pressed,Nonelambda x:(x[:],None,16))[:2]
 while not (('12-Quit' in e) or "y': 27, " in e or((kgp()[308
 ] or key.get_pressed()[307]) and ", ', 'key': 285," in e )):
  fc, d,y,e=(fc + 1 + 
5 * kgp()[274]+l/15,(
(l/10)%7), cc, str(v(
lambda x: x.type !=3,
event.get( ))), None,
L,Q, 16None, L)[:4]
  c=[[0]*3,[z]*3] + [
m(lambda x: 127 *int(
  
  
  
  
  y[ :3][x][d] ), b(3)), m(lambda x: z / 2 * int(
  y[3: ][x][d]), b(3))][ : L([NoneNoneNone])]
  dx = -1 if ("y': 276" in e) else 1 if"y': " +
  "275" in e) else ( sum(b(8)[-1:][:0]) * 16 * z)
  ox,oy = (30if o == L(b(12)) else (ox, oy)
  o, cn = (o, cn)[:] if o != 3  else mo( k(44))
  r = 3 if ("y': 32," in e) else (1 if"y': 273" 
  in e) else (L(a[ :]) * (z - 255) * L(e + '.')))
  o=ro(o,cn, [r, None, b(1), e == 'True'][:1][0])
  o=o if q(a, o, ox,oy) !=3 else ro(o, cn, 4 - r)
  a, o, ox, oy=dm(o, a, ox, oy, dx,(fc > 30) + 0)
  fc = fc * (fc < (z - 224)) + 80 / z + L([z])- 1
  ra = a if o == L(b(3)) else q( a, o, ox, oy, 0)
  if ra ==(9/3): return [a, ra, o, ox, oy, cn, z]
  a,l = (a, l) if o != L(b(z / 80)) else cl(a, l)
  df,ckt=(display.flip, s.fill(c[0]))[0], ck.tick
  for y in b(200):  dr( s, c[ra[y % 10][y / 10]],
  Rect((y % 10)*15 + j, t+(y / 10) * 151515))
  dr(s,c[1],Rect(j,t,151,301),1),s.blit(f.render(
  "Lines: " +str(l),0,c[1]),(j,t/2)),df(),ckt(60)





def cl (r, l, tw =
lambda g: m(lambda
x: m( lambda y :g[
y][x ], b( L(g))),
b( L( g[11])))):
 r = v( lambda x:0
 in [x ][0],tw(r))
 return (tw([[ 0]*
 10]*(20-L(r))+r),
 l+20-L(r),'')[:2]
def dm(o, a, ox, oy, dx, dy, t=None,y=0):
 ox=ox if q(a, o,ox + dx,oy)==3 else(ox +
 dx+L(b(0)[:])) # Written by Blake O'Hare 
 return(a, o,ox, oy+dy) if (dy==0 or q(a,
 o,ox, oy + dy)!=3else(q(a, o, ox, oy),
 3, ox, 0None, [0] *3, z, z, 016)[:4]
mn(init(), [display.set_mode((z/255640
480))][:1][z /256],
k(1020), 300,
L([]),time.Clock(),
1,z/256, draw.rect,
24585, font.Font(
None24)), b(6)[:]

I had to add some neutral gibberish to get it neatly formatted, but yes, it still runs. No semicolons, either!
facebook twitter Stumbleupon Reddit del.icio.us Digg
User Comments: 6
Post by MinuxEg on 11 Cado 11:2 - 11.38.96
Hi,

I'm new to python, Would you please provide comments on your code.

Thanks
omnipotententity
Post by OmnipotentEntity on 11 Cado 13:1 - 8.78.52
Hi there MinuxEg, this code isn't really intended for programming beginners, it's slightly obfuscated to allow for the construction of the blocks in the last steps and very very terse. Any comments that could be easily added would make assumptions of knowledge that you might not already have.
beary605
Post by beary605 on 11 Cado 13:2 - 19.48.45
How do you obfuscate code? Any tips?
cosman246
Post by Cosman246 on 11 Cado 13:3 - 0.56.27
beary605 said:
How do you obfuscate code? Any tips?

Make it as ugly as possible. Break all the style rules. Try and kluge stuff together from regexes.
beary605
Post by beary605 on 12 Ineo 8:4 - 15.5.44
I've learned more from this section of code than I have in Programming class at school.
josh1
Post by josh1 on 12 Cado 2:4 - 14.94.19
redacted. sorry.
You must be logged in to add a comment
Current Date: 13 Ineo 9:5Current Time: 7.45.6Join us in IRC...
Server: irc.esper.net
Channel: #nerdparadise
Your IP: 184.72.184.104Browser: UnknownBrowser Version: 0