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 4 of 4

Thread: Game w/ If Statements

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Game w/ If Statements

    Suppose your writing a simple game code and you want to have the user select any number of stages and each stage has 10 enemies and you only get 2 shots per stage (that the user can input). Lets look at the if code part...

    public void shoot(int enemies){
    if (!isOver){  //Method that ends the game.
    enemiesUp = enemiesUp - enemies;
     
    if(shoot == 1 && enemiesUp < 1 && stage != totalStages){  //last stage you can shoot 3 times.
    stage++;
    }

    When you test it goes good after the first shot, but the 2nd shot screws up on the count, getShoot should come back with the 2nd shot, and comes back on the 1st shot. Should I add shoot++; to that if statement? The enemies are doing great till I get to the 3 shoot() call, enemies should reset back to 10 after the 2nd shot, but my test is going into the negative.

    Should be--> Stage: 1  Enemies: 10  Shoot: 1
    Stage: 1 Enemies: 10 Shoot: 1
     
    Should be--> Stage: 1  Enemies: 5  Shoot: 1
    Stage: 1 Enemies: 5 Shoot: 1
     
    Should be--> Stage: 1  Enemies: 2  Shoot: 2
    Stage: 1 Enemies: 2 Shoot: 1
     
    Should be--> Stage: 2  Enemies: 5  Shoot: 1
    Stage: 2 Enemies: -3 Shoot: 1
     
    Should be--> Stage: 2  Enemies: 10  Shoot: 2
    Stage: 3 Enemies: -8 Shoot: 1

    Should I make more nested If statements, or else if?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Game w/ If Statements

    It's not clear how your project is set up, but it appears you have multiple opportunities to change important game state variables, like stage, enemies, shots, etc., and you've lost control or can't keep track of what's changing when. Rather than sprinkling those changes throughout your code, centralize them into methods like resetShots(), resetEnemies(), incrementShots(), incrementEnemies(), etc. that are called only when needed and do exactly as required to keep the game state under control.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Game w/ If Statements

    Quote Originally Posted by GregBrannon View Post
    It's not clear how your project is set up, but it appears you have multiple opportunities to change important game state variables, like stage, enemies, shots, etc., and you've lost control or can't keep track of what's changing when. Rather than sprinkling those changes throughout your code, centralize them into methods like resetShots(), resetEnemies(), incrementShots(), incrementEnemies(), etc. that are called only when needed and do exactly as required to keep the game state under control.
    I'm going to add a instance variable to help me keep track. If you use the shoot() method, and it produces a result, enemiesUp... how would you mathematically do that? Cause, enemiesUp = enemiesUp - enemies... lets call this new variable previousShot, you don't want to say previousShot = enemiesUp, that's just redundant. Is there a class I'm not realizing could help me? Like previousShot = getlastCall.shoot()? lol

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Game w/ If Statements

    It's hard to say without seeing more code. I have no idea what classes you have or don't have. My initial impression was that you've built the whole thing in one class run mostly from the main() method. Hopefully, you're actually beyond that, but who can tell?

    Each stage, weapon, enemy, bullet, etc. should probably have its own class and draw itself and keep track of itself and all of its resources. Beyond that, I'm not sure what to tell ya.

Similar Threads

  1. Help my if statements!
    By l2code in forum Loops & Control Statements
    Replies: 1
    Last Post: March 22nd, 2013, 01:47 PM
  2. [SOLVED] If and else if statements???
    By skeptile2 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 5th, 2013, 01:44 AM
  3. If Statements help
    By cowsquad in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2013, 06:41 AM
  4. Stuck on a few Statements/Questions on timing of Main Game Loop.
    By StevenW in forum Java Theory & Questions
    Replies: 3
    Last Post: July 19th, 2011, 09:20 AM
  5. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM