Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, here i have provided my piece of code which shows as my object hits the wall

ID: 3833087 • Letter: H

Question

Hi, here i have provided my piece of code which shows as my object hits the wall it reverses direction, however i want it to change the direction it is pointing at aswell when it hits the walls and when it is controlled using WASD keys as i change the direction. help would be appreciated. Thank you!

//eye's width and height
int eyeX = 9;
int eyeY = 15;
int x =400;
int y = 0;
int speed=4;

void setup(){
size(800,800);
smooth();
}


void draw(){
background(0);
player();
reverse();


}

void player(){
//body red circle
fill(255,0,0);
ellipse(x,30+y,50,50);

// black eye
fill(0);
ellipse(x,40+y,30,25);

// tail
stroke(255,0,0);
strokeWeight(3);
line(x,y,400,50+y);

//left eye
fill(255,255,255);
strokeWeight(0);
ellipse(x-5,40+y,eyeX,eyeY);

//right eye
ellipse(x+5,40+y,eyeX,eyeY);
}


void reverse(){
//functions
//change y location of kuru
y = y +speed;

//if we reach the edge reverse speed
if((y>height)||(y<0)){
speed = speed*-1;

}
}

void keyPressed(){
    if(key == 'a'){
    
    }
}

Explanation / Answer

I have gone through each part of your sample code. The key arrows are not supporting in your code.

I have written the program by keeping your code in hand. I have used the keypressed event for the keyboard functionality. You can see the void keyPressed Function is working fine and efficient along with the comments. The existing void keyPressed Function provided is completely wrong and syntax error.

Program:-

//eye's width and height
int eyeX = 9;
int eyeY = 15;
int x =400;
int y = 0;
int speed=4;
void setup(){
size(800,800);
smooth();
}

void draw(){
background(0);
player();
reverse();

}
void player(){
//body red circle
fill(255,0,0);
ellipse(x,30+y,50,50);
// black eye
fill(0);
ellipse(x,40+y,30,25);

// tail
stroke(255,0,0);
strokeWeight(3);
line(x,y,400,50+y);
//left eye
fill(255,255,255);
strokeWeight(0);
ellipse(x-5,40+y,eyeX,eyeY);

//right eye
ellipse(x+5,40+y,eyeX,eyeY);
}

void reverse(){
//functions
//change y location of kuru
y = y +speed;
//if we reach the edge reverse speed
if((y>height)||(y<0)){
speed = speed*-1;

}
}
// Call the keyPressed event
void keyPressed() {
  
   // check the conditions using IF-ELSE Statements
  
if (key == CODED) {
  
   // The keycode is for UP Arrow of Keyboard
  
if (keyCode == UP) {
      
fill(255,255,255);
}
  
   // The keycode is for DOWN Arrow of Keyboard
  
   else if (keyCode == DOWN) {
fill(0);
}
  
}
  
// The fill value is being 126 by default
  
else {
fillVal = 126;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote