컴퓨터와 맞서 싸우는 간단한 게임을 만들고 있습니다. 하지만 문제가 발생했습니다. 싸움에서 나는 루프가 플레이어가 공격하기를 기다린 다음 계속하기를 원합니다. 그러나 나는 그것을 어떻게하는지 모른다. 도움이된다면변수가 변경 될 때까지 루프 일시 중지
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;
}
코드가 끝없이 반복적으로 반복되는 경우 변수를 변경하기 위해 코드의 다른 부분에서 어떻게 될까요? – Plutonix
그게 전부입니다. 나는 그것을 바른 길로 만드는 방법을 모른다. 그래서 끝없이 반복하지 않는다. – Dipminister
[Threads] (http://msdn.microsoft.com/en-in/library/aa645740 (v = vs.71) .aspx)를 사용하면 문제를 일으킬 수 있습니다. –