Bounce conditional stopped working can someone help me????

int w;
int h;
int eye_w, eye_h, pupil_w, pupil_h, ear_w, ear_h, mouth_w, mouth_h, x, y, rot;
int speed=2;
int spacing =90;
int dia = 15;


void setup() {
  size(810, 810);
  w=50;
  h=50;
  eye_w=15;
  eye_h=15;
  pupil_w=7;
  pupil_h=7;
  ear_w =15;
  ear_h =30;
  mouth_w = 20;
  mouth_h = 9;
  x=width/2;
  y=height/18;
  rot=0;
}

void draw() {
  background(0);
  // grid
  for (int hoz = 90; hoz < 730; hoz = hoz + spacing) {
    for (int ver = 90; ver < 730; ver = ver + spacing) {
      fill(245, 216, 163);
      strokeWeight(0);
      ellipse(hoz, ver, dia, dia);
    }
  }

  if (key=='w') {
    y = y - speed;
    rot=180;
  } 
  if (key=='s') {
    y = y + speed;
    rot=0;
  }  
  if (key=='a') {
    x = x - speed;
    rot=90;
  }  
  if (key=='d') {
    x = x + speed ;
    rot=270;
  }


  bounce();
  drawFizzy(x, y, rot);
} 

void bounce() {


  if (x<30) {
    x=x+speed;
    rot=270;
  }
  if (x>785) {
    x=x-speed;
    rot=90;
  }
  if (y<30) {
    y=y+speed;
    rot=0;
  }
  if (y>785) {
    y=y-speed;
    rot=180;
  }
}



void drawFizzy(int x, int y, int rot) {

  //body
  ellipseMode(CENTER);
  translate(x, y);
  rotate(radians(rot));
  noStroke();
  //ears 
  fill(240, 152, 152);
  ellipse( -w/2, h/18, ear_w, ear_h);
  ellipse( w/2, h/18, ear_w, ear_h);
  fill(0);
  ellipse( -w/2, h/18, ear_w/2, ear_h/2);
  ellipse( w/2, h/18, ear_w/2, ear_h/2);
  //body
  fill(255, 0, 0);
  ellipse(0, 0, w, h);
  //mouth
  fill(0);
  ellipse(w/-22, h/2.8, mouth_w, mouth_h);
  fill(240, 152, 152);
  ellipse(w/-22, h/2.8, mouth_w/2, mouth_h/1);
  //eyebrows 
  fill(0);
  ellipse(-w/5, h/18, eye_w -2, eye_h);
  ellipse(w/5, h/18, eye_w -2, eye_h);
  fill(255, 0, 0);
  ellipse(-w/5, h/16, eye_w -2, eye_h);
  ellipse(w/5, h/16, eye_w -2, eye_h);
  //eyes
  fill(255);
  ellipse(-w/5, h/10, eye_w, eye_h);
  ellipse(w/5, h/10, eye_w, eye_h);
  //pupils
  fill(0);
  ellipse(-w/5, h/10, pupil_w, pupil_h);
  ellipse(w/5, h/10, pupil_w, pupil_h);
}
Tagged:

Answers

  • Answer ✓

    Introduce this code in line 54. I have to say it is not proper to use jey like that in draw. That is the reason why I have to reset it to zero. This way is unwanted because in case you want to use another key to do another action, you will lose the power of continuous motion, exactly what you have now.

    Consider checking the reference for keyPressed() and keyReleased() functions.

    Kf

      key=0;  
      x+=speed* cos(radians(rot+90));  
      y+=speed*sin(radians(rot+90)) ;
    
  • thanks man

Sign In or Register to comment.