Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:45:33+00:00 2026-06-05T11:45:33+00:00

I want to accomplish a few things. First is to create lives and start

  • 0

I want to accomplish a few things.
First is to create lives and start with 3.
Game Over on 0 lives.
Lose a life if you get hit by a ghost.
Lose a life if you fall below a certain distance.

Currently it’s just taking the lives count and goes negative without stopping. So it’s ignoreing my checkGameOver

public class World {
    public interface WorldListener {
        public void jump();

        public void highJump();

        public void hit();

        public void coin();

        public void dying();
    }

    public static final float WORLD_WIDTH = 10;
    public static final float WORLD_HEIGHT = 15 * 20;
    public static final int WORLD_STATE_RUNNING = 0;
    public static final int WORLD_STATE_NEXT_LEVEL = 1;
    public static final int WORLD_STATE_GAME_OVER = 2;
    public static final Vector2 gravity = new Vector2(0, -12);

    public final Hero hero;
    public final List<Platform> platforms;
    public final List<Spring> springs;
    public final List<Ghost> ghosts;
    public final List<Coin> coins;
    public Castle castle;
    public final WorldListener listener;
    public final Random rand;

    public float heightSoFar;
    public int score;
    public int state;
    public int lives=3;

    public World(WorldListener listener) {
        this.hero = new Hero(5, 1);
        this.platforms = new ArrayList<Platform>();
        this.springs = new ArrayList<Spring>();
        this.ghosts = new ArrayList<Ghost>();
        this.coins = new ArrayList<Coin>();
        this.listener = listener;
        rand = new Random();
        generateLevel();

        this.heightSoFar = 0;
        this.score = 0;
        this.state = WORLD_STATE_RUNNING;
    }

    private void generateLevel() {
        float y = Platform.PLATFORM_HEIGHT / 2;
        float maxJumpHeight = Hero.hero_JUMP_VELOCITY * Hero.hero_JUMP_VELOCITY
                / (2 * -gravity.y);
        while (y < WORLD_HEIGHT - WORLD_WIDTH / 2) {
            int type = rand.nextFloat() > 0.8f ? Platform.PLATFORM_TYPE_MOVING
                    : Platform.PLATFORM_TYPE_STATIC;
            float x = rand.nextFloat()
                    * (WORLD_WIDTH - Platform.PLATFORM_WIDTH)
                    + Platform.PLATFORM_WIDTH / 2;

            Platform platform = new Platform(type, x, y);
            platforms.add(platform);

            if (rand.nextFloat() > 0.9f
                    && type != Platform.PLATFORM_TYPE_MOVING) {
                Spring spring = new Spring(platform.position.x,
                        platform.position.y + Platform.PLATFORM_HEIGHT / 2
                                + Spring.SPRING_HEIGHT / 2);
                springs.add(spring);
            }

            if (rand.nextFloat() > 0.7f) {
                Ghost ghost = new Ghost(platform.position.x
                        + rand.nextFloat(), platform.position.y
                        + Ghost.GHOST_HEIGHT + rand.nextFloat() * 3);
                ghosts.add(ghost);
            }

            if (rand.nextFloat() > 0.6f) {
                Coin coin = new Coin(platform.position.x + rand.nextFloat(),
                        platform.position.y + Coin.COIN_HEIGHT
                                + rand.nextFloat() * 3);
                coins.add(coin);
            }

            y += (maxJumpHeight - 0.5f);
            y -= rand.nextFloat() * (maxJumpHeight / 3);
        }

        castle = new Castle(WORLD_WIDTH / 2, y);
    }

public void update(float deltaTime, float accelX) {
    updatehero(deltaTime, accelX);
    updatePlatforms(deltaTime);
    updateGhosts(deltaTime);
    updateCoins(deltaTime);
    if (hero.state != Hero.hero_STATE_HIT)
        checkCollisions();
    checkGameOver();
    checkFall();
}

private void updatehero(float deltaTime, float accelX) {
    if (hero.state != Hero.hero_STATE_HIT && hero.position.y <= 0.5f)
        hero.hitPlatform();
    if (hero.state != Hero.hero_STATE_HIT)
        hero.velocity.x = -accelX / 10 * Hero.hero_MOVE_VELOCITY;
    hero.update(deltaTime);
    heightSoFar = Math.max(hero.position.y, heightSoFar);
}

private void updatePlatforms(float deltaTime) {
    int len = platforms.size();
    for (int i = 0; i < len; i++) {
        Platform platform = platforms.get(i);
        platform.update(deltaTime);
        if (platform.state == Platform.PLATFORM_STATE_PULVERIZING
                && platform.stateTime > Platform.PLATFORM_PULVERIZE_TIME) {
            platforms.remove(platform);
            len = platforms.size();
        }
    }
}

private void updateGhosts(float deltaTime) {
    int len = ghosts.size();
    for (int i = 0; i < len; i++) {
        Ghost ghost = ghosts.get(i);
        ghost.update(deltaTime);
        if (ghost.state == Ghost.GHOST_STATE_DYING
                && ghost.stateTime > Ghost.GHOST_DYING_TIME) {
            ghosts.remove(ghost);
            len = ghosts.size();
        }
    }
}

private void updateCoins(float deltaTime) {
    int len = coins.size();
    for (int i = 0; i < len; i++) {
        Coin coin = coins.get(i);
        coin.update(deltaTime);
    }
}

