dist not working properly?

edited January 2016 in Questions about Code

Hello, I'm creating a simple game that involves timing when a box lands in the centre of the screen. I'm using the dist to find out how far away the moving rectangle is from the centre as "next level" condition. If the box is within the distance, the level is increased, otherwise you lose.

The box will move either along the X axis, or the Y axis. This being said, it has been brought to my attention that the dist function doesn't seem to work when the box is moving along the y axis and I have no idea why. No matter how precise you are with the timing, it won't seem to go to the next level like it does while on the X axis, however the code follows the same rules.

My description doesn't feel very informative, so I urge you to take a look at the code yourselves. Remove the lines to do with Fipps as you'd most likely not have it on your computer.

Many thanks in advance for anyone's help and advice.

EDIT: I also have no idea why the code looks so funky. I'm sure there's a way to fix that?

Here is the code.

    int x= 130;
    int y = 350;
    int move_x=55;
    int move_y=55;
    int count;
    boolean gamestate = false;
    int lvl; 
    PFont Fipps;
    int rand = int(random(0, 2));

    void keyPressed() {
      if (key== ' ') { // when SPACEBAR is pressed
        if (gamestate==true) {

          gamestate=true;
          if ( dist(x, y, width/2, height/2) <=27) { // if the distance of x/y is 
            //at least 27px from center of the page.
            background(0);
            lvl++;//increase the level
          } else {
            gamestate=false;// otherwise you lose and you start again
            background(255);
            lvl=1;
          }
        }
      }
    }


void mouseClicked() {
  if (gamestate==false) {//if you've already lost
    gamestate=true;// staet again, resetting all the values.
    rand = int(random(0, 2));
    x= 130;
    y = 350;
    move_x=55;
    move_y=55;
    lvl =1;
  }
}


void setup() {
  background(0);
  size(700, 700);
  smooth(100);
  Fipps=loadFont("Fipps.vlw"); //  loads 8 bit font, declares it as fipps//custom font
  lvl=1;// starts level at 1.
  frameRate=60;
}



void draw() {
  if (gamestate==false) {//starting screen
    background(0);
    textFont(Fipps, 20);
    textAlign(CENTER, CENTER);
    text("Click to START", width/2, height/2);
  } else 
  if (gamestate==true) {
    background(0);
    rectMode(CENTER);

    noFill();
    strokeWeight(5);
    stroke(255, 240, 0);
    rect(width/2, height/2, 505, 55);//large background rectangles on x axis
    rect(width/2, height/2, 55, 505);// on y axis


    fill(255, 225, 0, 110);
    noStroke();
    rect(width/2, height/2, 500, 50);//transparent rectangles for effect
    rect(width/2, height/2, 50, 500);

    stroke(0);
    strokeWeight(5);
    for (int l=103; l<325; l+=54) {//creates lines going from right to left to the center point

      line(l, height/2-23, l, height/2+22);
      line(l+278, height/2-23, l+278, height/2+22);
      line(width/2-23, l, width/2+22, l);
      line(width/2-23, l+278, width/2+22, l+278);
    }
    rectMode(CENTER);
    noStroke();
    fill(255, 255, 0);
    if (rand==0) {// if rand is 0 then it creates a rectangle on the x axis else y
      rect(x, height/2, 45, 45);
    } else {
      rect(width/2, y, 45, 45);
    } 
    count=frameCount;
    print(rand);


    switch (rand) {

    case 0:// if 0, then x box moves along x axis.

      if (lvl==1) {
        if (count%13==0) { 
          x=x+move_x;


          if (x>570) {
            x=515;
            move_x=-move_x;
          } 

          if (x<130) { 
            x=185;
            move_x=-move_x;
          }
        }
      }



      if (lvl==2) {
        if (count%10==0) { 
          x=x+move_x;

          if (x>570) {
            x=515;
            move_x=-move_x;
          }

          if (x<130) {
            x=185; 
            move_x=-move_x;
          }
        }
      } 

      if (lvl==3) {
        if (count%7==0) { 
          x=x+move_x;
          if (x>570) {
            x=515;
            move_x=-move_x;
          }

          if (x<130) {
            x=185;
            move_x=-move_x;
          }
        }
      }
      break;
    case 1:// if 1 then box moves along y axis


      if (lvl==1) {
        if (count%13==0) { 
          y=y+move_y;


          if (y>570) {
            y=515;
            move_y=-move_y;
          } 

          if (y<130) { 
            y=185;
            move_y=-move_y;
          }
        }
      }



      if (lvl==2) {
        if (count%10==0) { 
          y=y+move_y;

          if (y>570) {
            y=515;
            move_y=-move_y;
          }

          if (y<130) {
            y=185; 
            move_y=-move_y;
          }
        }
      } 

      if (lvl==3) {
        if (count%7==0) { 
          y=y+move_y;
          if (y>570) {
            y=515;
            move_y=-move_y;
          }

          if (y<130) {
            y=185;
            move_y=-move_y;
          }
        }
      }
      break;
    }
  }
}

Answers

  • Answer ✓

    I also have no idea why the code looks so funky.

    https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text

  • Thanks for that.

    Any idea about the actual code though? I think I forgot to mention I'm using processing.

  • but your problem is that you're only changing y, x remains as 130. which means that the dist() won't work

    try changing

    rect(width/2, y, 45, 45);
    

    to

    rect(x, y, 45, 45);
    

    and you'll hopefully see.

    (it's not a problem in the other direction because you're defaulting y to height/2 anyway)

    dist() is the wrong thing for this imo.

  • edited January 2016

    Thank you for the reply. However that didn't change much. X is at 130, so it moves up and down along the y at x=130, not the centre.

    I did rect(x+220, y, 45, 45); to try to rectify that. It goes back to square 1. Dist() isn't working still. I may try to initialise y differently.

    Could you suggest an alternative rather than using dist() for this?

    EDIT: Nevermind I fixed it. I changed my initialisation to :

    int x = 130;
    int y = 130;
    

    My mouse click function to

    x = 350;
    y = 350;
    

    Then in my if statement, rect(x,y,45,45);like you suggested.

  • Answer ✓

    Dist calculation was working. But x was always 130 so the distance was always > 27.

    Drawing the box at x+220 isn't any better than using width / 2 because you're still only using x in the distance calculation and that's still always 130.

  • Yeah, I realised. Thank you so much. I really appreciate your help!

Sign In or Register to comment.