简体   繁体   English

C#与对象作战

[英]C# combat with objects

To familiarize myself with the basics of programming I'm attempting to make a short, simple RPG. 为了使自己熟悉编程的基础知识,我尝试制作一个简短的RPG。 I'm probably not going to be doing anything the most efficient way, nor am I looking to. 我可能不会以最有效的方式做任何事情,也没有期待。 I am, however, looking to make everything functional. 但是,我希望使所有功能正常运行。

For this game, from my understanding I should use classes for players and enemies and create objects for each one to make it easier on myself. 就本游戏而言,据我了解,我应该为玩家和敌人使用类,并为每个类创建对象,以使自己更轻松。 However, this made things harder when it came to combat. 但是,这使得战斗变得更加困难。 In my playerAttack function, I figured that since the player doing the attacking isn't always going to be the same player, I would need to use that specific player (in this case, player1 ) since the stats would be different. 在我的playerAttack函数中,我发现由于进行攻击的玩家不一定总是同一个玩家,因此我需要使用该特定玩家(在本例中为player1 ),因为统计数据会有所不同。 The same goes for enemies. 敌人也是如此。 You can see my logic in the line I commented out. 您可以在我注释掉的行中看到我的逻辑。

Now the error I'm getting there is that player1 and badguy1 don't exist in the current context, which I assume is because they aren't initialized within that function itself. 现在我得到的错误是在当前上下文中不存在player1badguy1 ,我认为这是因为它们未在该函数本身中初始化。 But I'm pretty sure I don't WANT them to be initialized there. 但是我很确定我不想在这里初始化它们。

So I suppose I have two questions : 所以我想我有两个问题:

  1. how do I fix my current error? 我该如何解决当前的错误?

  2. how am I supposed to do combat when the combatants will be changing all the time? 当战斗人员一直在变化时,我应该如何战斗? Should I not be using objects? 我不应该使用对象吗?

Thanks for any help. 谢谢你的帮助。

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sim
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Player player1 = new Player();
            player1.currentHP = 30;
            player1.maxHP = 30;
            player1.atp = 10;
            player1.dfp = 5;


            Enemy badguy1 = new Enemy();
            badguy1.currentHP = 30;
            badguy1.maxHP = 30;
            badguy1.atp = 8;
            badguy1.dfp = 4;

        }
        public void playerAttack()
        {
            int enemyDamage;
           // enemyDamage = player1.atp - badguy1.dfp; 
        }
     public class Player
    {
       public int currentHP;
       public int maxHP;
       public int atp;
       public int dfp;

    }
    public class Enemy
    {
        public int currentHP;
        public int maxHP;
        public int atp;
        public int dfp;

    }
}

You can make player and enemy parameters of playerAttack : 您可以设置playerAttack playerenemy参数:

public void playerAttack(Player player, Enemy enemy)
{
    int enemyDamage = player.atp - enemy.dfp;
    ...
}

then when you call playerAttack you can pass whatever appropriate to it. 那么当您致电playerAttack您可以通过任何适合的方式。 For example: 例如:

Player player1 = new Player();
player1.currentHP = 30;
player1.maxHP = 30;
player1.atp = 10;
player1.dfp = 5;

Enemy badguy1 = new Enemy();
badguy1.currentHP = 30;
badguy1.maxHP = 30;
badguy1.atp = 8;
badguy1.dfp = 4;

playerAttack(player1, badguy1);

Initialize them globally 全局初始化

public partial class Form1 : Form
{

    Player player1 = new Player();
    player1.currentHP = 30;
    player1.maxHP = 30;
    player1.atp = 10;
    player1.dfp = 5;


    Enemy badguy1 = new Enemy();
    badguy1.currentHP = 30;
    badguy1.maxHP = 30;
    badguy1.atp = 8;
    badguy1.dfp = 4;

    public Form1()
    {
        InitializeComponent();


    }
    public void playerAttack()
    {
        int enemyDamage;
        enemyDamage = player1.atp - badguy1.dfp; 
    }
 public class Player
{
   public int currentHP;
   public int maxHP;
   public int atp;
   public int dfp;

}
public class Enemy
{
    public int currentHP;
    public int maxHP;
    public int atp;
    public int dfp;

}
}

This way, you can actually call them on anywhere within the class. 这样,您实际上可以在班级中的任何地方调用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM