为了熟悉编程的基础知识,我试图制作一个简短的RPG。我可能不会以最有效的方式做任何事情,我也不期待。但是,我希望使一切功能齐全。
对于这个游戏,根据我的理解,我应该为玩家和敌人使用类,并为每个人创建对象,以使自己更容易。然而,这使得战斗时的事情变得更加艰难。在我的playerAttack
函数中,我认为由于进行攻击的玩家并不总是同一个玩家,我需要使用该特定玩家(在这种情况下, player1 )因为统计数据会有所不同。敌人也是如此。你可以在我注释掉的那一行看到我的逻辑。
现在我遇到的错误是当前上下文中不存在 player1 和 badguy1 ,我认为这是因为它们未在内部初始化这个功能本身。但我很确定我不想在那里进行初始化。
所以我想我有两个问题:
如何修复当前错误?
当战斗员一直在变化时,我该如何进行战斗?我不应该使用物体吗?
感谢您的帮助。
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;
}
}
答案 0 :(得分:1)
您可以player
生成enemy
和playerAttack
个参数:
public void playerAttack(Player player, Enemy enemy)
{
int enemyDamage = player.atp - enemy.dfp;
...
}
然后当你致电playerAttack
时,你可以传递任何合适的内容。例如:
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);
答案 1 :(得分:0)
全局初始化
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;
}
}
这样,您实际上可以在班级的任何地方调用它们。