Unable to load image to puzzle
in
Programming Questions
•
7 months ago
Hi guys, I practically very new to processing. I took the example of a gradient sliding puzzle from
30datsofcreativity and make it to an image sliding puzzle. The problem is the image is always distorted and loaded in front of the squares instead of the square itself.
Can anyone please take a look at it and help figure out how to fix it?
int[][] field;
PImage desert;
int state = 0;
PFont font;
void setup() {
size(300,300);
desert = loadImage("Desert.jpg");
field = new int[4][4];
shuffle();
}
void draw()
{
background(0);
if (state == 0) {
for( int i = 0; i < 4; i++ ) {
for( int j=0; j < 4; j++) {
if (field[i][j] >= 0) {
image(desert, 300/4, 300/4 );// <----- need help with this string
rect( width/4 * i, height/4 * j, width/4, height/4);
}
}
}
} else
{
fill(0,255,0);
textFont( font, 48 );
textFont( font, 16 );
text("(Restart)", 50, 190);
}
}
void mousePressed() { if ( state == 0 ) {
int i = round( mouseX / (width/4));
int j = round( mouseY / (height/4));
move(i,j);
if (check()) {
state = 1;
}
} }
void keyPressed() { if (state == 1 ) {
shuffle();
state = 0;
} }
void move(int i, int j ) { if ( field[i][j] != -1 ) {
for( int a = 0; a < 4; 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 < 4; 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;
}
}
} }
boolean check()
{
int c =0;
for( int j = 0;
j < 4; j++ )
{
for( int i=0; i < 4; i++) {
println( "" + c + " " + field[i][j] );
if (c != field[i][j]) return false; c++;
if (c == 15) c = -1;
}
}
return true; }
void shuffle() { int c = 0; for( int j = 0; j < 4; j++ ) {
for( int i=0; i < 4; i++) {
field[i][j] = c;
c++;
}
} field[4-1][4-1] = -1; int x = 4-1; int y = 4-1; for(int i =0; i < 16; i++) {
x = int(random(4));
move( x, y );
y = int(random(4));
move( x, y );
} }
//credit goes to
30datsofcreativity
1