Hitbox and death counter

Hey there, i’m new to processing and programming in general, so go easy on me :smiley:

Well, i was writing this simple game for processing 3, and found myself into some difficulties.
I Made 2 tabs, one for the main coding, and other for classes (maybe you could give some tips for organization as well :slight_smile: )

so here’s my code

Main code tab:

 ball myBall;
 int score;
 int deaths;


void setup() {
  size ( 640, 480);
  noStroke();
  myBall = new ball();
}


void draw() {
  background(126);
  myBall.display();
  fill (255);
  ellipse(mouseX, mouseY, 33, 33);
  fill (0);
  text ("Score: " + score, 10, 460);
  text ("Deaths: " + deaths, 570, 460); 
  
  if (mouseX == myBall.xposb + 33 || mouseY == myBall.yposb + 33 || mouseX == myBall.xposb - 33 || mouseY == myBall.yposb - 33){
    die();
  }
}

void die(){
  deaths++;
  score = 0;
}

classes tab:

class ball {
  color c;
  float xposb;
  float yposb;
  float xspeed;

    ball(){
    c = color (0);
    xposb = width/2;
    yposb = height/2;
    xspeed = 0;
  }
  
  void display(){
   rectMode(CENTER);
   fill(c);
   rect(xposb, yposb, 33, 33);
  }
}

so, is there any easiest or clearer way to reference the rect hitbox instead of - ?

if (mouseX == myBall.xposb + 33 || mouseY == myBall.yposb + 33 || mouseX == myBall.xposb - 33 || mouseY == myBall.yposb - 33){
    die();
  }

and when i put the “if” outside the “draw()” function it says “expecting EOF, found if” but when i put it inside “draw()” function, the “die()” function runs every frame, which is very bad cause i don’t want it to happen, since it have to “die” once every time it hit the hitbox

Make this a boolean function inside the ball class

So you don’t need the ball. part

Also, I think you want && instead of ||

and >= and <= instead of ==

:o Thanks for the help on the hitbox! but still, i didn’t quite get the boolean function inside the class thing - it’s still making a death per frame xD

Ah, it’s not a boolean function, when you call die() directly.

The function must be inside the ball class, so you don’t have to write myBall. everywhere:

from what you’re writing I make the assumption

  • that the ball has a diameter of 66 and
  • that you use ellipseMode(CENTER);

That’s why I say -33 and +33

Chrisir



void checkHit() {
  if (mouseX >= xposb-33 &&  
    mouseY >= yposb-33 &&
    mouseX <= xposb+33 &&
    mouseY <= yposb+33 ) {
    die();
  }
}

i couldn’t assume better :smiley:

right, so how do i not call the "die()" function directly, and call it as a boolean function instead (if it is even possible)… in the meantime of your answer i’ll be trying to crack my head here, it’s not fun to have someone writing your code for you hahah :slight_smile:

thanks anyways!

OKAY

in draw() write:

if(myBall.hitCheck()) 
   die();

in the class write:

boolean checkHit() {
  // returns either true or false 
  return (mouseX >= xposb-33 &&  
    mouseY >= yposb-33 &&
    mouseX <= xposb+33 &&
    mouseY <= yposb+33 ) ;  
} // method