please format code with </> button * homework policy * asking questions
float xpos;
float ypos;
float xposN;
float yposN;
float playersize;
PVector position;
float direction;
PVector velocity;
float jumpSpeed;
float walkSpeed;
void setup(){
size(640, 360);
//fullScreen();
xpos = 50;
ypos = 280;
playersize=20;
}
void draw(){
background(0); // Clear the screen with a black background
playerDraw(); //function for the player: a cube
xposN=xpos;//!!!make sure the player can move aside the wall!!!
yposN=ypos;
playerControl(); // up, down, left, right (keypressed)
playerRange();//move but stop at the floor or outside the canvas
floorDraw();//function for the ground and floors
//How do you create a wall? - #3 by colin
}
void playerDraw() {
stroke(255, 153, 0);//orange color stroke of the cube
fill(255, 153, 0); //orange color of the cube
rect(xpos, ypos, 20, 20);//position and size of the cube
}
void floorDraw() {
stroke(0, 153, 255); // blue color of the ground line
line(0, 310, 640, 310); // Creat a line as ground
//line(200, 250, 250, 250); // Creat a line as a floor
}
void playerControl()
{ if(keyPressed){
if (keyCode == LEFT || key == ‘a’) {xposN = xpos-2;}
if (keyCode == RIGHT || key == ‘d’) {xposN = xpos+2;}
if (keyCode == UP || key == ‘w’) {yposN = ypos-2;}
if (keyCode == DOWN || key == ‘s’) {yposN = ypos+2;}
}}
void playerRange() {
if(!floor(50,50,10,50) && !floor(100,50,10,50)){
xpos=constrain(xposN,0,width-playersize); //player size
ypos=constrain(yposN,0,height-playersize-50); //50 is the ground height
}}
boolean floor(float x1, float y1, float w1, float h1){
stroke(0, 153, 255);
fill(0, 153, 255);
rect(x1, y1, w1, h1);
return (xposN+playersize>x1 && xposN<x1+w1 && yposN+playersize>y1 && yposN<y1+h1);
}
//the reason to use boolean: The operator ! is undefined for the argument type(s) void
//the reason to use return: the method must return a result of type boolean
/boolean floor2(float x2, float y2, float w2, float h2){
stroke(0, 153, 255);
fill(0, 153, 255);
rect(x2, y2, w2, h2);
return (xposN+playersize>x2 && xposN<x2+w2 && yposN+playersize>y2 && yposN<y2+h2);
}/
The rect on the right will just disappear if the cube hit the left one. I hope I could stop it from disappearing even the cube hit the left one, i.e, I have 2 barriers/walls.
I am a new learner and I got the current code from another forum so I am not sure the reason behind it also even I work on it for many hours.
Appreciate your kind help!
Regard,
Cherie