    private void checkCollisions() {
        checkPlatformCollisions();
        checkGhostCollisions();
        checkItemCollisions();
        checkCastleCollisions();
    }

    private void checkPlatformCollisions() {
        if (hero.velocity.y > 0)
            return;

        int len = platforms.size();
        for (int i = 0; i < len; i++) {
            Platform platform = platforms.get(i);
            if (hero.position.y > platform.position.y) {
                if (OverlapTester
                        .overlapRectangles(hero.bounds, platform.bounds)) {
                    hero.hitPlatform();
                    listener.jump();
                    if (rand.nextFloat() > 0.5f) {
                        platform.pulverize();
                    }
                    break;
                }
            }
        }
    }

    private void checkGhostCollisions() {
        int len = ghosts.size();
        for (int i = 0; i < len; i++) {
            Ghost ghost = ghosts.get(i);
            if (hero.position.y < ghost.position.y) {
                if (OverlapTester.overlapRectangles(ghost.bounds, hero.bounds)){
                    hero.hitGhost();
                    listener.hit();
                    lives--;
                }
                break;
            } else {
            if(hero.position.y > ghost.position.y) {
                 if (OverlapTester.overlapRectangles(hero.bounds, ghost.bounds)){
                     hero.hitGhostJump();
                     listener.jump();
                     ghost.dying();
                     score += Ghost.GHOST_SCORE;
                 }
                break;
                }
            }
        }
    }


    private void checkItemCollisions() {
        int len = coins.size();
        for (int i = 0; i < len; i++) {
            Coin coin = coins.get(i);
            if (OverlapTester.overlapRectangles(hero.bounds, coin.bounds)) {
                coins.remove(coin);
                len = coins.size();
                listener.coin();
                score += Coin.COIN_SCORE;
            }

        }

        if (hero.velocity.y > 0)
            return;

        len = springs.size();
        for (int i = 0; i < len; i++) {
            Spring spring = springs.get(i);
            if (hero.position.y > spring.position.y) {
                if (OverlapTester.overlapRectangles(hero.bounds, spring.bounds)) {
                    hero.hitSpring();
                    listener.highJump();
                }
            }
        }
    }

    private void checkCastleCollisions() {
        if (OverlapTester.overlapRectangles(castle.bounds, hero.bounds)) {
            state = WORLD_STATE_NEXT_LEVEL;
        }
    }

    private void checkFall() {          
        if (heightSoFar - 7.5f > hero.position.y) {
         --lives;
                    }
    }

        public boolean checkGameOver() {
             --lives;
             return isDead();
        }
        public boolean isDead(){
             return lives<1;
        }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:45:35+00:00Added an answer on June 5, 2026 at 11:45 am

    checkGameOver() returns true when the number of lives is zero or less, but you never do anything in response to that. In the update method, you will need something like this:

    if (checkGameOver()) {
        /* show a Game Over screen, or something like that */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to accomplish a few tasks: Open a Word document through my program
Maybe I should first give an idea of what I want to accomplish as
First off, before I want to get to the question, I want to say
I want to accomplish this (mysql) query: UPDATE table SET field = field +
I want to accomplish something like the following (my interest is in the toInt()
I want to accomplish nearly the same thing as this question , which is
What I want to accomplish is the effect I have seen in this site
What I want to accomplish: [a, b, c, d] -> [ (a, x), (b,
This question does pretty much what I want to accomplish, but my table is
Im not sure that its possible to do what I want to accomplish. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.