7 Replies - 363 Views - Last Post: 28 April 2020 - 03:10 PM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 867
  • Joined: 15-December 14

Fight scene in my text style C# game

Posted 27 April 2020 - 10:13 AM

Thought I would go back to basics and since I really enjoy C# over C++ I thought I would get more practice making a C# remake of the python game gothans from planet percel 25. I've made the changes previously suggested and I ran into two new problems. My goal in the program was to have the main menu run the entire game, if possible, and to make it as simple as possible.

I'm stuck on the fight scene in the game, similar to a RPG game the player(s) and their opponents have to roll initiative to see who goes first. I created two methods in the code, one of them is if the Gothan wins initiative and the other if the player wins initiative to handle the fight scene. That is to say, if the Gothan wins the code will go to a method where the enemy attacks first and vice versa. My question is I wanted to have the ability for the player to be able to reload their weapon and use a "heal potion" (Still havent figured out what to call it) during the bout. Does anyone have any ideas what I could do?

This is the program so far...
using System;
using System.Threading;
namespace Gothans_from_Planet_Percal_25
{
    class Program
    {
        public void Heal()//this will heal the player
        {
            Random number = new Random();//player attack
            double heal = number.Next(0, 10);
            Console.WriteLine("Your character healed ", heal, " damage");
        }
        public class Player
        {
            Random init = new Random();
            Random attack = new Random();
            Random encounters = new Random();//encounters
            int health = 10;
            string name = ("");
            string[] intro = {"The Gothons of Planet Percal #25 have invaded your ship and killed",
                "your entire crew.  You are the last surviving member and your last",
                "mission is to get the neutron destruct bomb from the Weapons Armoury,",
                "put it in the bridge, and blow the ship up after getting into an ",
                "escape pod.\n"};
            string[] quips = {"You died. You kinda suck at this.",
                "Your mom would be proud...if she were smarter.",
                "Such a luser.","I have a small puppy that's better at this." };
            public void Intro()
            {
                for (int i = 0; i < intro.Length; i++)
                {
                    Console.WriteLine(intro[i]);
                }
            }
            public string GetName()
            {
                Console.WriteLine("What is your name?");
                name = Console.ReadLine();
                Console.WriteLine("Your players name is {0}\n", name);

                return name;
            }
            public void PlayerHealth()
            {//This will display how much health the player has and can be accessed at anytime
                
                if (health == 10)
                {
                    Console.WriteLine("{0} you're at full health ", name);
                    Console.WriteLine("You have {0} health",health);
                }
                else if (health < 9)
                    Console.WriteLine("You have {0} health\n", health);
                else
                    Console.WriteLine("You're dead\n");
            }
            public void Initiative()
            {//This will handle the initiavite to see who goes first in a fight
                Console.WriteLine("Initiative");
                int Playerroll;
                int Gothanroll;
                Playerroll = init.Next(1, 10);
                Gothanroll = init.Next(1, 10);
                if (Playerroll > Gothanroll)
                {
                    Console.WriteLine("{0} goes first", name);
                    PlayerAttack();
                }    
                else
                {
                    Console.WriteLine("Gothan goes first");

                }
                    
            }
            
