Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将PictureBox链接到类中的对象_C#_Visual Studio - Fatal编程技术网

C# 将PictureBox链接到类中的对象

C# 将PictureBox链接到类中的对象,c#,visual-studio,C#,Visual Studio,我正在Windows窗体应用程序中制作一个游戏。我目前有一个精灵,我可以通过使用箭头键成功地在屏幕上移动。我想能够添加更多的精灵以后,也可以在屏幕上移动的箭头键。 我目前有一个Player类,它拥有对象“H”的所有属性。我当前移动的PictureBox名为Placeholder 1。如果可能的话,我希望能够将播放器对象分配给PictureBox,或者类似的东西 public class Player { // class members public string name;

我正在Windows窗体应用程序中制作一个游戏。我目前有一个精灵,我可以通过使用箭头键成功地在屏幕上移动。我想能够添加更多的精灵以后,也可以在屏幕上移动的箭头键。 我目前有一个Player类,它拥有对象“H”的所有属性。我当前移动的PictureBox名为Placeholder 1。如果可能的话,我希望能够将播放器对象分配给PictureBox,或者类似的东西

public class Player
{
    // class members 

    public string name;
    public int HP;
    public int currentHP;
    public int attack;
    public int defense;
    public int critical;

    public void Character(string name, int HP, int currentHP, int attack, int defense, int critical)
    {
        this.name = name;
        this.HP = HP;
        this.currentHP = currentHP;
        this.attack = attack;
        this.defense = defense;
        this.critical = critical;
    }

}
这是我目前的球员职业

public partial class Gameplay : Form
{
    public void Player()
    {       
        Player[] H = new Player[1];

        H[0] = new Player();
        H[1] = new Player();

        H[0].Character("Faye", 24, 24, 8, 5, 2);
        H[1].Character("Robin", 20, 20, 7, 6, 4);
    }

    public Gameplay()
    {
        InitializeComponent();

        KeyDown += new KeyEventHandler(Gameplay_KeyDown);
    }

    public int movesLeft = 6;

    // movement of character
    public void Gameplay_KeyDown(object sender, KeyEventArgs e)
    {
        if (movesLeft > 0)
        {
            int x = placeholder1.Location.X;
            int y = placeholder1.Location.Y;


            if (e.KeyCode == Keys.Right) x += 64;
            else if (e.KeyCode == Keys.Left) x -= 64;
            else if (e.KeyCode == Keys.Down) y += 64;
            else if (e.KeyCode == Keys.Up) y -= 64;

            placeholder1.Location = new Point(x, y);

            movesLeft--;
        }

        // Temporary developer option to skip to end screen
        if (e.KeyCode == Keys.Space)
        {
            EndGame endGame = new EndGame();
            endGame.Show();
            endGame.Location = Location;
            this.Hide();
        }
    }
}

这是我目前在游戏中使用的代码。我已经为玩家角色创建了一个数组,以及我为移动当前单数PictureBox而编写的代码。

我将使用graphic生成它。直接在表单上绘制图像,然后以这种方式移动它,而不是将其链接到图片框中。

您可以创建一个新表单来显示玩家角色

我做了一个代码示例,可以满足您的要求

表格1:

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

        public PictureBox Pic
        {
            get
            {
                return pictureBox1;
            }
            set
            {
                pictureBox1 = value;
            }

        }


        public int movesLeft = 10;
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.F1)
            {
                Form2 form = new Form2();
                form.ShowDialog();
            }
            if (movesLeft > 0)
            {
                int x = pictureBox1.Location.X;
                int y = pictureBox1.Location.Y;


                if (e.KeyCode == Keys.Right) x += 64;
                else if (e.KeyCode == Keys.Left) x -= 64;
                else if (e.KeyCode == Keys.Down) y += 64;
                else if (e.KeyCode == Keys.Up) y -= 64;

                pictureBox1.Location = new Point(x, y);

                movesLeft--;
            }

        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            Form2 form = new Form2();
            form.ShowDialog();
        }
    }
    public class Player
    {
        public string name;
        public int HP;
        public int currentHP;
        public int attack;
        public int defense;
        public int critical;

        public void Character(string name, int HP, int currentHP, int attack, int defense, int critical)
        {
            this.name = name;
            this.HP = HP;
            this.currentHP = currentHP;
            this.attack = attack;
            this.defense = defense;
            this.critical = critical;
        }

    }
表格2:

 public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        List<Player> list = new List<Player>();
        private void Form2_Load(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.Items.Add(@"F:\pic\1.jpg");
            comboBox1.Items.Add(@"F:\pic\2.jpg");
            list.Add(new Player { name = "1.jpg", attack = 1, critical = 2, currentHP = 3, defense = 4, HP = 10 });
            list.Add(new Player { name = "2.jpg", attack = 3, critical = 5, currentHP = 7, defense = 2, HP = 10 });
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = Path.GetFileName(comboBox1.SelectedItem.ToString());
            Player player = list.Where(p => p.name == name).First();
            txtname.Text = player.name;
            txtattack.Text = player.attack.ToString();
            txtcritical.Text = player.critical.ToString();
            txtcurrenthp.Text = player.currentHP.ToString();
            txtdefense.Text = player.defense.ToString();
            txthp.Text = player.HP.ToString();
            Form1 form1 = (Form1)Application.OpenForms["Form1"];
            form1.Pic.Image = Image.FromFile(comboBox1.SelectedItem.ToString());

        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Form1 form1 = (Form1)Application.OpenForms["Form1"];
            form1.Focus();
        }
    }
公共部分类表单2:表单
{
公共表格2()
{
初始化组件();
}
列表=新列表();
私有void Form2_加载(对象发送方、事件参数e)
{
comboBox1.DropDownStyle=ComboBoxStyle.DropDownList;
comboBox1.Items.Add(@“F:\pic\1.jpg”);
comboBox1.Items.Add(@“F:\pic\2.jpg”);
添加(新玩家{name=“1.jpg”,攻击=1,临界=2,当前生命=3,防御=4,生命=10});
添加(新玩家{name=“2.jpg”,攻击=3,临界=5,当前生命=7,防御=2,生命=10});
}
私有无效组合框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
字符串名称=Path.GetFileName(comboBox1.SelectedItem.ToString());
Player=list.Where(p=>p.name==name.First();
Text=player.name;
txtattack.Text=player.attack.ToString();
txtcritical.Text=player.critical.ToString();
txtcurrenthp.Text=player.currentHP.ToString();
txtdeffence.Text=player.defence.ToString();
txthp.Text=player.HP.ToString();
Form1 Form1=(Form1)Application.OpenForms[“Form1”];
form1.Pic.Image=Image.FromFile(comboBox1.SelectedItem.ToString());
}
私有void Form2\u FormClosed(对象发送方,FormClosedEventArgs e)
{
Form1 Form1=(Form1)Application.OpenForms[“Form1”];
形式1.焦点();
}
}
结果:

您可以使用Tag属性。它将接受任何对象,包括控件或列表等但你必须把它扔到你放在那里的任何东西上。如果你计划增长,也许可以创建一个类来保存各种东西,并将ot放在那里。我不理解“将PictureBox链接到一个类中的对象”。你能详细描述一下吗?或者你期望的结果是什么?我有一个图片框,它是我的精灵。我有一个Player类,它包含人类玩家的所有属性。我有可以移动图片框的代码,但是我必须指定它应该移动的图片框。我想能够有球员的统计数据出现在某处时,一个特定的图片框精灵被选中。