Hello Processing user,
in my program I want that the moveable character be stopped by a wall(a rect) if it collide with it.
int speed = 5;
int xPosChell = width/2; //Chell(movable Character)
int yPosChell = height/2;
/*int wall1X = 140;
int wall1Y = 120;*/
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
void setup() {
size(750, 500);
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
void draw() {
background(255);
/*______________________________________________*/
ellipseMode(CENTER);
ellipse(xPosChell, yPosChell, 25, 25);
/*_____________________________________________*/
//rect(wall1X, wall1Y, 140, 130); <------- should be the wall
/*_____________________________________________*/
if (keyPressed) {
if (key == 'w' || keyCode == UP) {
yPosChell = yPosChell - speed;
}
}
if (keyPressed) {
if (key == 's' || keyCode == DOWN) {
yPosChell = yPosChell + speed;
}
}
if (keyPressed) {
if (key == 'a' || keyCode == LEFT) {
xPosChell = xPosChell - speed;
}
}
if (keyPressed) {
if (key == 'd' || keyCode == RIGHT) {
xPosChell = xPosChell + speed;
}
}
/*_____________________________________________*/
//collide with Edges
if(xPosChell >= width - 10) {
xPosChell = xPosChell - speed;
}
if(xPosChell <= 10) {
xPosChell = xPosChell + speed;
}
if(yPosChell >= height - 10) {
yPosChell = yPosChell - speed;
}
if(yPosChell <= 10) {
yPosChell = yPosChell + speed;
}
}