This site requires JavaScript, please enable it in your browser!
Greenfoot back
yesineedhelp
yesineedhelp wrote ...

2023/12/3

Making a Asteroids Game (help needed).

yesineedhelp yesineedhelp

2023/12/3

#
In my current Asteroid Game, there is a Rocket who shoots bullets to destory Asteroid(s). I want my game to start off with 2 Asteroids and once the Rocket destory them both, 2 +1 more Asteroids spawn. This will represent levels (Level 1 = 2 Asteroids; Level 2 = 3 Asteroids; Level 3 = 4 Asteroids; Continues until Level 10 or something). Also, it's important to note that this game is made so that if a big Asteroid is shot upon, it breaks up into 2, then shot again, 4 small Asteroid part. I though the best way for me to imply this is by the Score/points (big Asteroid = 1 and small Asteroid = 4). Like said, there being 2 starting Asteroids, the final point you could receive for Level 1 is 46. Knowing this, this was my attempt
public Counter scoreCounter;
private int startAsteroids = 2;

public void addAsteroid()
{
if(scoreCounter.getValue() == 46)
     {
          LevelUp lu = new LevelUp();
          addObject(lu,300,250);
           
           addAsteroids(startAsteroids +1);  
      }

}
Counter is a Actor class and the score already works. Summarize, I want to add 1+ Asteroid when current amount of Asteroids are all taken out by the Rocket. Please help and a example of the codes would be helpful. Thank you!
danpost danpost

2023/12/5

#
yesineedhelp wrote...
I want to add 1+ Asteroid when current amount of Asteroids are all taken out by the Rocket.
A better question to ask at line 6 is 'Are there not any asteroids in the world?':
if (getObjects(Asteroid.class).isEmpty())
That way, the score value is irrelevant as far as when to change levels. It would take a bit of computation to come up with the end level scores to be checked on. The variable 'startAsteroids' can be modified to better represent what you want. In fact, 'level' would be a more useful thing to track. So, you might have this:
public Counter scoreCounter;
private int level;

public MyWorld() {
    super(600, 600, 1);
    scoreCounter = new Counter("Score:  ");
    addObject(scoreCounter, 150, 20);
    upLevel();
}

private void upLevel() {
    level++;
    addAsteroids(level+1);
}

private void addAsteroids() {
    // creates and adds asteroids into world
}

public void act() {
    if (getObjects(Asteroid.class).isEmpty()) upLevel();
    if (getObjects(Rocket.class).isEmpty()) gameOver();
}

private void gameOver() {
    // show "Game Over" text
    Greenfoot.stop();
}
yesineedhelp yesineedhelp

2023/12/5

#
Thank you, it worked; however, one single thing, upon reaching a new level, I want a sound to play. Since at the start, there are no asteroids, the game levels up, meaning we are adding a upLevel and the sound is playing at the begining, which I don't want. I put the Sound in the upLevel:
private void upLevel() {
    level++;
    addAsteroids(level+1);
    Greenfoot.playSound("Level Up.wav");
}
I think the problem is the "if (getObjects(Asteroid.class).isEmpty()) upLevel();" in the Act method. Please help if you can again, thank you.
danpost danpost

2023/12/6

#
yesineedhelp wrote...
I think the problem is the "if (getObjects(Asteroid.class).isEmpty()) upLevel();" in the Act method. Please help if you can again, thank you.
That line is not exactly the problem. However, that if statement needs to play the sound within its block -- not in the upLevel method:
if (getObjects(Asteroid.class).isEmpty()) {
    upLevel();
    Greenfoot.playSound("Level Up.wav");
}
The name of the sound file may be a problem when converted to javascript. I am not sure if a space is allowed in the name.
yesineedhelp yesineedhelp

2023/12/6

#
Thank you, it works. I guess I was thinking wrong.
You need to login to post a reply.