            public void Encounters()//random show Gothons
            {
                int enc;
                enc = encounters.Next(1, 10);
                if (enc % 2 == 0)
                {
                    Console.WriteLine("You encounter a Gothan");
                    Initiative();
                }
                else
                    Console.WriteLine("You didn't encounter a Gothan");
            }
            public double HealPlayer()
            {
                Random health = new Random();
                double healdmg = health.Next(5);
                Console.WriteLine("{0} healed {1} hit points",name,healdmg, " health");

                return healdmg;
            }
            public void Bridge()
            {
                Encounters();
                Console.WriteLine("Bridge");
            }
            public void GothanAttack()
            {
                int at;
                at = attack.Next(1, 20);
                //Gothan has to roll above a 13 to hit otherwise it hits the players armour
                Console.WriteLine("Gothan has to roll over 13 to hit {0}",name);
                Console.Write("The Gothan rolled a ");
                if (at > 12)
                {
                    Console.WriteLine(at);
                    Console.WriteLine("The Gothan hit {0} ",name);
                }
                else
                {
                    Console.WriteLine(at);
                    Console.WriteLine("Gothan missed {0} ", name);
                }
            }
            public void PlayerAttack()
            {
                int at;
                at = attack.Next(1, 20);
                //playerattack has to roll above a 13 to hit otherwise it hits the enemies armour
                Console.WriteLine("{0} has to roll over 13 to hit", name);
                Console.Write("{0} rolled a ",name);
                if (at > 12)
                {
                    Console.WriteLine(at);
                    Console.WriteLine("You hit the enemy");
                }
                else
                {
                    Console.WriteLine(at);
                    Console.WriteLine("{0} missed", name);
                }
            }
        }
        static void Main(string[] args)
        {
            Player player = new Player();
            player.Intro();
            player.GetName();
            int pick;
            string[] choice = { " Bridge", " Engine room", " Escape pod", " Armoury",
                " Heal damage"," Check health"," PlayerAttack"};
            while(true)
            {
                for(int x = 0; x < choice.Length; x++)
                {
                    Console.Write(x+1);
                    Console.WriteLine(choice[x]);
                }
                Console.WriteLine("\nChoose an option\n");
                pick = Convert.ToInt32(Console.ReadLine());
                if (pick == 1)
                {
                    player.Bridge();
                }
                else if (pick == 5)
                {
                    player.HealPlayer();
                }
                else if (pick ==6)
                {
                    player.PlayerHealth();
                }
                else if (pick == 7)
                    player.PlayerAttack();
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Fight scene in my text style C# game

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6901
  • View blog
  • Posts: 28,559
  • Joined: 12-December 12

Re: Fight scene in my text style C# game

Posted 27 April 2020 - 11:03 AM

Note that you should not be creating multiple instances of Random. don't use underscores either. (Have these points not been mentioned already?)

Did you complete a book or sequence of tutorials for C# as raised previously? I get the strong impression that you are jumping between projects, and not putting in the leg-work, in study or preparation, before jumping into coding projects.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15743
  • View blog
  • Posts: 63,065
  • Joined: 12-June 08

Re: Fight scene in my text style C# game

Posted 27 April 2020 - 12:03 PM

Oh there are so many ways to skin cats, and let's walk through one I threw together as I have the time now to dive a little deeper into interesting problems.

When doing something along these lines it's best to start with a plan up front. I tend to lean on how game engines work which would divide up things into what the 'world' does and what the actors in the world do.

I also tend to lean into abstraction to keep things tidy.

So.. an agent in the world, well my world - maybe your world but for this example my world - has a few things I would like it to keep track of.

Health, attack damage, and a name. I would like to have a way to print those things out, and a method to track stats like 'is this object alive'.

My agent has only one primary method 'do action'. I would like that to be randomized (in this instance) set of actions I'll chuck in an enum.

Since 'random' is best to have one variable I'll have the random variable passed in, from the world, at constructor time.

This is a throw away example which means proper public/private/protected gets chucked out the window for the most part.

   abstract class AGENT
    {
        public enum ACTION
        {
            NOTHING = 0,
            ATTACK = 1,
            HEAL = 2
        }

        public int HEALTH { get; set; }
        public int ATTACK_DAMAGE { get; set; }
        public string NAME { get; set; }

        protected Random _r;

        //most likely won't be used, but I like having an empty constructor around.
        public AGENT()
        {
            HEALTH = 1;
            NAME = "";
            ATTACK_DAMAGE = 1;
            
            //in case of no random.
            _r = new Random((DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond)*DateTime.Now.Day);
        }
        public AGENT(Random r, int health, int attack_damage, string name = "")
        {
            HEALTH = health;
            NAME = name;
            ATTACK_DAMAGE = attack_damage;
            _r = r;
        }
        public abstract ACTION DoAction();
        public abstract string GetInfo();

        public bool IsAlive()
        {
            return (HEALTH > 0);
        }
    }


Super duper, and on to the player. The player takes in a few things for constructors, fleshes out the 'do action', and prints out the info.

Go-go-gadget abstract class!

 class PLAYER : AGENT
    {
        public PLAYER() : base()
        {
        }
        public PLAYER(Random r, int health, int attack_damage, string name = "myPLAYER") : base(r,health, attack_damage, name)
        {
            
        }
        public override ACTION DoAction()
        {
            int foo = _r.Next(3);

            return (ACTION)foo;
        }

        public override string GetInfo()
        {
            return $"\tNAME: {NAME}\n" +
                $"\tHEALTH: {HEALTH}\n" +
                $"\tATTACK_DAMAGE: {ATTACK_DAMAGE}\n";
        }
    }



Now my enemy is a little different because I can. I want to add a 'monster type' in there. This could be handled with giving each monster their own class (and probably should be), but a shallow example like this it's just fun having a slightly off the path constructor.

Notice how the monster type is extra in the constructor and the print out.

       public enum MONSTER_TYPE
        {
            RAT = 0,
            SLIME = 1,
            GOBLIN = 2
        }

        public MONSTER_TYPE monsterType { get; set; }
        public ENEMY() : base()
        {
            monsterType = 0;
        }
        public ENEMY(Random r, int health, int attack_damage, MONSTER_TYPE monster_type, string name = "myENEMY") : base(r,health, attack_damage, name)
        {
            monsterType = monster_type;
        }

        public override ACTION DoAction()
        {
            int foo = _r.Next(3);

            return (ACTION)foo;
        }

        public override string GetInfo()
        {
            return $"\tNAME: {NAME}\n" +
                $"\tMONSTER_TYPE: {monsterType}\n" +
                $"\tHEALTH: {HEALTH}\n" +
                $"\tATTACK_DAMAGE: {ATTACK_DAMAGE}\n";
        }
    }


Gangster!

The game world, aka the 'main', has a few jobs. Primary is to setup the actors, create the random, and house the 'game loop' (aka the while loop).

I could easily have the objects in a list container, which would imply initiative order if I cared to roll for that, and even have 'enemy' objects go into the player and monster constructor to simulate their targets... but I'll avoid that and duplicate code for fun.

The 'game loop' turns over, asks the player for their action, attempts to facilitate that, and then asks the monster for their action.

If the agent's action is to attack the "world" gets the player's attack value, and removes that from the monster's health.

If the agent wants to heal then add a standard value to it's health.

This concept simplifies many functions, and puts the onus on the objects themselves. In the 'player' class it could easily "roll some dice" to get a 'to hit' option, and left the world to determine if it hits compared to the target's THAC0.. or 'reload' could be in there and the the 'world' resets some ammunition parameter in the actor.

It's all about that sweet sweet division of labor.

     static void Main(string[] args)
        {
            Random r = new Random();

            PLAYER _pc = new PLAYER(r, 10, 3);
            //Thread.Sleep(50);
            ENEMY _monster = new ENEMY(r, 3, 1, ENEMY.MONSTER_TYPE.RAT);

            int round = 1;

            //game world.
            while (true)
            {
                Console.WriteLine($"Round: {round}\n-----------------\n");

                // player goes first in this example
                Console.WriteLine($"{_pc.GetInfo()}"); //info
                switch (_pc.DoAction())//game engine asks the agent for their action, and then tries to facilitate that action
                {
                    case AGENT.ACTION.NOTHING:
                        Console.WriteLine($"{_pc.NAME} does NOTHING \n");
                        break;
                    case AGENT.ACTION.ATTACK:
                        Console.WriteLine($"{_pc.NAME} does ATTACK \n");
                        _monster.HEALTH -= (_pc.ATTACK_DAMAGE);
                        break;
                    case AGENT.ACTION.HEAL:
                        Console.WriteLine($"{_pc.NAME} does HEAL \n");
                        _pc.HEALTH += 5;
                        break;
                    default:
                        Console.WriteLine($"shit broke");
                        break;
                }

                if(!_monster.IsAlive())
                {

                    Console.WriteLine($"!! monster dead !!");
                    break;  
                }

                Console.WriteLine($"{_monster.GetInfo()}"); //info
                switch (_monster.DoAction())//game engine asks the agent for their action, and then tries to facilitate that action
                {
                    case AGENT.ACTION.NOTHING:
                        Console.WriteLine($"{_monster.NAME} does NOTHING \n");
                        break;
                    case AGENT.ACTION.ATTACK:
                        Console.WriteLine($"{_monster.NAME} does ATTACK \n");
                        _pc.HEALTH -= (_monster.ATTACK_DAMAGE);
                        break;
                    case AGENT.ACTION.HEAL:
                        Console.WriteLine($"{_monster.NAME} does HEAL \n");
                        _monster.HEALTH += 5;
                        break;
                    default:
                        Console.WriteLine($"shit broke");
                        break;
                }


                if(!_pc.IsAlive())
                {
                    Console.WriteLine($"!! player dead !!");
                    break;  
                }
                round += 1;

                if (round == 10)
                {

                    Console.WriteLine($"Round limit exceeded");
                    break;
                }
            }

            Console.WriteLine($"~~Good bye~~");

            Console.ReadLine();
        }



Examples of runs:


Round: 1
-----------------

        NAME: myPLAYER
        HEALTH: 10
        ATTACK_DAMAGE: 3

myPLAYER does NOTHING

        NAME: myENEMY
        MONSTER_TYPE: RAT
        HEALTH: 3
        ATTACK_DAMAGE: 1

myENEMY does ATTACK

Round: 2
-----------------

        NAME: myPLAYER
        HEALTH: 9
        ATTACK_DAMAGE: 3

myPLAYER does NOTHING

        NAME: myENEMY
        MONSTER_TYPE: RAT
        HEALTH: 3
        ATTACK_DAMAGE: 1

myENEMY does ATTACK

Round: 3
-----------------

        NAME: myPLAYER
        HEALTH: 8
        ATTACK_DAMAGE: 3

myPLAYER does ATTACK

!! monster dead !!
~~Good bye~~









Round: 1
-----------------

        NAME: myPLAYER
        HEALTH: 10
        ATTACK_DAMAGE: 3

myPLAYER does ATTACK

!! monster dead !!
~~Good bye~~



You can use this to see about a cleaner way of doing it all, or try and incorporate what's up in your stuff.
Was This Post Helpful? 1
  • +
  • -

#4 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 867
  • Joined: 15-December 14

Re: Fight scene in my text style C# game

Posted 28 April 2020 - 07:14 AM

andrewsw
I had a dumb duh moment, I went through your post and realised below your comment you had links to help me. I thought you meant buy a C# book in that post. So I went to a tutorial on youtube which showed a cursory explanation of C# and as I watched it I took notes and saw how close it resembled C++. I will go through the tutorials you suggested and then go back to the text style game.


I actually have a new question for you, is C# similar to C++ that theres a C# 14, C# 17?


modi123_1

Thank you for your example it helped tremendously.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7404
  • View blog
  • Posts: 24,977
  • Joined: 05-May 12

Re: Fight scene in my text style C# game

Posted 28 April 2020 - 09:08 AM

C# is up to C# version 8.0.
Was This Post Helpful? 0
  • +
  • -

#6 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 867
  • Joined: 15-December 14

Re: Fight scene in my text style C# game

Posted 28 April 2020 - 12:30 PM

Is there a book that you guys could recommend that I could use to study C#? The one you guys(Skydiver and Jim) recommended for me in C++ was amazing and I got to chapter 5. Although I understand from that video the similarities of C++ and C# there are a lot of things I know I have to do to write a program in C# but I don't know why. If you guys could recommend a cheap book, I was laid off from my job due to covid and I am trying to stay busy while looking for a job.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7404
  • View blog
  • Posts: 24,977
  • Joined: 05-May 12

Re: Fight scene in my text style C# game

Posted 28 April 2020 - 02:39 PM

None that I can personally recommend since I'm self-taught for C# while C# was still in development.

I do know that from some the past questions that have been asked in this forum, I would not recommend the C# book by Joyce Farrell. Part of the reason is because it feels like she just translated her Java version of the book over to C# without taking into account any of the stylistic feel and approaches more common in C#. Or put another way, it's like a programming book written for C, being republished for C++. Even if the author took the time to not just use C++ as "C-with-classes" there are still things that done the C way and done the C++ way. (ex. one never calls exit() in a C++ program even though the compiler will let you, but it is common practice to call exit() in C.) And so, it was similar for her book. There are somethings that she teaches which are the Java way of doing things rather than the C# way. And content wise, the few times I've skimmed pages of the book to try to help someone with their question here, it felt like the material was taught in a fragmented manner. But recall that I was just skimming and have not read the book cover to cover. It's quite possible there is rhyme and reason to the way the material is presented.

I've heard third hand that some adult learners have had success with Head First C#. On the other hand, I've also heard others complain that the Head First books are good enough to get you a job interview, but don't teach you any good programming practices that will serve you long term. But as I said, this is only what I've heard third hand. I don't have any personal experience with the book.
Was This Post Helpful? 0
  • +
  • -

#8 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 867
  • Joined: 15-December 14

Re: Fight scene in my text style C# game

Posted 28 April 2020 - 03:10 PM

I guess I'll have to try trial and error to find a good resource to learn and avoid the book you mentioned.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1