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;
}
}
}