我是c#的超级新手,我有一个问题,我一直想弄清楚过去几天我想要做一个二十一点游戏,但我无法弄清楚我的生活如何将图片框中显示的随机卡片的图像与变量/整数相关联,这样我就可以创建一个函数来添加卡片的值,如果你能给我一些想法的话那会很棒!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace pokerapp
{
public partial class pokergame : Form
{
//Card Varibles
int card1 = 10;
int card2 = 10;
int card3 = 10;
int card4 = 10;
int card5 = 10;
int card6 = 10;
int card7 = 10;
int card8 = 10;
int card9 = 10;
int card10 = 10;
int card11 = 10;
int card12 = 10;
int card13 = 10;
int card14 = 10;
int card15 = 10;
int card16 = 10;
int card17 = 10;
int card18 = 10;
int card19 = 10;
int card20 = 10;
int card21 = 9;
int card22 = 9;
int card23 = 9;
int card24 = 9;
int card25 = 8;
int card26 = 8;
int card27 = 8;
int card28 = 8;
int card29 = 7;
int card30 = 7;
int card31 = 7;
int card32 = 7;
int card33 = 6;
int card34 = 6;
int card35 = 6;
int card36 = 6;
int card37 = 5;
int card38 = 5;
int card39 = 5;
int card40 = 5;
int card41 = 4;
int card42 = 4;
int card43 = 4;
int card44 = 4;
int card45 = 3;
int card46 = 3;
int card47 = 3;
int card48 = 3;
int card49 = 2;
int card50 = 2;
int card51 = 2;
int card52 = 2;
public pokergame()
{
InitializeComponent();
}
Random random = new Random();
List<Image> images = new List<Image>();
public void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = images[random.Next(0, images.Count - 1)];
pictureBox2.Image = images[random.Next(0, images.Count - 1)];
pictureBox14.Image = images[random.Next(0, images.Count - 1)];
pictureBox13.Image = images[random.Next(0, images.Count - 1)];
button2.Enabled = true;
button1.Enabled = false;
}
public void pokergame_Load(object sender, EventArgs e)
{
pictureBox3.Visible = false;
pictureBox4.Visible = false;
pictureBox5.Visible = false;
pictureBox6.Visible = false;
pictureBox7.Visible = false;
pictureBox8.Visible = false;
pictureBox9.Visible = false;
pictureBox10.Visible = false;
pictureBox11.Visible = false;
pictureBox12.Visible = false;
//label1.Text = Form1.textBox1.text;
button1.Enabled = true;
button2.Enabled = false;
images.Add(pokerapp.Properties.Resources._1);
images.Add(pokerapp.Properties.Resources._2);
images.Add(pokerapp.Properties.Resources._3);
images.Add(pokerapp.Properties.Resources._4);
images.Add(pokerapp.Properties.Resources._5);
images.Add(pokerapp.Properties.Resources._6);
images.Add(pokerapp.Properties.Resources._7);
images.Add(pokerapp.Properties.Resources._8);
images.Add(pokerapp.Properties.Resources._9);
images.Add(pokerapp.Properties.Resources._10);
images.Add(pokerapp.Properties.Resources._11);
images.Add(pokerapp.Properties.Resources._12);
images.Add(pokerapp.Properties.Resources._13);
images.Add(pokerapp.Properties.Resources._14);
images.Add(pokerapp.Properties.Resources._15);
images.Add(pokerapp.Properties.Resources._16);
images.Add(pokerapp.Properties.Resources._17);
images.Add(pokerapp.Properties.Resources._18);
images.Add(pokerapp.Properties.Resources._19);
images.Add(pokerapp.Properties.Resources._20);
images.Add(pokerapp.Properties.Resources._21);
images.Add(pokerapp.Properties.Resources._22);
images.Add(pokerapp.Properties.Resources._23);
images.Add(pokerapp.Properties.Resources._24);
images.Add(pokerapp.Properties.Resources._25);
images.Add(pokerapp.Properties.Resources._26);
images.Add(pokerapp.Properties.Resources._27);
images.Add(pokerapp.Properties.Resources._28);
images.Add(pokerapp.Properties.Resources._29);
images.Add(pokerapp.Properties.Resources._30);
images.Add(pokerapp.Properties.Resources._31);
images.Add(pokerapp.Properties.Resources._32);
images.Add(pokerapp.Properties.Resources._33);
images.Add(pokerapp.Properties.Resources._34);
images.Add(pokerapp.Properties.Resources._35);
images.Add(pokerapp.Properties.Resources._36);
images.Add(pokerapp.Properties.Resources._37);
images.Add(pokerapp.Properties.Resources._38);
images.Add(pokerapp.Properties.Resources._39);
images.Add(pokerapp.Properties.Resources._40);
images.Add(pokerapp.Properties.Resources._41);
images.Add(pokerapp.Properties.Resources._42);
images.Add(pokerapp.Properties.Resources._43);
images.Add(pokerapp.Properties.Resources._44);
images.Add(pokerapp.Properties.Resources._45);
images.Add(pokerapp.Properties.Resources._46);
images.Add(pokerapp.Properties.Resources._47);
images.Add(pokerapp.Properties.Resources._48);
images.Add(pokerapp.Properties.Resources._49);
images.Add(pokerapp.Properties.Resources._50);
images.Add(pokerapp.Properties.Resources._51);
images.Add(pokerapp.Properties.Resources._52);
}
int hitButtonClickCount = 0;
public void button2_Click(object sender, EventArgs e)
{
Random random = new Random();
if (hitButtonClickCount == 0)
{
pictureBox3.Visible = true;
pictureBox3.Image = images[random.Next(0, images.Count - 1)];
//pictureBox3.Location = new Point(86, 130);
hitButtonClickCount++;
}
else if (hitButtonClickCount == 1)
{
pictureBox4.Visible = true;
pictureBox4.Image = images[random.Next(0, images.Count - 1)];
//pictureBox4.Location = new Point(100, 140);
hitButtonClickCount++;
}
else if (hitButtonClickCount == 2)
{
pictureBox5.Visible = true;
pictureBox5.Image = images[random.Next(0, images.Count - 1)];
// pictureBox5.Location = new Point(241, 197);
hitButtonClickCount++;
}
else if (hitButtonClickCount == 3)
{
pictureBox6.Visible = true;
pictureBox6.Image = images[random.Next(0, images.Count - 1)];
// pictureBox5.Location = new Point(241, 197);
hitButtonClickCount++;
}
else if (hitButtonClickCount == 4)
{
pictureBox7.Visible = true;
pictureBox7.Image = images[random.Next(0, images.Count - 1)];
// pictureBox5.Location = new Point(241, 197);
hitButtonClickCount++;
button2.Enabled = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
pictureBox2.Image = null;
pictureBox3.Image = null;
pictureBox4.Image = null;
pictureBox5.Image = null;
pictureBox6.Image = null;
pictureBox7.Image = null;
pictureBox8.Image = null;
pictureBox9.Image = null;
pictureBox10.Image = null;
pictureBox11.Image = null;
pictureBox12.Image = null;
pictureBox13.Image = null;
pictureBox14.Image = null;
pictureBox3.Visible = false;
pictureBox4.Visible = false;
pictureBox5.Visible = false;
pictureBox6.Visible = false;
pictureBox7.Visible = false;
pictureBox8.Visible = false;
pictureBox9.Visible = false;
pictureBox10.Visible = false;
pictureBox11.Visible = false;
pictureBox12.Visible = false;
button1.Enabled = true;
button2.Enabled = false;
hitButtonClickCount = 0;
}
public void Points()
{
if (pictureBox1.Image == images[0])
{
label3.Text = "1/10";
}
}
private void button4_Click(object sender, EventArgs e)
{
button2.Enabled = false;
button4.Enabled = false;
}
}
}
答案 0 :(得分:0)
首先,尝试对阵列和列表进行一些阅读。这将有助于缩短您的代码并使您更容易阅读。
提出你的问题。 尝试将卡片放入阵列中。然后选择一个随机数而不是随机图像。使用此随机数选择正确的相应卡片图像和卡片值。您只需要确保它们彼此正确排列。
您可以像访问数组一样访问列表的地址。 List.Items [random_int]