Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Simple Spaceship Shooting Game

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Simple Spaceship Shooting Game

    Alright, this is for a programming project which consists of the classes: Game, EnemyShip, PlayerShip and Guns.

    For some reason, I cannot update the enemy's life and the gun's life.

    I have passed the enemy as a parameter from the game class, player class and then to the gun class.

    Here is a part of my code...

    GAME CLASS
    case 'f':
    'F':
    player.fire(enemy1)
    player.fire(enemy2)
    player.fire(enemy3)

    ------
    PLAYERSHIP CLASS

    public void fire(EnemyShip enemy)
    {
    gun1.fire(enemy);
    gun2.fire(enemy);
    }
    -------
    GUN CLASS
    public void fire(EnemyShip enemy) // the gun attemps to fire at the enemy ship
    {
    if (x == enemy.getPosition())
    {
    enemy.updateLife(getPower());
    if (enemy.isDestroyed())
    updatePoints(0);
    }
    }
    ------
    ENEMY CLASS
    public boolean isDestroyed()
    {
    if (life == 0)
    return true;
    else
    return false;
    }
     
    public void updateLife(int damagetaken) // returns the remaining life (damagetaken = gun's powerlevel)
    {
    life = life - damagetaken;
    }

    Thanks very much for your help!
    Last edited by bj12marion; April 20th, 2012 at 07:24 AM. Reason: minor errors in code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple Spaceship Shooting Game

    I cannot update the enemy's life and the gun's life.
    Please explain. Are you having problems writing the code or is the code you have written not doing what you want?
    Where is "life" defined and how is its value updated?
    Try debugging the code by adding println statements to print out the value of life every time it is changed and used. The print outs will show you what the computer is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    bj12marion (April 20th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple Spaceship Shooting Game

    Sorry about that. I am having problems about the code not doing as I wanted it to do.

    And I have life defined in the enemy class...

    and this is how I update it:

    public void updateLife(int damagetaken) // returns the remaining life (damagetaken = gun's powerlevel)
    {
    life = life - damagetaken;
    }

    And thanks. I'll try the debugging and see what actually happens...

    ---

    (EDIT)
    I tried debugging it, and I've realised that the life has not been updated. Gosh. I'll see what I can do for now.

    ---

    (EDIT AGAIN)
    Thanks very much! It's all good now. Even just debugging helped a lot! (So much for being a beginner
    Last edited by bj12marion; April 20th, 2012 at 11:04 AM. Reason: more explanation

Similar Threads

  1. I have a problem with my code of a simple game
    By byrne in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 17th, 2012, 09:38 AM
  2. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM
  3. Simple game problem
    By frozen java in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 30th, 2011, 09:14 PM
  4. Programming AI for simple game?
    By YouGoLast in forum Java Theory & Questions
    Replies: 2
    Last Post: May 28th, 2011, 08:53 AM
  5. Simple game in Java
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:04 AM