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

时间:2014-09-07 16:07:07

标签: c# loops

我正在制作一个简单的游戏,你可以与计算机作斗争。但我遇到了一个问题。在一场战斗中,我想要一个等待玩家攻击的循环,然后继续。但我不知道该怎么做。这是战斗的主要方法:

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;

    }

1 个答案:

答案 0 :(得分:0)

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

这意味着,不要在一个函数中塞入所有内容,而是决定单击按钮时应该发生什么,然后将相应的代码放在事件处理程序中。