MouseY limitations

how do you limit the mouseY movement of the car between 400 px and 600? px

Car car1;
o1 o1;
o2 o2;
o3 o3;
o4 o4;
o5 o5;
o6 o6;
o7 o7;
int xx=1000;
int x1;
int x2;
int x3;
int x4;
int x5;
int x6;
int x7;
float y1=400;
float y2=450;
float y3=500;
float y4=550;
float y5=500;
float y6=500;
float y7=500;
int c1=0;
int d=0;
int speed=1;
int p=0;
PImage bg;
PImage b;

void setup() {
  size(1000, 600);
  bg = loadImage("road.jpg");
  car1= new Car();
  o1= new o1();
  o2= new o2();
  o3= new o3();
  o4= new o4();
  o5= new o5();
  o6= new o6();
  o7= new o7();
  smooth();
}

void draw() {
  x1=xx+30;
  println(c1);
  loop();
  noStroke();
  background(bg);
  fill(183, 67, 123);
  car1.cardisplay();
  fill(128);
  o1.o1();
  o2.o2();
  o3.o3();
  o4.o4();
  o5.o5();
  o6.o6();
  o7.o7();
  xx-=speed;
  fill(108);
  textSize(15);
  text("CLICK TO PAUSE", 10, 55);
  fill(255, 169, 88);
  textSize(35);
  text("Score:", 10, 35);
  text(c1*100, 120, 35);
  if (xx<=-25) {
    xx=1000;
    c1+=1;
    y1=(y1*3)+1;
    y2=(y2*3)+1;
    y3=(y3*3)+1;
    y4=(y4*3)+1;
    y5=(y5*3)+1;
    y6=(y6*3)+1;
    y7=(y7*3)+1;
    if (y1>1000) {
      y1=y1%1000;
    }
    if (y2>1000) {
      y2=y2%1000;
    }
    if (y3>1000) {
      y3=y3%1000;
    }
    if (y4>1000) {
      y4=y4%1000;
    }
    if (y5>1000) {
      y5=y5%1000;
    }
    if (y6>1000) {
      y6=y6%1000;
    }
    if (y7>1000) {
      y7=y7%1000;
    }
  }
  if (((y1-15<=mouseY)&&(y1+15>=mouseY))&&(xx-27<=25)) {
    noLoop();
    d=1;
  }
  if (((y2-15<=mouseY)&&(y2+15>=mouseY))&&(xx-27<=25)) {
    noLoop();
    d=1;
  }
  if (((y3-15<=mouseY)&&(y3+15>=mouseY))&&(xx-27<=25)) {
    noLoop();
    d=1;
  }
  if (((y4-15<=mouseY)&&(y4+15>=mouseY))&&(xx-27<=25)) {
    noLoop();
    d=1;
  }
  if (((y5-15<=mouseY)&&(y5+15>=mouseY))&&(xx-27<=25)) {
    noLoop();
    d=1;
  }
  if (((y6-15<=mouseY)&&(y6+15>=mouseY))&&(xx-27<=25)) {
    noLoop();
    d=1;
  }
  if (((y7-15<=mouseY)&&(y7+15>=mouseY))&&(xx-27<=25)) {
    noLoop();
    d=1;
  }
  if (d==1) {
    background(128);
    textSize(42);
    fill(245, 166, 166);
    text("GAME OVER", 400, 240);
    fill(255);
    text("SCORE:", 450, 290);
    text(c1*100, 450, 340);
    fill(166, 214, 245);
    text("CLICK TO RETRY", 350, 390);
  }
  if (c1>=5) {
    speed=(c1/5)+1;
  }
}

class Car {
  void cardisplay() {
    rect(25, mouseY, 40, 35);
    triangle(25, mouseY, 15, mouseY+35, 25, mouseY+35);
    triangle(65, mouseY, 85, mouseY+35, 65, mouseY+35);
    triangle(85, mouseY+35, 65, mouseY+37, 65, mouseY+30);
    triangle(15, mouseY+35, 25, mouseY+37, 25, mouseY+30);
    fill(0);
    ellipse(30, mouseY+40, 15, 15);
  }
}

