I'm in the middle of an final project in Programming and I have a tiny problem with my keypressed. My blocks won't always connect when two blocks are side by side.
I've only tried coding the UP key but I guess it doesn't matter. Here's the code:
int[][] bane = new int[4][4];
int score = 0;
int afstand = 20;
int felt = 125;
int randX;
int randY;
int countSpawn = 0;
void setup()
{
size(600, 600);
background(255);
for (int i = 0; i < 4; i++) {
for (int n = 0; n < 4; n++) {
bane[i][n] = 0;
}
}
spawn();
spawn();
}
void spawn()
{
countSpawn = 0;
for (int i = 0; i < 4; i++) {
for (int n = 3; n >= 0; n--) {
if (bane[i][n] == 0) {
countSpawn++;
}
}
}
if (countSpawn <= 0) {
return;
}
int randX = (int)random(4);
int randY = (int)random(4);
if (bane[randX][randY] != 0) {
spawn();
} else {
bane[randX][randY] = 2;
}
}
void draw()
{
fill(187,173,160);
noStroke();
rect(0,0,600,600,15);
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
fill(205,193,179);
rect(145*i+20,145*j+20,125,125,10);
if (bane[i][j] > 0)
{
textAlign(CENTER);
textSize(40);
fill(0);
text(bane[i][j], i*145+82.5, j*145+92.5);
}
}
}
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
for (int i = 0; i < 4; i++) {
for (int n = 3; n > 0; n--) {
if (bane[i][n] != 0) {
if (bane[i][n-1] == 0) {
bane[i][n-1] = bane[i][n];
bane[i][n] = 0;
} else {
if (bane[i][n-1] == bane[i][n]) {
bane[i][n-1] = bane[i][n]*2;
bane[i][n] = 0;
}
break;
}
}
}
}
spawn();
} else if (keyCode == DOWN) {
} else if (keyCode == LEFT) {
} else if (keyCode == RIGHT) {
}
}
}
Aucun commentaire:
Enregistrer un commentaire