A Color-Gradient-Sliding-Puzzle in Processing

Nikolaus Gradwohl2010-06-22T05:36:00+02:00

I made a sliding puzzle game in processing. What makes it a bit different from all the others is, that there are no numbers on the pices but you have it to sort the fields by color-gradient.

click here to give it a try.

this is my Day 21 Project for 30datsofcreativity

sliding_puzzle

Tweet This! submit to reddit Digg!   Tags: | 1 comments | no trackbacks

Trackbacks

Comments

Leave a response

  1. alphachap 2011-01-17T13:33:47+01:00

    I made it easier for me. Changed the size and colors. Just change psiz=.. on 2nd line. Here is the barely modified code:

    int[][] field; int psiz=2; int psiz2=psiz*psiz; int state = 0; PFont font;

    void setup() { size(300,300); field = new int[psiz][psiz]; shuffle(); font = loadFont("LucidaBright-48.vlw"); }

    void draw() { background(0); if (state == 0) {

    for( int i = 0; i < psiz; i++ ) { 
      for( int j=0; j < psiz; j++) { 
        if (field[i][j] >= 0) { 
          fill( field[i][j]*256/psiz2, 196 - field[i][j]*128/psiz2, 72+field[i][j]*160/psiz2 ); 
          rect( width/psiz * i, height/psiz * j, width/psiz, height/psiz);
        }
      }
    }
    

    } else {

    fill(0,255,0); 
    textFont( font, 48 ); 
    text("You Won!", 70,150); 
    textFont( font, 16 ); 
    text("(Press any key to restart the game)", 50, 190);
    

    } }//draw()

    void mousePressed() { if ( state == 0 ) {

    int i = round( mouseX / (width/psiz)); 
    int j = round( mouseY / (height/psiz)); 
    move(i,j); 
    if (check()) { 
      state = 1;
    }
    

    } }//mousePressed()

    void keyPressed() { if (state == 1 ) {

    shuffle(); 
    state = 0;
    

    } }//keyPressed()

    void move(int i, int j ) { if ( field[i][j] != -1 ) {

    for( int a = 0; a < psiz; a++ ) { 
      if ( field[a][j] == -1 ) { 
        if ( a < i ) { 
          for ( int t = a; t < i; t++) { 
            field[t][j] = field[t+1][j];
          }
        } 
        else { 
          for ( int t = a; t > i; t--) { 
            field[t][j] = field[t-1][j];
          }
        } 
        field[i][j] = -1; 
        return;
      }
    } 
    for( int a = 0; a < psiz; a++ ) { 
      if ( field[i][a] == -1 ) { 
        if ( a < j ) { 
          for ( int t = a; t < j; t++) { 
            field[i][t] = field[i][t+1];
          }
        } 
        else { 
          for ( int t = a; t > j; t--) { 
            field[i][t] = field[i][t-1];
          }
        } 
        field[i][j] = -1; 
        return;
      }
    }
    

    } }//move()

    boolean check() { int c =0; for( int j = 0; j < psiz; j++ ) {

    for( int i=0; i < psiz; i++) { 
      println( "" + c + " " + field[i][j] ); 
      if (c != field[i][j]) return false; 
      c++; 
      if (c == 15) c = -1;
    }
    

    } return true; }//check()

    void shuffle() { int c = 0; for( int j = 0; j < psiz; j++ ) {

    for( int i=0; i < psiz; i++) { 
      field[i][j] = c; 
      c++;
    }
    

    } field[psiz-1][psiz-1] = -1; int x = psiz-1; int y = psiz-1; for(int i =0; i < 16; i++) {

    x = int(random(psiz)); 
    move( x, y ); 
    y = int(random(psiz)); 
    move( x, y );
    

    } }//shuffle()

Leave a comment