We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Infinite Score! Not so good...
Page Index Toggle Pages: 1
Infinite Score! Not so good... (Read 786 times)
Infinite Score! Not so good...
Dec 4th, 2009, 4:50pm
 
I'm trying to get the score variable to increase by 2 every time an enemy is 'killed'. I've tried inserting code within the second if statement below.

Code:

for (int i = 0; i < bubbles.length; i++) {
bubbles[i].move();
bubbles[i].display();

if (i == bubbles.length) {
i = 0;
}

//Checks through all enemies and detects collisions, subsequently 'bursting'
//the bubble and 'killing' the enemy

for (int j = 0; j < enemies.length; j++) {
if (bubbles[i].hit(enemies[j])) {
bubbles[i].burst();
enemies[j].kill();
}
}
}


I've also tried inserting it in the burst() function in the Bubble class. In both cases, whenever an enemy is 'killed', the score proceeds to increase rapidly continually; it must be something to with the fact that the increase is included in a for loop. Here is the whole code:

Code:

Bubble[] bubbles = new Bubble[100];
Enemy[] enemies = new Enemy[1000];

Otter otter;

int bubCount;
int totalEnemies;
int startTime;
float enemyAttack = random(1000,3000);
int score;
PFont scoreFont;
PImage otterPic;
PImage bubblePic;
PImage fishPic;
PImage octopusPic;
PImage sharkPic;
PImage bgPic;

void setup() {
size(500,500);
background(255);
smooth();
noCursor();

startTime = millis();
otterPic = loadImage("Otter.png");
bubblePic = loadImage("Bubble.png");
fishPic = loadImage("Fish.png");
octopusPic = loadImage("Octopus.png");
sharkPic = loadImage("Shark.png");
bgPic = loadImage("Background.png");
scoreFont = loadFont("AppleCasual-24.vlw");
textFont(scoreFont, 24);

//Initialise all objects (otter, bubbles, and enemies)

otter = new Otter();

for (int i = 0; i < bubbles.length; i++) {
bubbles[i] = new Bubble();
}

for (int i = 0; i < enemies.length; i++) {
enemies[i] = new Enemy();
}

}

void draw() {
background(152,197,240);
imageMode(CORNER);
image(bgPic,-20,-20,570,570);

fill(0);
text("Score: " + score, 15, 30);

//Display the otter at the mouse location

otter.display();
otter.setLocation(mouseX,mouseY);

//Spawn the first enemy 1-3s after the game starts, then every
//1-2s after that

if ((millis() - startTime) > enemyAttack) {
enemies[totalEnemies].alive = true;

if (totalEnemies >= enemies.length) {
totalEnemies = 0;
}

totalEnemies++;
startTime = 0;
enemyAttack = enemyAttack + (random(500,1500));
}

//Display bubbles (only if alive/after mouse click)

for (int i = 0; i < bubbles.length; i++) {
bubbles[i].move();
bubbles[i].display();

if (i == bubbles.length) {
i = 0;
}

//Checks through all enemies and detects collisions, subsequently 'bursting'
//the bubble and 'killing' the enemy

for (int j = 0; j < enemies.length; j++) {
if (bubbles[i].hit(enemies[j])) {
bubbles[i].burst();
enemies[j].kill();
}
}
}

//Display enemies (only if alive/after elapsed time)

for (int i = 0; i < enemies.length; i++) {
enemies[i].move();
enemies[i].display();

if (i == enemies.length) {
i = 0;
}

//Kill enemies if they reach the left side of the screen

if (enemies[i].reachedEnd()) {
enemies[i].kill();
}
}

}

//Create bubbles upon mouse click with the same x and y position
//as the otter

void mouseClicked() {
bubbles[bubCount].alive = true;

bubbles[bubCount].x = otter.x;
bubbles[bubCount].y = otter.y;

if (bubCount >= bubbles.length - 1) {
bubCount = 0;
}

bubCount++;
}




class Bubble {

int r;
int speedX;
int x;
int y;
boolean alive;

Bubble() {
r = 10;
x = otter.x;
y = otter.y;
speedX = 10;
alive = false;
}

void display() {
if (alive) { //Only display if alive (after mouse click)
image(bubblePic, x+30, y, r, r);
}
}

void move() {
if (alive) { //Only move if alive (after mouse click)
x = x + speedX;
}
}

void burst() {
alive = false;
x = 1000;
y = -2000;
speedX = 0;
}

boolean hit(Enemy a){
float distance = dist(x,y,a.x,a.y);
if (distance < r + a.r) {
return true;
} else {
return false;
}
}

}




class Enemy {

float x,y;
float speed;
int r;
boolean alive;
float rand;

Enemy() {
r = 20;
x = height + r*2;
y = random(height);
speed = random(5,15);
alive = false;
rand = random(1,10);
}

void move() {
if (alive) { //Only move if alive (after elapsed time)
x -= speed; //Move left
}
}

//Determines whether the enemies have reached the left side of the
//screen

boolean reachedEnd() {
if (x < 0 - r*2) {
x = 1000;
y = -2000;
score--;
return true;
}
else {
return false;
}
}

void display() {
if (alive) { //Display only if alive (after elapsed time)
if (rand <= 8) {
image(fishPic, x, y, r, r);
}
else if (rand >= 9.5) {
image(octopusPic, x, y, 5*r, 5*r);
}
else {
image(sharkPic, x, y, 3*r, 3*r);
}
}
}

void kill() {
alive = false;
x = 1000;
y = -2000;
speed = 0;
}

}




class Otter {

int x;
int y;

Otter() {
}

void display() {
imageMode(CENTER);
image(otterPic, x, y, 80, 21);
}

void setLocation(int tempX, int tempY) {
x = tempX;
y = tempY;
}
}
Re: Infinite Score! Not so good...
Reply #1 - Dec 5th, 2009, 1:32am
 
Quote:
if (i == bubbles.length) {
     i = 0;
   }
Test is useless because i never reaches bubbles.length inside the loop. And because I don't see why you do it.

You should check alive status in hit() too.
Re: Infinite Score! Not so good...
Reply #2 - Dec 6th, 2009, 3:16pm
 
Testing if alive in the hit() function did the job nicely Smiley Cheers!
Page Index Toggle Pages: 1