Coding game: catching coins and avoiding bombs

Hello, I have a few questions about following code. I have coins, which are being caught by the ship and b0mbs which have to be avoided. However, the b0mbs are somehow stuck with the coins and they aren’t their own objects. The score should start at 0 and every time you catch coins, it should goes up and when a b0mb does hit the ship, the high score and life/health should go down, but it doesn’t work because I don’t know how to code the health and the high score is faulty. I could really use the help. Thanks in advance!

<

//main
int score; //Score Variable
float x = 100;
float y = 500;
int gefangCoins = 0;

PImage image;

Balken balken; //One ship object
Timer timer;
Ball[] ball; //An array of ball objects

void setup() {
size(1280, 720);
smooth();
frameRate(30);
textSize(15);

image = loadImage(“PirateIsland.jpg”);

balken = new Balken(20);
ball = new Ball[100]; //Create 1000 spots in the array
timer = new Timer(300);
timer.start();
}

void draw() {
image(image,width,height);
background(image);

// Display the catcher
balken.display(); //Balken/ship display

// Check the timer
if (timer.isFinished()) {
// Deal with coins
ball[gefangCoins] = new Ball(); // Initialize one coin
gefangCoins++; // Increment totalCoins

// If we hit the end of the array
if (gefangCoins >= ball.length) {
  gefangCoins = 0; // Start over
  score = 0;
}
timer.start();

}

// Move and display all drops
for (int i = 0; i < gefangCoins; i++ ) {
ball[i].move();
ball[i].display();
if (balken.intersect(ball[i])) {
ball[i].caught();
score++;
}
}
textSize(15);
text("Score = "+score, 10, 20);
}

//class Ship/Balken

class Balken {

float xSpeed = 0; //Speed in direction X
float ySpeed = 0; //Speed in direction Y

float r; //Radius
float b, h; //Width and height
color col; // color
float x, y; // location

PImage image;

Balken(float tempR) {

image = loadImage("ship.png");

b = 150;
h = 150;
r = tempR;
col = color(50, 129, 129);
fill(col);
x = 100;
y = 520;

}

void setLocation(float x1, float y1) { //float tempX/Y
x = x1; //tempX
y = y1; //temp Y
}

void move() {
x -= xSpeed; //Increment x
y += ySpeed; //Increment y

// Check horizontal edges
/*  if (x > width || x < 0) {
 xSpeed *= - 1;
 }
 
 // Check vertical edges
 if (y > height || y < 0) {
 ySpeed *= - 1;
 }*/

}

void display() {
stroke(9);
fill(col);

if ((keyPressed == true) && (key == CODED)) {
  if (keyCode == LEFT) {
    x-=10;
  } else if (keyCode == RIGHT) {
    x+=10;
  }
}
  image(image, x, y, b, h);
 
}

// A function that returns true or false based on
// if the catcher intersects a raindrop
boolean intersect(Ball d) {
// Calculate distance
float distance = dist(x, y, d.x, d.y);
if((d.x > x && d.x < x+b) && (dist(d.y, 0, y, 0) < 54 || d.y>=y-22))
{
return true;
} else return false;

// Compare distance to sum of radii
/*if (distance < r + d.r) { 
  return true;
} else {
  return false;
}*/

}
}

//class ball/coins

class Ball {

float x; //X-Koordinate
float y; //Y-Koordinate
float r; // Radius 10

int yDir = 1; //In which direction does it go?
float y_speed = random(1, 7);

int score = 0; //Score Variable
int leben = 5; //Health variable
boolean lost = false; //shows if you won or not

color c; // Define color ‘c’

PImage image;
PImage bombe;

Ball() {
image = loadImage(“coin.png”);
bombe = loadImage(“Bombe.png”);
image.resize(50,0);
bombe.resize(50,0);
r = 10; // All coins are the same size
x = random(width); // Start with a random x location
y = -r*4; // Start a little above the window
y_speed = random(1, 3); // Pick a random speed
c = color(255, 129, 129); //Color
}

// Move the Ball down
void move() {
// Increment by speed
y+=y_speed;
}

//Check if it hits the bottom
boolean Bodenerreicht() {
//If we go beyond the bottom
if (y > height) {
return true;
} else {
return false;
}
}

void display() { //Display the ball
fill(c);
noStroke();
//for(int i = 2; i < r; i++) {
image(image, x, y);
image(bombe, x, y);
//}
}

//If the drop is caught
void caught() {
y_speed = 0; //Stop it from moving by setting speed equal to zero
y= -1000; //Set the location to somewhere way off-screen
}
}

//timer class

class Timer {

int savedTime; // When Timer started
int totalTime; // How long Timer should last

Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}

// Starting the timer
void start() {
// When the timer starts it stores the current time in milliseconds.
savedTime = millis();
}

// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Check how much time has passed
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}

image(image, width, height);

this won’t work. background(image); is better.

  • wrong: image(image, width, height);
  • it would be image(image, 0, 0); OR
  • image(image, 0,0, width, height); // which is not good style (img, pos and size as parameters)

Your question

Look here:

I don’t have your images and can’t run your code but I see here that you have coins and bombs in the same class Ball. That’s the problem!

  • The image for the bomb is called bombe (good name here) the image for the coin is named image (bad name, doesn’t tell its purpose).

Remark

In the function display() in the class Ball you display both images!!!

    image(image, x, y);
    image(bombe, x, y);

That won’t work.

It’s okay that you handle both types in ONE class but you need to distinguish between them!!!

For example make a variable boolean isCoin=false; in the Ball class.

In the constr say if (random(1) < 0.5) isCoin = true; else isCoin = false;

then use isCoin :

    if(isCoin)
          image(image, x, y);
    else 
        image(bombe, x, y);

