Adie Knight

Adie Knight

  • NA
  • 3
  • 12.2k

Calling a method from a class to a from

Jan 26 2011 11:31 AM
Hi all,

I am writing a small text based fighting game using the Windows form so I can have dialog boxes pop-up when a user does something or something has happened.  I have created a class for the player and enemy to store their "stats" as variables. So on the Form1 I have a button that allows the player to "Fight", and in the class I have created a method to play out the fight sequence.  When the player clicks the "Fight" button I am trying to call the fighting sequence from class1.  I am using Visual Studio 2010 express.

(please bare in mind I am totally new to C#)

Here is the code:

Form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FightApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FightSystem();
        }
    }
}


Class1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FightApp
{
    public class Class1 : Form1 
    {
        int playerHealth = 60;
        int playerMaxHealth = 60;
        int playerCurrentHealth = 60;
        int playerGold = 0;
        int playerXP = 0;
        int playerLevel = 1;
        int playerAttack = 10;
        int playerDefence = 10;

        Random random = new Random();
        int winFight = 0;

        int enemyHealth = 60;
        int enemyCurrentHealth = 60;
        int enemyLevel = 1;
        int enemyAttack = 10;
        int enemyDefence = 10;

        public void FightSystem()
        {
            playerAttack = playerAttack + random.Next(1, 6);
            enemyAttack = enemyAttack + random.Next(1, 6);

            if (playerAttack > enemyAttack)
            {
                winFight = playerAttack - enemyAttack;
                enemyCurrentHealth = enemyCurrentHealth - winFight;

                playerGold = random.Next(1, 3);
                playerXP = 1;
            }
        }
    }
}

I understand that there maybe be an easier way to implement it but as I said I am a complete rookie :s

Thanks in advance for any help or suggestions.

Answers (3)