So I am making a game on Processing and I cant seem to be finding how to make this if statements condition. the condition is supposed to deduct a life when my circle touches a block I have the idea but I just can't put it in code please help me. The "if" the condition is below and the full game code is right after that.
void difficulty()
{
if(/*what do i write here*/){
lives--;
}
else{
fill(0, 255, 0);
}
}
/////// The main game code starts from here////////
int score;
int score1;
int miss;
int lives=10;
int ballx, bally;
class Rect
{
float x;
float y;
float speed;
float leng;
color c;
boolean valid;
final int MAX_COLOR = 255;
final int MIN_X = 50, MAX_X = 750;
final int MIN_Y = -800, MAX_Y = -100;
int MIN_SPEED = 1, MAX_SPEED = 2;
final int MIN_Leng = 50, MAX_Leng =100 ;
Rect()
{
initAll();
}
void initAll() {
valid = true;
c = color(random(255), random(255), random(255));
x = random(MIN_X, MAX_X);
y = random(MIN_Y, MAX_Y);
speed = random(MIN_SPEED, MAX_SPEED);
leng = random(MIN_Leng, MAX_Leng);
}
void update() {
if (!valid) {
initAll();
return;
}
move();
draw_rect();
}
void draw_rect()
{
fill(c);
rect (x, y, leng, leng);
}
void move()
{
if (y-leng <= height)
{
y += speed;
} else if (y-leng > height )
{
valid = false;
miss++;
}
}
void difficulty()
{
if(){
lives--;
}
else{
fill(0, 255, 0);
}
}
void GameOver()
{
if (lives==0)
{
for (int i = 0; i < Obj.length; i++)
{
Obj[i] = new Rect();
}
background(0);
textSize(50 );
fill(255);
text( "You Lose ", 15, 150);
text( "Score: " + score, 15, 100);
}
}
boolean isOver(int mx, int my) {
float disX = x - mx;
float disY = y - my;
if (sqrt(sq(disX) + sq(disY)) < leng/2 ) {
return true;
} else {
return false;
}
}
}
Rect [] Obj = new Rect [10];
void setup() {
size (400, 600);
ballx=200;
bally=300;
for (int i = 0; i < Obj.length; i++)
{
Obj[i] = new Rect();
}
}
void draw() {
background(0);
textSize(50);
text( "Score: " + score, 0, 100);
text("Lives: " + lives, 0, 50);
ellipse(ballx,bally,20,20);
for (int i = 0; i < Obj.length; i++) {
Obj[i].update();
Obj[i].difficulty();
Obj[i].GameOver();
}
surface.setTitle(nf(frameRate, 3, 2));
}
void keyPressed(){
for(Rect s : Obj){
if ( key =='q' ){
ballx=ballx-2;
score++;
score1++;
s.valid = false;
break;
}
if ( key =='e'){
ballx=ballx+2;
score++;
score1++;
s.valid = false;
break;
}
}
}
What you want to do is check if the player is colliding with any of the rectangles.
Think about this, what does it mean for the player to collide with a rectangle?
It means that the x-position of the player has to be larger than the x-position of the left, but smaller than the x-positon of the right side of the rectangle. The same goes for the y-positions.
Since you need to check the position of all rectangles, you should use a for or a foreach loop.
You could now just do what I said until now, and it would work, but you should also take into account the size of the player itself. The player should still collide with the rectangles if his left/right side collides with them, not just his center. The player is technically a circle, but for simplification, we will approximate it as a rectangle.
boolean isColliding = false;
for(Rect r : Obj)
{
if (ballx + 10 > r.x && //is the right side of the ball further right than the left side of the rectangle?
ballx - 10 < r.x + r.leng && //is the left side of the ball further left than the right side of the rectangle?
//if both of the above are true, the balls x-position is between the left and right side of the ball
bally + 10 > r.y && //is the bottom of the ball further down than the top of the rectangle?
bally - 10 < r.y + r.leng) { //is the top of the ball further up than the bottom of the rectangle?
isColliding = true; //if all of the above are true, the ball is colliding with the rectangle
}
}
if (isColliding) {
lives--;
} else {
fill(0, 255, 0);
}