class o1 {
  void o1() {
    ellipse(xx+50, y1, 50, 40);
  }
}
class o2 {
  void o2() {
    ellipse(xx, y2, 40, 30);
    triangle(xx, y2, xx+25, y2-15, xx+25, y2+15);
    triangle(xx-20, y2, xx+3, y2, xx+3, y2-25);
  }
}
class o3 {
  void o3() {
    ellipse(xx, y3, 40, 30);
    triangle(xx, y3, xx+25, y3-15, xx+25, y3+15);
    triangle(xx-20, y3, xx+3, y3, xx+3, y3-25);
  }
}
class o4 {
  void o4() {
    beginShape();
    vertex(xx-1000, 216);
    vertex(xx-951, 216);
    vertex(xx-951, 247);
    vertex(xx-931, 228);
    vertex(xx-923, 247);
    vertex(xx-923, 150);
    vertex(xx-908, 135);
    vertex(xx-893, 150);
    vertex(xx-893, 179);
    vertex(xx-882, 167);
    vertex(xx-870, 179);
    vertex(xx-870, 195);
    vertex(xx-826, 195);
    vertex(xx-826, 224); 
    vertex(xx-807, 198);
    vertex(xx-788, 224);
    vertex(xx-768, 198);
    vertex(xx-749, 224);
    vertex(xx-749, 150);
    vertex(xx-734, 135);
    vertex(xx-719, 150);
    vertex(xx-719, 217);
    vertex(xx-671, 217);
    vertex(xx-671, 248);
    vertex(xx-657, 228);
    vertex(xx-642, 248);
    vertex(xx-642, 195);
    vertex(xx-599, 195);
    vertex(xx-599, 224);
    vertex(xx-579, 200);
    vertex(xx-560, 224);
    vertex(xx-560, 179);
    vertex(xx-548, 168);
    vertex(xx-536, 179);
    vertex(xx-536, 233);
    vertex(xx-493, 233);
    vertex(xx-493, 195);
    vertex(xx-449, 195);
    vertex(xx-449, 151);
    vertex(xx-435, 136);
    vertex(xx-420, 151);
    vertex(xx-420, 179);
    vertex(xx-409, 167);
    vertex(xx-397, 179);
    vertex(xx-384, 167);
    vertex(xx-373, 179);
    vertex(xx-373, 223);
    vertex(xx-353, 198);
    vertex(xx-328, 233);
    vertex(xx-292, 233);
    vertex(xx-292, 214);
    vertex(xx-249, 214);
    vertex(xx-249, 179);
    vertex(xx-236, 168);
    vertex(xx-224, 179);
    vertex(xx-224, 225);
    vertex(xx-204, 200);
    vertex(xx-186, 225);
    vertex(xx-186, 151);
    vertex(xx-171, 137);
    vertex(xx-156, 151);
    vertex(xx-156, 197);
    vertex(xx-114, 197);
    vertex(xx-114, 181);
    vertex(xx-101, 169);
    vertex(xx-89, 181);
    vertex(xx-89, 215);
    vertex(xx-48, 215);
    vertex(xx-23, 181);
    vertex(xx, 211);
    vertex(xx, 216);
    vertex(xx, 216); //1st
    vertex(xx, 216);
    vertex(xx+49, 216);
    vertex(xx+49, 247);
    vertex(xx+63, 228);
    vertex(xx+77, 247);
    vertex(xx+77, 150);
    vertex(xx+92, 135);
    vertex(xx+107, 150);
    vertex(xx+107, 179);
    vertex(xx+118, 167);
    vertex(xx+130, 179);
    vertex(xx+130, 195);
    vertex(xx+174, 195);
    vertex(xx+174, 224); 
    vertex(xx+193, 198);
    vertex(xx+212, 224);
    vertex(xx+232, 198);
    vertex(xx+251, 224);
    vertex(xx+251, 150);
    vertex(xx+266, 135);
    vertex(xx+281, 150);
    vertex(xx+281, 217);
    vertex(xx+329, 217);
    vertex(xx+329, 248);
    vertex(xx+343, 228);
    vertex(xx+358, 248);
    vertex(xx+358, 195);
    vertex(xx+401, 195);
    vertex(xx+401, 224);
    vertex(xx+421, 200);
    vertex(xx+440, 224);
    vertex(xx+440, 179);
    vertex(xx+452, 168);
    vertex(xx+464, 179);
    vertex(xx+464, 233);
    vertex(xx+507, 233);
    vertex(xx+507, 195);
    vertex(xx+551, 195);
    vertex(xx+551, 151);
    vertex(xx+565, 136);
    vertex(xx+580, 151);
    vertex(xx+580, 179);
    vertex(xx+591, 167);
    vertex(xx+603, 179);
    vertex(xx+616, 167);
    vertex(xx+627, 179);
    vertex(xx+627, 223);
    vertex(xx+647, 198);
    vertex(xx+672, 233);
    vertex(xx+708, 233);
    vertex(xx+708, 214);
    vertex(xx+751, 214);
    vertex(xx+751, 179);
    vertex(xx+764, 168);
    vertex(xx+776, 179);
    vertex(xx+776, 225);
    vertex(xx+796, 200);
    vertex(xx+814, 225);
    vertex(xx+814, 151);
    vertex(xx+829, 137);
    vertex(xx+844, 151);
    vertex(xx+844, 197);
    vertex(xx+886, 197);
    vertex(xx+886, 181);
    vertex(xx+899, 169);
    vertex(xx+911, 181);
    vertex(xx+911, 215);
    vertex(xx+952, 215);
    vertex(xx+977, 181);
    vertex(xx+1000, 211);
    vertex(xx+1000, 216);
    vertex(xx+1000, 216); //1st
    vertex(xx+1000, 315);
    vertex(xx-1000, 315);
    endShape(CLOSE);
  }
}
class o5 {
  void o5() {
    ellipse(xx, y5, 40, 30);
    triangle(xx, y5, xx+25, y5-15, xx+25, y5+15);
    triangle(xx-20, y5, xx+3, y5, xx+3, y5-25);
  }
}
class o6 {
  void o6() {
    ellipse(xx, y6, 40, 30);
    triangle(xx, y6, xx+25, y6-15, xx+25, y6+15);
    triangle(xx-20, y6, xx+3, y6, xx+3, y6-25);
  }
}
class o7 {
  void o7() {
    ellipse(xx, y7, 40, 30);
    triangle(xx, y7, xx+25, y7-15, xx+25, y7+15);
    triangle(xx-20, y7, xx+3, y7, xx+3, y7-25);
  }
}
void mousePressed() {
  if (d==1) {
    redraw();
    c1=0;
    speed=1;
    d=0;
    xx=1000;
  }
  if ((d==0)&&(p==0)) {
    noLoop();
    background(110);
    fill(255);
    textSize(42);
    fill(245, 166, 166);
    text("PAUSED", 450, 280);
    fill(166, 214, 245);
    text("CLICK TO RESUME", 350, 330);
    p=1;
  }
  if ((d==0)&&(p==1)) {
    redraw();
    p=0;
  }
}
Tagged:

Answers

  • Answer ✓

    with map you can beam values of one scale to another:

    virtualMouseY = map ( mouseY, 0,width,400,600 );

    ;-)

  • Or, you can use constrain to limit the minimum and maximum range of the mouse (I guess I'll use what already exists):

    virtualMouseY = constrain(mouseY, 400, 600);
    

    Re-mapping the mouse will also work, but it depends on what you're trying to do. I usually prefer to have things to move with the mouse, rather than slower or faster, depending on the respective size ratios.

    Your choice. :)

  • thanks guys! it's a great help! :D

Sign In or Register to comment.