暂停循环,直到变量发生变化

本文关键字:变化 变量 循环 暂停 | 更新日期: 2023-09-27 18:20:53

我正在制作一个简单的游戏,让你与电脑对抗。但是我遇到了一个问题。在一场比赛中,我想要一个循环,等待球员进攻,然后继续。但我不知道该怎么做。这是战斗的主要方法:

private void Battle()
    {
        // player info screen
        richTextBox1.Text = name + "'r'n" + "Health: " + hp + "'r'n" + "Arrows: " + arrows;
        // Enemy info screen
        richTextBox2.Text = monName + "'r'n" + "Health: " + monHp;
        // Output
        richTextBox3.Text = "The battle begins!"; // richTextBox3 is a log, where you can see everything that has happened.

        while (winner == 2)
        {
            if (monHp < 1)
            {
                winner = 1;
            }
            else if (hp < 1)
            {
                winner = 0;
            }
            if (turn == 1) // Player turn
            {
                // player info screen
                richTextBox1.Text = name + "'r'n" + "Health: " + hp + "'r'n" + "Arrows: " + arrows;
                // Enemy info screen
                richTextBox2.Text = monName + "'r'n" + "Health: " + monHp;
                busy.WaitOne();
                playerCanAtk(true);// Enables the player to attack. Basically a method that enables and disables the attack buttons.
                while (playerHasAtk == false) // Waits for the user to attack
                {
                  // Something that makes the loop wait, until the user has attacked
                }

            }
            else if (turn == 0 && playerHasAtk == true) // Enemy turn
            {
                // player info screen
                richTextBox1.Text = name + "'r'n" + "Health: " + hp + "'r'n" + "Arrows: " + arrows;
                // Enemy info screen
                richTextBox2.Text = monName + "'r'n" + "Health: " + monHp;
                //playerCanAtk(false); // Disables the player attack
                int monDmg = Fight.attack(monMinDmg, monMaxDmg);
                hp = hp - monDmg;
                richTextBox3.AppendText("'r'n" + monName + " attacks " + name + " and does " + monDmg + " damage!");
                turn = 1;
                playerHasAtk = false;
            }
        }

        if (winner == 1)
        {
            richTextBox3.Text = "Congratulations! You won!";
        }
        else if (winner == 0)
        {
            richTextBox3.Text = "You lost! Better luck next time.";
        }
    }

如果有帮助的话,这是我的其余代码:

// Player info
    string name = "Player";
    int hp = 100;
    int arrows = 3;
    //Enemy info
    string monName = "Enemy";
    int monHp = 60;
    int monMinDmg = 6;
    int monMaxDmg = 15;
    //Other properties
    int turn = 1; // 1 = Player, 0 = enemy.
    int winner = 2; // 2 = No winner (yet), 1 = Player won, 0 = Enemy won.
    bool playerHasAtk = false;
    ManualResetEvent busy = new ManualResetEvent(false);



    private void button5_Click(object sender, EventArgs e) // Start the battle by pressing this button
    {
        Battle();
    }
    private void button3_Click(object sender, EventArgs e) // Attacking with the sword
    {
        int dmg = Fight.attack(8, 19);
        monHp = monHp - dmg;
        richTextBox3.AppendText("'r'n" + name + " attacks " + monName + " with a sword and does " + dmg + " damage!");
        playerHasAtk = true;
        turn = 0;
    }

暂停循环,直到变量发生变化

GUI程序不同于控制台程序:它是基于事件的。你不是等待某件事发生,而是对事件做出反应。

也就是说,不要把所有的东西都塞进一个函数中,而是决定单击按钮时应该发生什么等等,并将相应的代码放入事件处理程序中。