simple battle system help

if anyone has some spare time

Page 1 of 1

2 Replies - 1481 Views - Last Post: 02 June 2010 - 03:17 PM Rate Topic: -----

#1 theblackmage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 21-January 10

simple battle system help

Posted 02 June 2010 - 10:20 AM

so, hey y'all. I know what everyone saysabout not trying too make an RPG before your an expert with your langauge, but this is just the most basic battle system possible, and something I put together for fun.

here's my code (save a few methods that are just validation/changing the form, so I don;t want too clutter the site up with them)

this is done in C#

        public abstract class Character : Ibattler
        {
            public Character(int Atk, int Hp, int defence, string Name)
            {
                this.defence = defence;
                this.hP = Hp;
                this.attk = Atk;
                this.name = Name;
            }
            Random crazyAttack = new Random();
            public string name;
            protected int hP;
            public int Hp { get { return hP; } }
            protected int defence;
            public int Defence { get { return defence; } }
            protected int attk;
            public int Attk { get { return attk; } }

            private int CalculateDefenceBonus(Character target)
            {
                int canIBlockThis = crazyAttack.Next(66);
                int DefenceBonus = crazyAttack.Next(target.Defence / 2);
                if (canIBlockThis <= 11)
                    return 0;
                else if (canIBlockThis >= 56)
                    DefenceBonus *= 2;
                return DefenceBonus;
            }

            private int CalucltaeAttackBonus(){
            
                int goodOrBad = crazyAttack.Next(100);
                int damageModifier = crazyAttack.Next(100);
                int finalModifier = 0;
                if (goodOrBad < 50)
                    finalModifier += damageModifier;
                else
                    finalModifier -= damageModifier;
            return finalModifier;
            }

            public virtual int Attack(Character target)
            {
                    int damage = attk;
                    int defenceBonus = CalculateDefenceBonus(target);
                    int damageModifier = CalucltaeAttackBonus();
                    damage += damageModifier - defenceBonus;
                    if (damage < 1)
                        damage = 1;
                    target.hP -= damage;
                    if (target.hP < 0)
                        target.hP = 0;
                return damage;
            }
        }
        public class Enemies : Character, IEnemy
        {
            public Enemies(int Atk, int Hp, int defence, string Name, int exp): base (Atk, Hp, defence, Name)
            {
                this.exp = exp;
            }
            private int exp;
            public int Exp { get { return exp; } }
            public void GiveThemExp(Player WhoKilledMe)
            {
                WhoKilledMe.AddExp(exp);
            }
            
        }
        public class Player : Character
        {
            public Player(int Atk, int Hp, int defence, string Name)
                : base(Atk, Hp, defence, Name)
            {
                level = 1;
                SetExpToLevel();
            }
            private int exp;
            public int Exp { get { return exp; } }
            private int level;
            public int Level { get { return level; } }
            private int expToLevel;
            public int ExpToLevel { get { return expToLevel; } }

           public void AddExp(int ExpTooAdd)
            {
                exp += ExpTooAdd;
                if (exp >= expToLevel){
                    levelUp();
                }
            }

            private void levelUp()
            {
                Random statboosts = new Random();
                level = level + 1;
                attk += 50; 
                attk+= statboosts.Next(30);
                hP += 200;
                attk += statboosts.Next(100);
                defence += 30;
                defence += statboosts.Next(20);
                SetExpToLevel();
            }
            private void SetExpToLevel()
            {
                expToLevel = level * 400;
            }
        }
        public interface Ibattler
        {
            int Hp { get; }
            int Attk { get; }
            int Attack(Character target);
        }
        public interface IEnemy{
            int Exp { get; }
            void GiveThemExp(Player WhoKilledMe);
        }

        void CreateABattleScene()
        {
            gameOver = false;
            character1 = new Player(250, 1000, 60, "Player");
            enemies1 = new Enemies(80, 600, 100, "Goblin 1", 40);
            enemies2 = new Enemies(120, 500, 80, "Goblin 2", 40);
            enemies3 = new Enemies(120, 600, 100, "Goblin 3", 40);
            enemies4 = new Enemies(80, 700, 140, "Armoured Goblin", 40);
            enemies = new Enemies[] { enemies1, enemies2, enemies3, enemies4 };
            WhosStilFighting();
            UpdateForm();
        }

private void button1_Click(object sender, EventArgs e)
        {
            if (!waiting)
            {
                waiting = true;
                PlayerAttack();
                if (!gameOver)
                {
                    for (int i = 0; i < enemies.Length; i++)
                    {
                        if (!gameOver)
                            EnemyAttack(enemies[i]);
                        else
                        {
                            i = 4;
                            CreateABattleScene();
                        }
                    }
                } else {
                    CreateABattleScene() ;
                }
                waiting = false;
            }
        }

 void PlayerAttack()
        {
            Character target = enemies[comboBox1.SelectedIndex];
            if (target.Hp == 0)
            {
                target = enemies[comboBox1.SelectedIndex + 1];
            }
            whatsGoinOnBox.Clear();
            whatsGoinOnBox.Text += "\r\n" + character1.name + "Takes a whack at " + target.name + " for " +
                character1.Attack(target).ToString() + "\r\n-----------------------------";
            Application.DoEvents();
            System.Threading.Thread.Sleep(1500);
            if (target.Hp == 0)
            {
                if (target is IEnemy)
                {
                    Enemies IWantMyExp = target as Enemies;
                    IWantMyExp.GiveThemExp(character1);
                }

            }
            WhosStilFighting();
            battleOver();
            displayVictoryMessage();
        }
        void EnemyAttack(Character attacker)
        {
                if (attacker.Hp > 0)
                {
                    whatsGoinOnBox.Clear();
                    whatsGoinOnBox.Text += "\r\n" + attacker.name + "Takes a whack at " + character1.name + " for " +
            attacker.Attack(character1).ToString() + "\r\n-----------------------------";
                    battleOver();
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(3000);
                }
                UpdateForm();
            displayVictoryMessage();
        }




Now, I have a few tinny litte problems with it, that I was hoping some of you good people could help me with. Nothing really advanced, but hey :sigh:

1) when I call the attack method, with two characters with the same attk variable, against the same character, they always return the same ammount of damage. In fact, every random number is always the same, meaning it's not really random, just going down a list
I know that in VB.Net, I have too call Randomize()too avoid all my randoms being not random, so I was wondering if there was something like that in C#

2) I'm thinking of adding in a speed stat too, so the attack order isn't always character then the enemies.
My curren plan for this is a bubble sort too put the characters in order of speed, with a small random added too the speed stat so it's not always in the same order and then have the attacks run in that order.
I'm just wondering about peoples thoughs on wether that would work, or if there's a simpler solution.

3) I need more enemies too fight. So far all I have is goblins, so if anyone has any suggestion as too other fanasy critters, and what kind of stats they'd have, please feel free too suggest some (beg) :D

4) What kinds of animated image format can be used in a picture box. just wondering in case of some future expansion when I'm more confortable with C#

thanks too everyone who took the time too read this as it's longer thanI first intended.

Is This A Good Question/Topic? 0
  • +

Replies To: simple battle system help

#2 janne_panne   User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: simple battle system help

Posted 02 June 2010 - 11:14 AM

private int CalucltaeAttackBonus()



EARTHQUAKE!!

No, seriously, what happened there =D

1) When you create a New Random() object, it uses computer's milliseconds on which it bases the randomness. So if you create to New Random() objects at the same millisecond count, they will always produce the same numbers. What you should do is just have one Random object which is used everywhere. I usually have a class where I store consts or static readonly variables/objects so you could use:
public class Const 
{
  public static readonly Random RANDOM = new Random(); 
}



2) Speed is good. You could also make the speed decide how often the thing attacks. If someone has a speed of 1 and the other has 100, the other one would hit 100 times during the time the other one just hits once. Of course 1 vs 100 would be quite extreme but sometimes you could have slow creatures like golems which hit hard but slow so they could have speed of 50 when the normal speed is 100 so they would only hit once while you hit twice.

3) Hard to say about stats since we have no idea about goblin's stats. But play any fantasy game to get some basic enemy ideas: golems, skeletons, mummies, vampires, dragons, ogres etc.
edit: whoops, we can actually see the stats, missed that part in the code.
After reading a bit more of the code, I would like to point some things out which will help you to write cleaner code:
- class Enemies should be class Enemy because it is just one Enemy. If you want to store the enemies somewhere, that class should be EnemyCollection.
- You could have a class called Combat with static methods EnemyAttack and PlayerAttack, this would clean up the code a bit.
- Variables and parameters should start with non capitalized letter and all class names and methods/properties are capitalized

4) When you are more comfortable with C#, program your game in XNA instead of windows forms. WinForms isn't meant for game programming and should never be used for the actual games, only for some tools like map editors and stuff. But for moving image you could try gif in picturebox, I haven't tried it but it might work.

This post has been edited by janne_panne: 02 June 2010 - 11:22 AM

Was This Post Helpful? 1
  • +
  • -

#3 theblackmage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 21-January 10

Re: simple battle system help

Posted 02 June 2010 - 03:17 PM

Thanks for the quick reply

Quote

Variables and parameters should start with non capitalized letter and all class names and methods/properties are capitalized


heh, the book I've been working off, Head First C#, said too capitalise public things, and use lowercase for private.

I know the naming isn't too good, but this right now is just something I wrote p quickly. Especialy the calculateAttackBonus method (or what it ended up as ;))

thanks for the info about randoms
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1