I want to make a square player game but when trying to make the square move with functions it keeps on doing only one movement. i want it so that I can pass a series of statements to make the square move I also want to add obstacles later.
oben = up
unten = down
rechts = right
links = left
this is my code:
long lastTime = 0;
int[] xpos = new int[1];
int[] ypos = new int[1];
int playerCount = 0;
long[] posCase = new long[50];
int[] playerNo = new int[50];
int delay = 500;
boolean f = true;
int count = 0;
int border = 20; // border on one side is 20 both is 40
int sqsize = 96;
void setup() {
size(1000, 1000);
xpos[playerCount] = border;
ypos[playerCount] = border;
frameRate(5);
}
void draw() {
background(#767C7C);
fill(255, 255, 255);
// for start
for (int w = 0; w < 10; w++) {
int newBorderWidth = border + w*sqsize;
for (int i = 0; i < 10; i++) {
int newBorderLength = border + i*sqsize;
fill(#F6F9EF);
stroke(#BABAB6);
strokeWeight(0.5);
rect(newBorderLength, newBorderWidth, sqsize, sqsize);
}
}
// for end
switch(int(posCase[playerCount])) {
case 1 :
if (xpos[playerCount] < border + 9*sqsize)
xpos[playerCount] = xpos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 1");
break;
case 2 :
if (xpos[playerCount] > border)
xpos[playerCount] = xpos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 2");
break;
case 3 :
if (ypos[playerCount] > border)
ypos[playerCount] = ypos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 4");
break;
case 4 :
if (ypos[playerCount] < border + 9*sqsize)
ypos[playerCount] = ypos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 3");
break;
}
fill(255);
rect(xpos[0], ypos[0], sqsize, sqsize);
}
void rechts() {
posCase[playerCount] = 1;
delay(delay);
}
void links() {
posCase[playerCount] = 2;
delay(delay);
}
void oben() {
posCase[playerCount] = 3;
delay(delay);
}
void unten() {
posCase[playerCount] = 4;
delay(delay);
}
I have made a few changes that will allow you to move the square using the WASD keys. The whole code can be simplified a lot by creating a two dimensional array to hold the state of the board.
I'll stick with the original implementation though and only add the pieces that are needed to get you going. The important bits are to add the keyPressed() method (see documentation) and replace line
rect(xpos[0], ypos[0], sqsize, sqsize);
with
rect(xpos[playerCount], ypos[playerCount], sqsize, sqsize);
Here is the above code including the needed changes:
long lastTime = 0;
int[] xpos = new int[1];
int[] ypos = new int[1];
int playerCount = 0;
long[] posCase = new long[50];
int[] playerNo = new int[50];
int delay = 500;
boolean f = true;
int count = 0;
int border = 20; // border on one side is 20 both is 40
int sqsize = 96;
void setup() {
size(1000, 1000);
xpos[playerCount] = border;
ypos[playerCount] = border;
frameRate(5);
}
void draw() {
background(#767C7C);
fill(255, 255, 255);
// for start
for (int w = 0; w < 10; w++) {
int newBorderWidth = border + w*sqsize;
for (int i = 0; i < 10; i++) {
int newBorderLength = border + i*sqsize;
fill(#F6F9EF);
stroke(#BABAB6);
strokeWeight(0.5);
rect(newBorderLength, newBorderWidth, sqsize, sqsize);
}
}
// for end
switch(int(posCase[playerCount])) {
case 1 :
if (xpos[playerCount] < border + 9*sqsize)
xpos[playerCount] = xpos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 1");
break;
case 2 :
if (xpos[playerCount] > border)
xpos[playerCount] = xpos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 2");
break;
case 3 :
if (ypos[playerCount] > border)
ypos[playerCount] = ypos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 4");
break;
case 4 :
if (ypos[playerCount] < border + 9*sqsize)
ypos[playerCount] = ypos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 3");
break;
}
fill(255);
rect(xpos[playerCount], ypos[playerCount], sqsize, sqsize);
}
void rechts() {
posCase[playerCount] = 1;
delay(delay);
}
void links() {
posCase[playerCount] = 2;
delay(delay);
}
void oben() {
posCase[playerCount] = 3;
delay(delay);
}
void unten() {
posCase[playerCount] = 4;
delay(delay);
}
void keyPressed() {
switch(key) {
case 'a':
case 'A':
links();
break;
case 'd':
case 'D':
rechts();
break;
case 'w':
case 'W':
oben();
break;
case 's':
case 'S':
unten();
break;
}
}