显示与所示骰子图像相关的数值
本文关键字:图像 显示 | 更新日期: 2023-09-27 17:56:35
我对编程相当陌生,对于一个uni项目,我创建了一个简单的游戏,其中有两个付款人,每个人都有一个骰子。然后掷骰子,谁的数字最多,谁就赢了,很简单。
该程序按预期工作,但我想在模具下方添加一个额外的文本框,该文本框将显示图像的数值。
我相信 DisplayDice 方法将值分配给正确的图像,那么我是否需要以某种方式将其分配给文本框? 我的想法是否正确,我无法将 int 分配给文本框,所以我需要使用 int。解析?这是我的代码;
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
}
}
}
}
任何帮助将不胜感激,因为它也将帮助我制作我也在尝试制作的二十一点游戏。
您说得对,您不能将 int 分配给文本框,但是,.NET 中的所有对象都具有"ToString()"函数,因此您可以使用以下命令获取字符串值:
Player1ScoreTextBox.Text = player1Dice.ToString();
有关 MSDN 的详细信息
请注意,如果您的骰子计数很大,或者浮点数,或者需要以特定方式格式化的其他内容,则可以向 ToString 传递格式化字符串。例如,ToString("F2")
格式为精度为 2 位小数的固定点数,因此 2.1 变为 2.10。
其他字符串也可以在MSDN上找到
您可以将整数值的文本表示形式分配给文本框的文本,如下所示:
tbPlayer1Value.Text = diceValue.ToString();