Sabbia nella clessidra

  • Mouse: attiva finestra grafica
  • Tastiera (LEFT, RIGHT, UP, DOWN); ruota/ribalta la clessidra
  • Applicazione Java
float gravita, vento;
color fondo, spazio, sabbia;
int   num;
color[][] tab;
void setup()
{
   size(500, 500);
   frameRate(30);
   noStroke();
   tab=new color[width][height];
   int w=width, h=height;
   num=w*h/3;
   gravita=h/15;
   vento=w/50;

   fondo=color(255, 255, 255);
   spazio=color(0, 0, 0);
   sabbia=color(255, 255, 100); 

   background(fondo);
   fill(spazio);
   triangle(0, h, w, h, w/2, h/2);

   int d=20;
   quad(w/2-d, h/2-d, w/2+d, h/2-d, w/2+d, h/2+d, w/2-d, h/2+d);

   fill(sabbia);
   triangle(0,0, w, 0, w/2, h/2);
}

void draw()
{
   if(keyPressed)
   {
	 for(int i=0; i < width; i++)
		for(int j=0; j < height; j++)
		  tab[i][j]=get(i, j);

	 if(keyCode==UP || keyCode==DOWN)
	 {
		for (int i=0; i < width; i++)
		  for(int j=0; j < height; j++)
			set(i, j, tab[i][height-j-1]);
	 }
	 else if(keyCode==RIGHT)
	 {
		for(int i=0; i < width; i++)
		  for(int j=0; j < height; j++)
			set(i, j, tab[j][height-i-1]);
	 }
	 else if(keyCode==LEFT)
	 {
		for(int i=0; i < width; i++)
		  for(int j=0; j < height; j++)
			set(i, j, tab[width-j-1][i]);
	 }
	 delay(200);
   }
   else
   {
	   for(int i=0; i < num; i++)
	   {
		 int x =int(random(0, width));
		 int y =int(random(0, height));
		 color c =get(x, y);
		 int y1=y+int(random(0, gravita));
		 int x1=x+int(random(-vento, +vento));
		 color c1=get(x1, y1);

		 if(c==sabbia && c1==spazio)
		 {
			set(x, y, spazio);
			set(x1, y1, sabbia);
		 }
	   }
   }
}