import processing.core.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; import javax.sound.midi.*; import javax.sound.midi.spi.*; import javax.sound.sampled.*; import javax.sound.sampled.spi.*; import java.util.regex.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.sax.*; import javax.xml.transform.stream.*; import org.xml.sax.*; import org.xml.sax.ext.*; import org.xml.sax.helpers.*; public class sketch_080212a extends PApplet {int siz=160;//taille de l'\u00e9cran
int nbx=floor(siz/8);
int nby=floor(siz/8);
int nb=nbx*nby;
int t;
int g;
int[] sol= new int[nbx*2+1];
Blocs[] bloc=new Blocs[nb];
PImage[] im;
PImage[] ch2;
PImage ch;
int chx=10;
int chy=10;
int depl;
int depl2;
boolean chpose;
boolean pressed[] = new boolean[128]; 
float animch=0;
int solpos;
int i;

class Blocs {
  int x;
  int y;
  int no;

  Blocs(int x, int y, int no) {
    this.x=x;
    this.y=y;
    this.no=no;
  }


  public void dessine() {
    stroke(0);
    for (int x2=0;x2<8;x2++) {
      for (int y2=0;y2<8;y2++) {
        if(brightness(im[no].pixels[x2+y2*8])==0) {
          point(x+x2,y+y2);
        }
      }
    }
  }
}

public void setup() {
  background(255);
  frameRate(30);
  size(siz,siz);
  im=new PImage[4];
  im[0]=loadImage("00.GIF");
  im[1]=loadImage("01.GIF");
  im[2]=loadImage("02.GIF");
  im[3]=loadImage("03.GIF");
  ch2=new PImage[3];
  ch2[0]=loadImage("ch00.GIF");
  ch2[1]=loadImage("ch01.GIF");
  ch2[2]=loadImage("ch02.GIF");
  generelesol();
  generelesblocs();
}

public void draw() {
  animchf();
  scrolldroite();
  clavier();
  gravite();
  avance();
  fonddeg();
  dessinechar();
  dessinetout();  
}

public void fonddeg() {
  for (i=0;i<siz;i++) {
    stroke(i*255/siz);
    line(0,i,siz,i);
  }
}

public void animchf() {
  animch+=0.1f;
  if (animch>=3) {
    animch=0;
  }
  ch=ch2[floor(animch)];
}

public void gravite() {
  g++;
  chpose=false;
  if (g>0) {
    for(int i=0;i<g;i++) {
      if (verifcolld(chx,chy+1)==false) {
        chy+=1;
      }
      else {
        chpose=true;
        g=0;
      }
    }
  }
  if (g<0) {
    for(int i=0;i>g;i-=1) {
      if (verifcolld(chx,chy-1)==false) {
        chy-=1;
      }
      else {
        chpose=true;
        g=0;
      }
    }
  }
}

public boolean verifcolld(int chx, int chy) {
  boolean verif= false;
  for (int x2=0;x2<8;x2++) {
    for (int y2=0;y2<8;y2++) {
      if(brightness(ch.pixels[x2+y2*8])==0) {
        if(decors(chx+x2,chy+y2)==true){
          verif=true;
        }
      }
    }
  }
  return verif;
}

public boolean decors(int x, int y){
  if (brightness(im[bloc[constrain(floor(x/8)+(floor(y/8)*nbx),0,nb-1)].no].pixels[constrain((x-(floor(x/8)*8))+(y-(floor(y/8)*8))*8,0,63)])!=255) {
    return true;
  }
  else{
    return false;
  }
}

public void scrolldroite() {
  if (chx>siz/2){
    for (i=0;i<nbx;i++) {
      sol[i]=sol[i+1];
    }
    for (i=i;i<nbx*2;i++) {
      if (random(10)<5) {
        solpos+=floor((random(-1,2)));
        solpos=constrain(solpos,3,(siz/8)-3);
      }
      sol[i]=solpos;
    }
    generelesblocs();
    chx-=8;
  }
}

public void keyPressed() {
  pressed[key & 0x7f] = true;
}

public void keyReleased() {
  pressed[key & 0x7f] = false;
}

public void avance() {
  for (int i=0;i<abs(depl);i++) {
    if (depl>0) {
      depl2=1;  
    }
    if (depl<0) {
      depl2=-1;  
    }
    if (verifcolld(chx+depl2,chy)==false) {
      chx+=depl2;
    }
    else if (verifcolld(chx+depl2,chy-1)==false) {
      chx+=depl2;
      chy-=1;
    }
    else {
      depl=0;
    }
  }
}

public void clavier() {
  if (pressed[102]) {  
    depl+=1;
  }
  if (pressed[115]) {  
    depl-=1;
  }
  if (!pressed[102] && !pressed[115]) {  
    if (depl>0) {
      depl-=1;
    }
    if (depl<0) {
      depl+=1;
    }
  }
  if (pressed[100]) {  
    if (chpose==true) {
      g=-10;
    }
  }
}

public void generelesol() {
  solpos=nby-floor(nby/3);
  for (int i=0;i<=nbx*2;i++) {
    if (random(10)<5) {
      solpos+=floor(random(-1,2));
      solpos=constrain(solpos,3,(siz/8)-3);
    }
    sol[i]=solpos;
  }
}

public void generelesblocs() {
  int x=0;
  int y=0;
  for (int i=0;i<nb;i++) {
    Blocs truc=new Blocs(x*8,y*8,typedesol(x,y));
    bloc[i]=truc;
    x++;
    if (x==nbx) {
      x=0;
      y++;
    }
  }
}

public int typedesol(int x, int y) {
  if (y<sol[x]) {
    t=0;
  }
  if (y==sol[x]) {
    t=1;
    if(sol[x+1]>sol[x] && sol[constrain(x-1,0,x)]<sol[x])      {
      t=2;
    }
    if(sol[x+1]<sol[x] && sol[constrain(x-1,0,x)]>sol[x])      {
      t=3;
    }

  }
  if (y>sol[x]) {
    t=1;
  }
  return t;
}

public void dessinechar() {
  stroke(0);
  for (int x2=0;x2<8;x2++) {
    for (int y2=0;y2<8;y2++) {
      if(brightness(ch.pixels[x2+y2*8])==0) {
        point(chx+x2,chy+y2);
      }
    }
  }
}

public void dessinetout() {
  for (int i=0;i<nb;i++) {
    bloc[i].dessine();
  }
}

  static public void main(String args[]) {     PApplet.main(new String[] { "sketch_080212a" });  }}