and use if(isCoin) also when you register a HIT to either decrease health or increase score.

OK?

Warm regards,

Chrisir

1 Like

Thank you so much! The help is really appreciated and it worked too.

1 Like

Update: the health is still not working. The score goes up now but it doesn’t matter if a coin or a bomb hits the ship, even though there should be a difference. It should only go up with the coins and the health doesn’t work at all. I don’t understand what you meant by use if(isCoin) how exactly should I use it in this context?

1 Like

Does it show different images now?

Then you used isCoin already to distinguish between them.

Now use if (isCoin).... to either change score or health
upon a collision

1 Like

It does, but I don’t know where I should use the second isCoin and how exactly I should write it for the health and score

Do you have the collision check?

There you have to have the distinction with isCoin between

  • increasing score OR
  • decreasing health

Please note that it is policy of this forum not to do homework. That’s why I don’t do the full Sketch…

As I tried to explain, when something hits the ship (an item from class Ball I suppose) distinguish whether it was a bomb or a coin:

if(isCoin) 
   score++; 
     else 
  health--;

Show your entire code so we can take a look.

Okay, so the lives do go down now but the game doesn’t stop when the lives hit 0

//main

int score; //Ergebnisanzeige Variable
int lives = 5;
float x = 100;
float y = 500;
int gefangCoins = 0;

PImage image;

Balken balken; //One Balken object
Timer timer;
Ball[] ball; //An array of ball objects

void setup() {
size(1280, 720);
smooth();
frameRate(30);
textSize(15);

image = loadImage(“PirateIsland.jpg”);

balken = new Balken(20);
ball = new Ball[100]; //Create 1000 spots in the array
timer = new Timer(300);
timer.start();
}

void draw() {
image(image,width,height);
//image(image, 0, 0);
background(image);

// Display the catcher
balken.display(); //Balken anzeige

// Check the timer
if (timer.isFinished()) {
// Deal with coins
ball[gefangCoins] = new Ball(); // Initialize one coin
gefangCoins++; // Increment totalCoins

// If we hit the end of the array
if (gefangCoins >= ball.length) {
  gefangCoins = 0; // Start over
  score = 0;
 
}
timer.start();

}

// Move and display all drops
for (int i = 0; i < gefangCoins; i++ ) {
ball[i].move();
ball[i].display();
if (balken.intersect(ball[i])) {
ball[i].caught();
// score++;
}
}
textSize(15);
text("Score = "+score, 10, 20);
text("Lives = "+lives, 10, 40);

}

//class bar/ship

class Balken {

float xSpeed = 0; //Speed in direction X
float ySpeed = 0; //Speed in direction Y

float r; //Radius
float b, h; //Width and Height
color col; // color
float x, y; // location

PImage image;

Balken(float tempR) {

image = loadImage("ship.png");

b = 150;
h = 150;
r = tempR;
col = color(50, 129, 129);
fill(col);
x = 100;
y = 520;

}

void setLocation(float x1, float y1) { //float tempX/Y
x = x1; //tempX
y = y1; //temp Y
}

void move() {
x -= xSpeed; //Increment x
y += ySpeed; //Increment y

}

void display() {
stroke(9);
fill(col);

if ((keyPressed == true) && (key == CODED)) {
  if (keyCode == LEFT) {
    x-=10;
  } else if (keyCode == RIGHT) {
    x+=10;
  }
}
  image(image, x, y, b, h);
 
}

// A function that returns true or false based on
// if the catcher intersects a raindrop
boolean intersect(Ball d) {
// Calculate distance
float distance = dist(x, y, d.x, d.y);
if((d.x > x && d.x < x+b) && (dist(d.y, 0, y, 0) < 54 || d.y>=y-22))
{
return true;
} else return false;

}
}

//class ball

class Ball {

float x; //X-Koordinate
float y; //Y-Koordinate
float r; // Radius 10

int yDir = 1; //In Welche Richtung geht es?
float y_speed = random(1, 7);

int score = 0; //Ergebnisanzeige Variable
int leben = 5; //Lebensanzeige variable
boolean lost = false; //Anzeige ob man verloren hat oder nicht

color c; // Define color ‘c’

PImage muenze;
PImage bombe;

boolean isCoin ;

Ball() {
muenze = loadImage(“coin.png”);
bombe = loadImage(“Bombe.png”);
muenze.resize(50, 0);
bombe.resize(50, 0);
r = 10; // All coins are the same size
if (random(1) < 0.5)
{
isCoin = true;
} else {
isCoin = false;
}

x = random(width);    // Start with a random x location
y = -r*4;      // Start a little above the window
y_speed = random(1, 3);      // Pick a random speed
c = color(255, 129, 129);    //Color

}

// Move the Ball down
void move() {
// Increment by speed
y+=y_speed;
}

//Check if it hits the bottom
boolean Bodenerreicht() {
//If we go beyond the bottom
if (y > height) {
return true;
} else {
return false;
}
}

void display() { //Display the ball
fill(c);
noStroke();
// image(muenze, x, y);
// image(bombe, x, y);

 if(isCoin)
      image(muenze, x, y);
else 
    image(bombe, x, y);

}

//If the drop is caught
void caught() {
y_speed = 0; //Stop it from moving by setting speed equal to zero
y= -1000; //Set the location to somewhere way off-screen

if(isCoin) 

score++;

 else 

lives–;

}

}

//class timer

class Timer {

int savedTime; // When Timer started
int totalTime; // How long Timer should last

Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}

// Starting the timer
void start() {
// When the timer starts it stores the current time in milliseconds.
savedTime = millis();
}

// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Check how much time has passed
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}

I see. Did you implement that the games stops when the lives hit 0?

In which line or function is it?

processing won’t do this automatically, you know.