Cannot stop glitching through block

Lines 203-215 seem to be the problem. The square is 30 px in length, and the obstacle is 160. I’m trying to get it so that you cannot move into the square. It works when I just have lines 203-208, but when changing the 8 coordinates it gets off. I think it is because it goes inside of the ‘hitbox’ to move them the other way. Any was to fix?

float squareX;
float squareY;
float score = 0;
int scoreDis;
float circleX;
float circleY;

float obstacleRan;
float obstacleX;
float obstacleY;
float obstacleMoveX;
float obstacleMoveY;

float pulX;
float pulY;
float purX;
float purY;
float pdlX;
float pdlY;
float pdrX;
float pdrY;

boolean play;
boolean gameOver;
boolean scoreCount;

boolean[] keyDwn = new boolean[5];

final int _keyR = 39;
final int _keyL = 37;
final int _keyU = 38;
final int _keyD = 40;
final int _keyW = 87;
final int _keyF = 68;
final int _keyS = 83;
final int _keyA = 65;
final int _keySP = 32;


void setup() {
  size(600,600);
  background(0, 255, 0);
  surface.setTitle("run");
  score=0;
  scoreDis = round(score);
  squareX = width/2;
  squareY = height/2;
  play = true;
  gameOver = false;
  keyDwn[0] = false;
  keyDwn[1] = false;
  keyDwn[2] = false;
  keyDwn[3] = false;
  keyDwn[4] = false;
  circleX = width/2;
  circleY = 10;
  scoreCount = false;
  
  obstacleX = 200;// 81;
  obstacleY = 200;//81;
  obstacleMoveX = 0;//2;
  obstacleMoveY = 0;//random(-3, 3); ........................................
  
  
  
  pulX = squareX-15;
  pulY = squareY-15;
  purX = squareX+15;
  purY = squareY-15;
  pdlX = squareX-15;
  pdlY = squareY+15;
  pdrX = squareX+15;
  pdrY = squareY+15;
  
  
  
}
void draw() {
  if (play == true) {
    background(0, 255, 0);
    rectMode(CENTER);
    stroke(0);
    strokeWeight(1);
    square(squareX, squareY, 30);
    stroke(255);
    strokeWeight(1);
    point(pulX, pulY);
    point(purX, purY);
    point(pdlX, pdlY);
    point(pdrX, pdrY);
    
    
    
    
    
    
    if (scoreCount == true) {
        score = score+1;
    }
    scoreDis = round(score);
    if (keyDwn[0] == true) {
      squareY = squareY-5;
    }
    if (keyDwn[1] == true) {
      squareX = squareX+5;
      scoreCount = true;
    }    if (keyDwn[2] == true) {
      squareY = squareY+5;
    }
    if (keyDwn[3] == true) {
      squareX = squareX-5;
      scoreCount = true;
    }
    
    
    
    if (squareX >= 585) {
      squareX = 585;
    }
    if (squareX <= 15) {
      squareX = 15;
    }
    if (squareY <= 15) {
      squareY = 15;
    }
    if (squareY >= 585) {
      squareY = 585;
    }
    
    strokeWeight(1);
    stroke(0);
    fill(255, 0, 0);
    square(circleX, circleY, 30);
    
    pulX = squareX-15;
    pulY = squareY-15;
    purX = squareX+15;
    purY = squareY-15;
    pdlX = squareX-15;
    pdlY = squareY+15;
    pdrX = squareX+15;
    pdrY = squareY+15;
    
 //   if(circleX > squareX) {
 //     circleX = circleX-4;
 //  }
 //   if(circleX < squareX) {
 //     circleX = circleX+4;
 //   }
 //   if(circleY > squareY) {
 //     circleY = circleY-4;
 //   }
 //   if(circleY < squareY) {
 //     circleY = circleY+4;
 //   }
    
    
    
    
    if (pulX > circleX-15 && pulX < circleX + 15 && pulY > circleY-15 && pulY < circleY + 15) {
      play = false;
      gameOver = true;
    }
    if (purX > circleX-15 && purX < circleX + 15 && purY > circleY-15 && purY < circleY + 15) {
      play = false;
      gameOver = true;
    }
    if (pdlX > circleX-15 && pdlX < circleX + 15 && pdlY > circleY-15 && pdlY < circleY + 15) {
      play = false;
      gameOver = true;
    }
    if (pdrX > circleX-15 && pdrX < circleX + 15 && pdrY > circleY-15 && pdrY < circleY + 15) {
      play = false;
      gameOver = true;
    }
    if (squareX > circleX -15 && squareX < circleX + 15 && squareY > circleY - 15 && squareY < circleY + 15) {
      play = false;
      gameOver = true;
    } 
    
    fill(0, 0, 255);
    rectMode(CENTER);
    square(obstacleX, obstacleY, 160);
    obstacleX = obstacleX + obstacleMoveX;
    if (obstacleX >= 520) {
      obstacleMoveX = -obstacleMoveX;
      obstacleMoveY = random(-3, 3);
      obstacleX = 519;
    }
    if (obstacleX <= 80) {
      obstacleMoveX = -obstacleMoveX;
      obstacleMoveY = random(-3, 3);
      obstacleX = 81;
    }
    obstacleY = obstacleY + obstacleMoveY;
    if (obstacleY <= 80) {
      obstacleMoveY = -obstacleMoveY;
    }
    if (obstacleY >= 520) {
      obstacleMoveY = -obstacleMoveY;
    }
    
    if (squareX + 15 >= obstacleX - 80 && squareY -15 <= obstacleY + 80 && squareY +15 >= obstacleY - 80 && squareX < obstacleX) {
      squareX = obstacleX - 95;
    }
    if (squareX - 15 <= obstacleX + 80 && squareY -15 <= obstacleY + 80 && squareY +15 >= obstacleY - 80 && squareX > obstacleX) {
      squareX = obstacleX + 95;
    }
    if (squareY + 15 >= obstacleY - 80 && squareX -15 <= obstacleX + 80 && squareX +15 >= obstacleX - 80 && squareY < obstacleY) {
      squareX = obstacleX - 95;
    }
    if (squareY - 15 <= obstacleY + 80 && squareX -15 <= obstacleX + 80 && squareX +15 >= obstacleX - 80 && squareY > obstacleY) {
      squareX = obstacleX + 95;
    }
    
    
    
    
    
    
    
    
    fill (255, 0, 0);
    
    
    
  }
  
  
  
  
  
  
  if (gameOver == true) {
    background(0);
    strokeWeight(1);
    rectMode(CENTER);
    square(squareX, squareY, 30);
    textAlign(CENTER);
    textSize(50);
    text("GAME OVER", width/2, height/5);
    text("YOUR SCORE:", width/2, height*2/5);
    text(scoreDis, width/2, height/2);
    text("TRY AGAIN?", width/2, height*4/5);
    
    strokeWeight(3);
    noFill();
    stroke(255, 0, 0);
    rectMode(CORNER);
    rect(150, 420, 300, 100);
    
    if (keyDwn[0] == true) {
      squareY = squareY-5;
    }
    if (keyDwn[1] == true) {
      squareX = squareX+5;
    }    if (keyDwn[2] == true) {
      squareY = squareY+5;
    }
    if (keyDwn[3] == true) {
      squareX = squareX-5;
    }
    if (mousePressed) {
      if (mouseX > 150 && mouseX < 450 && mouseY > 420 && mouseY < 520) {
        play = true;
        gameOver = false;
        circleX = width/2;
        circleY = 0;
        score=0;
        obstacleRan = random(1,4);
      }
    }
    if (keyDwn[4] == true) {
      play = true;
      gameOver = false;
      circleX = width/2;
      circleY = 0;
      score=0;
      obstacleRan = random(1,4);
    }
  }
  
  
  if (squareX >= 615) {
    squareX = -14;
  }
  if (squareX <= -15) {
    squareX = 614;
  }
  if (squareY >= 615) {
    squareY = -14;
  }
  if (squareY <= -15) {
    squareY = 614;
  }
  
}

void keyPressed() {
  if (keyCode == _keyU || keyCode == _keyW) {
    keyDwn[0] = true;
  }
  if (keyCode == _keyR || keyCode == _keyF) {
    keyDwn[1] = true;
  }
  if (keyCode == _keyD || keyCode == _keyS) {
    keyDwn[2] = true;
  }
  if (keyCode == _keyL || keyCode == _keyA) {
    keyDwn[3] = true;
  }
  if (keyCode == _keySP) {
    keyDwn[4] = true;
  }
}
void keyReleased() {
  if (keyCode == _keyU || keyCode == _keyW) {
    keyDwn[0] = false;
  }
  if (keyCode == _keyR || keyCode == _keyF) {
    keyDwn[1] = false;
  }
  if (keyCode == _keyD || keyCode == _keyS) {
    keyDwn[2] = false;
  }
  if (keyCode == _keyL || keyCode == _keyA) {
    keyDwn[3] = false;
  }
  if (keyCode == _keySP) {
    keyDwn[4] = false;
  }
}

maybe see here Collision Detection

1 Like