13 Replies - 907 Views - Last Post: 14 July 2017 - 08:23 AM Rate Topic: -----

#1 Snaef98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-July 17

XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 11 July 2017 - 09:23 PM

My code for turn based combat works but there is a problem with it running through the entire battle rapidly so a draw issue and to solve this I want to have an option for "Press enter key to continue to next combat round." and I can't seem to figure out how to implement this. Here is my code:

public void Combat()
        {
            if (IsPlayerInCombat)
            {
                if (TargetMonsterID == 1)
                {
                    while ((PlayerHP > 0) && (IsTargetMonsterAlive) && (IsPlayerInCombat))
                    {
                        if (PlayerRound)
                        {
                             // Checks To See If Player Hits
                             Random rnd = new Random();
                             int PlayerHitChance = rnd.Next(1, 100);
                             if (PlayerHitChance <= PlayerAcc)
                             {
                                 // Player Hits
                                 Random rnd2 = new Random();
                                 int PlayerDmg = PlayerAtk - TargetMonsterDefense;
                                 int PlayerHitDmg = rnd2.Next(1, PlayerDmg);
                                 TargetMonsterHP = TargetMonsterHP - PlayerHitDmg;
                                 CombatRoundDisplayDmg = PlayerHitDmg;
                                 CombatRoundDisplay = "You hit the target!";
                                 PlayerRound = false;
                                 TargetMonsterRound = true;
                                 
                                 if (TargetMonsterHP <= 0)
                                 {
                                        IsTargetMonsterAlive = false;
                                        PlayerExp = PlayerExp + TargetExpGranted;
                                        PlayerGold = PlayerGold + TargetGoldGranted;
                                        CombatRoundDisplay = "Victory! Monster defeated!";
                                        IsPlayerInCombat = false;
                                 }
                             }
                             else if (PlayerHitChance > PlayerAcc)
                             {
                                    // Player Misses
                                    CombatRoundDisplay = "You missed the target!";
                                    PlayerRound = false;
                                    TargetMonsterRound = true;
                             }
                       }
                       else if (TargetMonsterRound)
                       {
                            // Checks To See If Monster Hits
                            Random rnd3 = new Random();
                            int MonsterHitChance = rnd3.Next(1, 100);
                            if (MonsterHitChance <= 75)
                            {
                                 // Monster Hits
                                 Random rnd4 = new Random();
                                 int MonsterHitDmg = rnd4.Next(1, TargetMonsterAttack);

                                 if (MonsterHitDmg <= 0)
                                 {
                                     // Monser Hits For Zero Damage
                                     CombatRoundDisplay = "The monster hit you but your armor absorbed the damage!";
                                     TargetMonsterRound = false;
                                     PlayerRound = true;
                                 }
                                 else if (MonsterHitDmg >= 1)
                                 {
                                     // Monster Hits For Damage
                                     CombatRoundDisplay = "The monster has hit you!";
                                     PlayerHP = PlayerHP - MonsterHitDmg;
                                     TargetMonsterRound = false;
                                     PlayerRound = true;

                                     if (PlayerHP <= 0)
                                     {
                                         CombatRoundDisplay = "Player has died. Game over!";
                                         gameState = GameState.Menu;
                                     }
                                 }
                             }
                             else if (MonsterHitChance > 75)
                             {
                                 // Monster Misses
                                 CombatRoundDisplay = "The monsters attack missed you!";
                                 TargetMonsterRound = false;
                                 PlayerRound = true;
                             } 
                       }
                    }
                }
                else if (TargetMonsterID == 2)
                {

                }
                else if (TargetMonsterID == 3)
                {

                }

            }
     

        } 


Is This A Good Question/Topic? 0
  • +

Replies To: XNA 4.0 Help with while loop - Need to be able pause each combat round

#2 Snaef98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-July 17

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 13 July 2017 - 10:39 PM

I'm looking for suggestions to tackle an issue I'm having. I have a while loop to do combat calculations and im having an issue with the draw portion of my code. The entire loop runs and combat ends and it just rapidly draws all the text in the window as if it didn't even happen. Its tough to explain im trying to find a way pause via key input or a better way to layout my draw code so each round of battle text shows up. Possibly some kind of scrolling ability? I'm not using a console app.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,121
  • Joined: 05-May 12

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 14 July 2017 - 04:58 AM

You already have a topic open about this. Please do not create duplicate threads. Just reply back on your old thread.

Merging threads...
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,121
  • Joined: 05-May 12

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 14 July 2017 - 05:11 AM

The basic problem is that it you don't understand how the XNA game loop works. You need to work within that game loop instead of creating your own loop. That means you will need to make a state machine with 4 states: human player, wait, computer player, wait.
Was This Post Helpful? 1
  • +
  • -

#5 Snaef98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-July 17

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 14 July 2017 - 05:19 AM

Could you help explain how I would go about doing that? I'm very much a novice and could use instruction.

I am already using GameState using enum. for each of my windows: Splash, MainMenu, Character Creation etc. is it something similar to that?
Was This Post Helpful? 0
  • +
  • -

