MicroSand

MicroSand is a very small FallingSandGame.
It has 13 lines and 828 characters of code.

Download

You can download the game and the sourcecode here.

Source Code (10 lines, 737 chars)

#include <SDL/SDL.h>
int main(int,char**){SDL_Surface*s=SDL_SetVideoMode(640,480,8,0);SDL_Event e;
int r,u,x,y,a=-1,b,h=s->h,z=s->pitch,q[]={0,r=91,u=140,200};Uint8*o,*p=(Uint8*)
s->pixels+z;SDL_WM_SetCaption("MicroSand 2 by Max Nagl",0);while(!SDL_Flip(s))
{for(x=0;++x<s->w;p[x-z]=p[(h-2)*z+x]=1);for(o=p+(h-2)*z;--o>p;){if(*o-200)if
((*o>(b=*(o+z)))&&((r*=5)%((*o-b)/5+2)>5)){*(o+z)=*o;*o=b-1?b:0;}else if((b=*o)
>*(o+(y=1))&&((r*=7)%97)>1||b>*(o+(y=a))){*o=*(o+y);*(o+y)=b-1?b:0;}}while(
SDL_PollEvent(&e)){switch(e.type){case 2:u=q[e.key.keysym.sym%4];break;case 12:
return 0;case 4:case 5:SDL_MouseMotionEvent m=e.motion;for(y=m.y-9;y++<m.y+9&&
m.state;)for(x=m.x-9;x++<m.x+9;)if((x>a)*(x<s->w)*(y>a)*(y<h))p[y*z+x]=u;}}}}

Beautified Code

#include <SDL/SDL.h>
int main(int,char**){
	SDL_Surface*s=SDL_SetVideoMode(640,480,8,0);
	SDL_Event e;
	int r,u,x,y,a=-1,b,h=s->h,z=s->pitch,q[]={0,r=91,u=140,200};
	Uint8*o,*p=(Uint8*) s->pixels+z;
	SDL_WM_SetCaption("MicroSand 2 by Max Nagl",0);
	while (!SDL_Flip(s)) {
		for (x=0; ++x<s->w; p[x-z]=p[(h-2)*z+x]=1);
		for (o=p+(h-2)*z; --o>p; ) {
			if (*o-200)if((*o>(b=*(o+z)))&&((r*=5)%((*o-b)/5+2)>5)) {
				*(o+z)=*o;
				*o=b-1?b:0;
			} else if ((b=*o) >*(o+(y=1))&&((r*=7)%97)>1||b>*(o+(y=a))) {
				*o=*(o+y);
				*(o+y)=b-1?b:0;
			}
		}
		while (SDL_PollEvent(&e)) {
			switch (e.type) {
			case 2:
				u=q[e.key.keysym.sym%4];
				break;
			case 12:
				return 0;
			case 4:
			case 5:
				SDL_MouseMotionEvent m=e.motion;
				for (y = m.y-9; y++ < m.y + 9 && m.state; ) {
					for (x = m.x-9; x++ < m.x + 9; ) {
						if((x>a)*(x<s->w)*(y>a)*(y<h)) {
						p[y*z+x]=u;
						}
					}
				}
			}
		}
	}
}


�2008 Max Nagl
Web-design, development and programming by Max Nagl