我對編程和uni項目相當陌生我創建了一個簡單的遊戲,其中有兩個付款人每個都有一個死亡。骰子然後滾動,誰擁有最高的數字勝利,簡單。顯示與顯示的骰子圖像有關的數值
該程序按預期方式工作,但我想添加一個額外的文本框,將顯示圖像的數值的死亡。
我相信DisplayDice方法將值分配給正確的圖像,所以我需要以某種方式將它分配給文本框?我是否認爲我不能將int分配給文本框,所以我需要使用int.Parse? 這是我的代碼;
public partial class Form1 : Form
{
// Attributes
// Values live for as long as the form is open
Random _rnd = new Random(); // Random number generator
string[] _diceImages; // store names of all image files
int[] _playerDice; // Dice values for each player
int _spinCount; // Count of number of animation ticks
const int NUMBER_OF_FACES = 6;
const int NUMBER_OF_PLAYERS = 2;
const int PLAYER_ONE = 0;
const int PLAYER_TWO = 1;
const int NUMBER_OF_SPINS = 5;
public Form1()
{
InitializeComponent();
_diceImages = new string[NUMBER_OF_FACES]; // _diceImages is set to the value given to NUMBER_OF_FACES
SetupDiceImages();
// One die per player, create dice
_playerDice = new int[NUMBER_OF_PLAYERS]; // _playerDice is set to the value given to NUMBER_OF_PLAYERS
ChangeDiceValue();
DisplayDice();
}
// sets the correct dice image to the relevant value in the array
// there are six faces on a die so the array needs six values, 0-5
private void SetupDiceImages()
{
_diceImages[0] = "one.gif";
_diceImages[1] = "two.gif";
_diceImages[2] = "three.gif";
_diceImages[3] = "four.gif";
_diceImages[4] = "five.gif";
_diceImages[5] = "six.gif";
}
// random number is generated to change the value displayed on the dice
private void ChangeDiceValue()
{
// Generate random values between 0 and 5
_playerDice[PLAYER_ONE] = RandomValue(NUMBER_OF_FACES - 1); //random number generated for player 1
_playerDice[PLAYER_TWO] = RandomValue(NUMBER_OF_FACES - 1); //random number generated for player 2
}
// tells the program which number to display
private void DisplayDice()
{
int diceValue;
diceValue = _playerDice[PLAYER_ONE]; // Value thrown by player one
playerDicePictureBox1.Image = Image.FromFile("../../Images/" + _diceImages[diceValue]); // tells the program where to get the image for player 1's picture box
diceValue = _playerDice[PLAYER_TWO]; // Value thrown by player two
playerDicePictureBox2.Image = Image.FromFile("../../Images/" + _diceImages[diceValue]); // tells the program where to get the image for player 1's picture box
}
// Generate random value between 0 and maxValue
// including 0 and maxValue
private int RandomValue(int maxValue)
{
return _rnd.Next(0, maxValue + 1);
}
// animation starts when the button is clicked
private void throwDiceButton_Click(object sender, EventArgs e)
{
_spinCount = 0; // Start counting again
animationTimer.Enabled = true; // Start the timer
}
private void FindWinner()
{
// Check values of dice here
int player1Dice;
int player2Dice;
player1Dice = _playerDice[PLAYER_ONE] + 1; // 0 To 5 hence + 1
player2Dice = _playerDice[PLAYER_TWO] + 1;
}
private void animationTimer_Tick(object sender, EventArgs e)
{
ChangeDiceValue();
DisplayDice();
if (_spinCount + 1 < NUMBER_OF_SPINS)
{
// Safe to implement the count
_spinCount++;
}
else
{
// Stop animation now
_spinCount = 0;
animationTimer.Enabled = false; // Stop the timer
FindWinner();
// if statement is used to find a winner
if (_playerDice[PLAYER_ONE] > _playerDice[PLAYER_TWO]) // if player one's die is greater than player two's
{
resultTxtBox.Text = ("Player 1 WINS!!!"); // this message is displayed if the above is true
}
else
{
resultTxtBox.Text = ("Player 2 WINS!!!"); // if the above is false then this message is displayed
}
if (_playerDice[PLAYER_ONE] == _playerDice[PLAYER_TWO]) // player one and two have the same number
{
resultTxtBox.Text = ("It's a DRAW!!!"); // if the above is true then this message is displayed
}
}
}
}
任何幫助,將不勝感激,因爲它也將幫助我二十一點遊戲,我也試圖讓。