#6 eazyyzae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-February 13

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 14 July 2017 - 05:35 AM

I made the same mistake recently as well.

There is already a game loop running. The while loop needs to be changed to something else. Most likely an if statement. Also you don't have any thing to handle key presses or anything. Look online for Microsoft.xna.framework.input there are tutorials out there that can explain it much much more clearer than me (as I also are a bit noob :)/> ).

Or if you don't mind you can use the console to wait for a key press I'm sure. But that wouldn't be the right way to do it.

Anyway. here is my input manager class that might help you. You can use this one I got it from an youtube tutorial anyway.

using Microsoft.Xna.Framework.Input;

namespace YourNameSpaceHere
{
    public class InputManager
    {
        KeyboardState prevKeyState, keyState;

        public KeyboardState PrevKeyState
        {
            get { return prevKeyState;  }
            set { prevKeyState = value; }
        }

        public KeyboardState KeyState
        {
            get { return keyState; }
            set { keyState = value; }
        }

        public void Update()
        {
            prevKeyState = keyState;
            keyState = Keyboard.GetState();
        }

        public bool KeyPressed(Keys key)
        {
            if (keyState.IsKeyDown(key) && prevKeyState.IsKeyUp(key))
                return true;
            return false;
        }

        public bool KeyPressed(params Keys[] keys)
        {
            foreach(Keys key in keys)
            {
                if (keyState.IsKeyDown(key) && prevKeyState.IsKeyUp(key))
                    return true;
            }
            return false;
        }

        public bool KeyReleased(Keys key)
        {
            if (keyState.IsKeyUp(key) && prevKeyState.IsKeyDown(key))
                return true;
            return false;
        }

        public bool KeyReleased(params Keys[] keys)
        {
            foreach (Keys key in keys)
            {
                if (keyState.IsKeyUp(key) && prevKeyState.IsKeyDown(key))
                    return true;
            }
            return false;
        }

        public bool KeyDown(Keys key)
        {
            if (keyState.IsKeyDown(key))
                return true;
            return false;
        }

        public bool KeyDown(params Keys[] keys)
        {
            foreach (Keys key in keys)
            {
                if (keyState.IsKeyDown(key))
                    return true;
            }
            return false;
        }

    }
}



I forgot to mention. The battle might even look more seamless if you use a timer instead of having to press a key. Just a thought
Was This Post Helpful? 0
  • +
  • -

#7 Snaef98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-July 17

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 14 July 2017 - 05:46 AM

I already tried breaking it down into if statements without a loop and that broke it. Also I am using input like this
if ((currentKeyboardState.IsKeyUp(Keys.H)) && (oldKeyboardState.IsKeyDown(Keys.H)))
{
gameState = GameState.Help;
} 

Was This Post Helpful? 0
  • +
  • -

#8 eazyyzae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-February 13

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 14 July 2017 - 05:57 AM

Do you have an input for IsKeyPressed you could try instead.
Was This Post Helpful? 0
  • +
  • -

#9 Snaef98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-July 17

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 14 July 2017 - 06:10 AM

Am I totally thinking about this wrong? Because im using a tile based system its a text game with minimal graphics would it be better to just have each coordinate its own gamestate for drawing purposes? Im trying to coordinate stuff by Map# PosX PosY

The issue that I was having was that everything calculated fine. It drew the text from the combat only to immediately be overwritten by local window text. Maybe the combat needs to be its own screen rather then trying to draw it on the same screen?
Was This Post Helpful? 0
  • +
  • -

#10 eazyyzae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-February 13

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 14 July 2017 - 06:27 AM

I have to go out right now so I wont be able to answer until I get back but, quickly before I go.

The problem is the while loop within the game loop itself. I assume the rest of the game is having to run in the background while the battle is happening. The while loop would likely crash the game if it was waiting on a key press. What you need is the game to keep looping and only take action when the key pressed. So that's why if statements might be more appropriate. I don't think you should change the design plan at all. There should be a way to make it work.

