Lives Counter in Game is infinite.
in
Programming Questions
•
5 months ago
Hi,
So I am attempting to create a simple game where a cat is catching mice. For each mouse that hits the bottom, I want the lives counter to go down by 1, and at 0, cue an end screen. Now when it runs, as soon as one mouse hits the bottom the counter starts counting down infinitely. This is a similar code project to the example in Learning Processing
http://www.learningprocessing.com/examples/chapter-10/example-10-10/
Here is my code:
Cat cat;
Timer timer;
Mouse[] mouse;
int totalMice = 0;
PImage img, img1, bg;
boolean gameOver =false;
int score=0;
int lives=10;
PFont f;
void setup() {
size(600,600);
smooth();
img = loadImage("cat.gif");
img1= loadImage("mouse.gif");
bg= loadImage("frame.gif");
cat = new Cat(50);
mouse = new Mouse[1000];
timer = new Timer(500);
timer.start();
f = createFont("Georgia",50 );
}
void draw() {
boolean gameOver =false;
if (gameOver) {
textFont(f);
textAlign(CENTER);
fill(0);
text("OVERWELMED BY MICE!", width/2,height/2);
}
else{
background(bg);
cat.location(mouseX,500);
cat.display();
if (timer.finish()) {
mouse[totalMice] = new Mouse();
totalMice ++ ;
if (totalMice >= mouse.length) {
totalMice = 0;
}
timer.start();
}
}
for (int i = 0; i < totalMice; i++ ) {
mouse[i].move();
mouse[i].display();
if (cat.intersect(mouse[i])) {
mouse[i].caught();
score++;
}
if(mouse[i].reachedBottom()){
mouse[i].caught();
lives=lives-1;
}
}
if(lives<=0){
gameOver=true;
}
textFont(f,20);
text("Score: "+score, 20,30);
textFont(f,20);
text("Lives: "+lives, 20,50);
}
and the Code for the Mouse object:
class Mouse {
float x,y;
float speed;
float r;
boolean caught = false;
Mouse() {
r = 8;
x = random(20,580);
y = -r*2;
speed = random(1,6);
}
void move() {
y += speed;
}
boolean reachedBottom() {
if (y > height) {
return true;
} else {
return false;
}
void display() {
imageMode(CENTER);
image(img1,x,y);
}
void caught() {
caught=true;
speed = 0;
y = 700;
}
}
1