I have a game currently which has a player and some obstacles, I want to navigate this player with some commands, but the problem is that the command get executed but the rectangle does not get updated so I have to do some weird while logic with break and a global variable, because otherwise the player would jump. Is there a way to make the functions only count once and maybe loop with for and while normally? this is my code so far:
int border = 20;
int sqsize = 96;
int i = 0;
ArrayList<Player> Players = new ArrayList<Player>();
ArrayList<Obstacle> Obstacles = new ArrayList<Obstacle>();
void setup() {
size(1000, 1000);
players.add(new player(9, 0));
obstacles.add(new obstacle(4, 0));
obstacles.add(new obstacle(4, 1));
obstacles.add(new obstacle(4, 2));
obstacles.add(new obstacle(4, 3));
obstacles.add(new obstacle(4, 4));
obstacles.add(new obstacle(4, 5));
}
void draw() {
background(#767C7C);
for (int l = 0; l < 10; l++) {
for (int w = 0; w < 10; w++) {
fill(#F6F9EF);
stroke(#BABAB6);
strokeWeight(0.5);
rect(border + l*sqsize, border + w*sqsize, sqsize, sqsize);
}
}
Players.get(0).draw();
for ( int i = Obstacles.size()-1; i>= 0; i--) {
Obstacles.get(i).draw();
}
for ( int i = Obstacles.size()-1; i>= 0; i--) {
Obstacles.get(i).ask();
}
while (i < 5) {
Players.get(0).left();
Players.get(0).down();
break;
}
i++;
}
class Player {
int x, y;
player(int ix, int iy) {
x = border + ix*sqsize;
y = border + iy*sqsize;
}
void draw() {
fill(255, 0, 0);
rect(x, y, sqsize, sqsize);
}
void right() {
if (x < 1000-2*sqsize) {
x = x + sqsize;
}
}
void left() {
if (x > 20) {
x = x - sqsize;
}
}
void up() {
if (y > 20+sqsize) {
y = y + sqsize;
}
}
void down() {
if (y < 1000-2*sqsize) {
y = y + sqsize;
}
}
void destroy() {
textSize(64);
text("Game Over", 500-2*sqsize, 500-sqsize/2);
noLoop();
}
int[] request() {
int[] pos = {x, y};
return pos;
}
}
class obstacle {
int x, y;
obstacle(int ix, int iy) {
x = border + ix*sqsize;
y = border + iy*sqsize;
}
void draw() {
fill(255, 0, 0);
rect(x, y, sqsize, sqsize);
}
void right() {
if (x < 1000-2*sqsize) {
x = x + sqsize;
}
}
void left() {
if (x > 20) {
x = x - sqsize;
}
}
void up() {
if (y > 20+sqsize) {
y = y + sqsize;
}
}
void down() {
if (y < 1000-2*sqsize) {
y = y + sqsize;
}
}
void ask() {
for ( int i = Players.size()-1; i>= 0; i--) {
int[] check = Players.get(i).request();
if (/*dist(check[0]+sqsize, check[1]+sqsize, x, y) == 0 || dist(check[0]-sqsize, check[1]-sqsize, x, y) == 0 ||*/ dist(check[0], check[1], x, y) == 0) {
players.get(i).destroy();
}
}
}
}
Is there a way also to detect which side of the player has touched the obstacle.
thank you very much (Iām a newbie).