Try if NOT key down then run the that bit of code (!currentKeyboardState.IsKeyDown(Keys.H)

As I said I'm kinda new at this so might not work

After the key IS down ofcourse
Was This Post Helpful? 0
  • +
  • -

#11 Snaef98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-July 17

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 14 July 2017 - 07:03 AM

I'm trying something new and it may be promising. Going to add GameState.CombatWait GameState.CombatPlayerRound GameState.CombatMonsterRound and alternate between windows, each allowing for a keypress after 1 combat cycle. then use variables to control looping of those windows. once the monster or player are defeated they end up back at the wait screen or I can make a separate one for on death/win at which point they exit back to normal game screen or gameover screen.

So -> normal game -> GameState.CombatWait -> (Determine who goes first/display message) Press Enter -> Player Round - > Wait Screen - > Monster Screen -> Wait etc etc -> Wait -> Game or Gameover.
Was This Post Helpful? 0
  • +
  • -

#12 Snaef98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-July 17

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 14 July 2017 - 07:28 AM

omg omg omg omg it worked!
Was This Post Helpful? 0
  • +
  • -

#13 Snaef98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-July 17

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 14 July 2017 - 07:57 AM

is there a method I can use to pass this code over and over since drawing all them different states requires duplicate code.
 // Base GUI & Icons
                spriteBatch.Begin();
                spriteBatch.Draw(gamescreengui, new Vector2(0, 0), Color.White);
                spriteBatch.Draw(iconhealthpotion, new Vector2(10, 170), Color.White);
                spriteBatch.Draw(iconmanapotion, new Vector2(115, 170), Color.White);
                spriteBatch.DrawString(textFont, "x" + PlayerHealthPot.ToString(), new Vector2(48, 200), Color.White);
                spriteBatch.DrawString(textFont, "x" + PlayerManaPot.ToString(), new Vector2(153, 200), Color.White);
                spriteBatch.Draw(iconattack, new Vector2(220, 182), Color.White);
                spriteBatch.DrawString(textFont, PlayerAtk.ToString(), new Vector2(260, 200), Color.White);
                spriteBatch.Draw(icondefense, new Vector2(325, 183), Color.White);
                spriteBatch.DrawString(textFont, PlayerDef.ToString(), new Vector2(365, 200), Color.White);
                spriteBatch.Draw(iconequipment, new Vector2(10, 235), Color.White);
                spriteBatch.DrawString(textFont, "[E] Equipment", new Vector2(60, 242), Color.White);
                spriteBatch.Draw(iconquestjournal, new Vector2(210, 235), Color.White);
                spriteBatch.DrawString(textFont, "[Q] Quests", new Vector2(260, 242), Color.White);
                spriteBatch.Draw(iconmap, new Vector2(10, 285), Color.White);
                spriteBatch.DrawString(textFont, "[M] Map", new Vector2(60, 292), Color.White);
                spriteBatch.Draw(iconmenu, new Vector2(210, 285), Color.White);
                spriteBatch.DrawString(textFont, "[Space] Menu", new Vector2(260, 292), Color.White);
                //spriteBatch.DrawString(textFont, "Current Location: ", new Vector2(10, 340), Color.White);
                //spriteBatch.DrawString(textFont, CurrentLocation.ToString(), new Vector2(200, 340), Color.Red);

                // Character Stats Row 1
                spriteBatch.DrawString(textFont, PlayerLevel.ToString(), new Vector2(154, 31), Color.White);
                spriteBatch.DrawString(textFont, PlayerHP.ToString(), new Vector2(126, 57), Color.White);
                //spriteBatch.DrawString(textFont, "/" + PlayerMaxHP.ToString(), new Vector2(146, 57), Color.White);
                spriteBatch.DrawString(textFont, PlayerMP.ToString(), new Vector2(134, 83), Color.White);
                //spriteBatch.DrawString(textFont, "/" + PlayerMaxMP.ToString(), new Vector2(144, 83), Color.White);
                spriteBatch.DrawString(textFont, PlayerGold.ToString(), new Vector2(150, 111), Color.White);
                spriteBatch.DrawString(textFont, PlayerExp.ToString(), new Vector2(134, 138), Color.White);
                // Character Stats Row 2
                spriteBatch.DrawString(textFont, PlayerStr.ToString(), new Vector2(356, 6), Color.White);
                spriteBatch.DrawString(textFont, PlayerDex.ToString(), new Vector2(358, 31), Color.White);
                spriteBatch.DrawString(textFont, PlayerInt.ToString(), new Vector2(353, 58), Color.White);
                spriteBatch.DrawString(textFont, PlayerMnd.ToString(), new Vector2(375, 85), Color.White);
                spriteBatch.DrawString(textFont, PlayerVit.ToString(), new Vector2(353, 113), Color.White);
                spriteBatch.DrawString(textFont, PlayerAcc.ToString(), new Vector2(381, 140), Color.White);
                spriteBatch.End();

                if (PlayerClass == 1)
                {
                    spriteBatch.Begin();
                    spriteBatch.Draw(iconwarrior, new Vector2(8, 8), Color.White);
                    spriteBatch.DrawString(textFont, PlayerName.ToString(), new Vector2(152, 6), Color.White);
                    spriteBatch.End();
                }
                else if (PlayerClass == 2)
                {
                    spriteBatch.Begin();
                    spriteBatch.Draw(iconwizard, new Vector2(8, 8), Color.White);
                    spriteBatch.DrawString(textFont, PlayerName.ToString(), new Vector2(152, 6), Color.White);
                    spriteBatch.End();
                }
                else if (PlayerClass == 3)
                {
                    spriteBatch.Begin();
                    spriteBatch.Draw(iconhealer, new Vector2(8, 8), Color.White);
                    spriteBatch.DrawString(textFont, PlayerName.ToString(), new Vector2(152, 6), Color.White);
                    spriteBatch.End();
                }

Was This Post Helpful? 0
  • +
  • -

#14 Snaef98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-July 17

Re: XNA 4.0 Help with while loop - Need to be able pause each combat round

Posted 14 July 2017 - 08:23 AM

Accually nevermind figured this one out on my own ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1