骰子游戏,图片列表和图片框 [英] dice game, imagelist and picturebox

查看:94
本文介绍了骰子游戏,图片列表和图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助...我不知道该怎么做:
Q1?按下滚动"按钮后,将启用一个计时器,以每1/10秒生成一次随机的模头轧制...
Q2 ?,如何使dice#1成为整数或计数为1.因为在滚动停止之后,需要将它们全部加起来并显示得分.
我正在使用5图片盒,图像列表中有6图片(从1到6切成小方块),2个按钮(滚动并再次播放).
谢谢...

help pls...i don''t know how to do this:
Q1? After the Roll button is pressed, a timer will be enabled to generate random die rolls every 1/10 of a second...
Q2?, how can make dice#1 an integer or count as 1. because after the roll stop, need to add them all and display score.
i am using 5 picturebox, the imagelist has 6 picture(dice 1 to 6), 2 buttons(roll and play again).
Thank you...

private Random m_rnd = new Random();
        private int[] i_Array = new int[5] { 0, 1, 2, 3, 4 };
        public Form1()
        {
            InitializeComponent();
            
            pictureBox1.Image = imageList1.Images[i_Array[0]];
            pictureBox2.Image = imageList1.Images[i_Array[1]];
            pictureBox3.Image = imageList1.Images[i_Array[2]];
            pictureBox4.Image = imageList1.Images[i_Array[3]];
            pictureBox5.Image = imageList1.Images[i_Array[4]];
        }

        private void btn_roll_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = imageList1.Images[m_rnd.Next(0, 6)];
            pictureBox2.Image = imageList1.Images[m_rnd.Next(0, 6)];
            pictureBox3.Image = imageList1.Images[m_rnd.Next(0, 6)];
            pictureBox4.Image = imageList1.Images[m_rnd.Next(0, 6)];
            pictureBox5.Image = imageList1.Images[m_rnd.Next(0, 6)];

        }

推荐答案

我认为您正在以错误的方式进行操作.这就是我要做的.我假设您尝试从5个骰子中获得随机结果,并将图像设置为5 pb's以对应于每个掷骰子的数字.我还假设您的imageList包含与骰子的六个面相对应的六个图像.首先,我将我的PictureBoxes存储在List< PictureBox>中. .这样,我可以从循环访问它们.我也会有一个List< int>存储骰子结果,所以我将它们声明为表单级别变量,以及一个用于存储总掷骰的int和一个Random类的实例以生成随机结果:-

I think you are going about this the wrong way. This is how I would do it. I am assuming you are trying to get random results from 5 dice, and setting the image for 5 pb''s to correspond to each number rolled. I aslo assume your imageList contains six images which correspond to the six sides of a dice. First off I would store my PictureBoxes in a List<PictureBox> . That way I could access them from a loop. I would also have a List<int> to store my dice results, so i would declare them as form level variables, along with an int to store the total rolled and an instance of the Random class to generate random results :-

Random r = new Random();
        List<int> diceResults = new List<int>();
        List<PictureBox> dice = new List<PictureBox>();
        int diceTotal;



然后我将我的PictureBoxes添加到窗体构造函数的骰子列表中(也可以在FormLoad事件中完成),如下所示:



Then I would add my PictureBoxes to my dice list in the form constructor(could also be done in FormLoad event) like this;

public Form1()
        {
            InitializeComponent();
            dice.Add(pictureBox1);
            dice.Add(pictureBox2);
            dice.Add(pictureBox3);
            dice.Add(pictureBox4);
            dice.Add(pictureBox5);
        }



然后在滚动"按钮的click事件中,您只需构建一个循环以生成随机数并按如下方式添加:-



then in the click event of the roll button, you just construct a loop to generate random numbers and add as you go like this:-

private void button1_Click(object sender, EventArgs e)
        {
            diceTotal = 0;
            diceResults.Clear();
            for (int i = 0; i <= 4; i++)
            {
                diceResults.Add(r.Next(1,6));
                int imageNumber = diceResults[i] - 1;
                dice[i].Image = this.imageList1.Images[imageNumber];
                diceTotal += diceResults[i];
            }
            lblDiceTotal.Text = diceTotal.ToString();
        }



请注意,我将1用作我的随机生成器的最小值,因此我不会得到0,这意味着要生成图像索引时必须减去1.

希望这对您有帮助



notice i used 1 as the minimum for my random generator so I don''t get 0 as a result, this means I must subtract 1 when want to generate the image index.

Hope this helps


我解决了第一个问题,现在我很难这样做:
单击标签后,背景颜色将变为红色,表示该特定模具将不会滚动.如果用户再次单击红色骰子,则颜色将变为绿色,表示该骰子将被滚动.

i solved the first question, now I''m having a hard time doing this:
When a label is clicked, the background color will be changed to red, indicating that that particular die will not be rolled. If the user clicks on a red die again, the color will return to green, indicating that that die will be rolled.

        Random m_rnd = new Random();
        List<int> diceResults = new List<int>();
        List<picturebox> die = new List<picturebox>();
        PictureBox[] dice = null;
        int i_diceTotal = 0;
        //int totalScore = 0;
        int[] i_Array = new int[5];
        int rolls = 0;
        int i_rolling = 0;
        public Form1()
        {
            InitializeComponent();
            die.Add(pictureBox1);
            die.Add(pictureBox2);
            die.Add(pictureBox3);
            die.Add(pictureBox4);
            die.Add(pictureBox5);
            dice = new PictureBox[5]
            {
                pictureBox1,
                pictureBox2,
                pictureBox3,
                pictureBox4,
                pictureBox5
            };
            for (int i = 0; i < 5; i++)
            {
                dice[i].Image = imageList1.Images[i];
                i_Array[i] = i;
            }

            lbl_totalscore.Text = "0";
            lbl_rollscore.Text = "0";
            timer1.Interval = 10;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void btn_Roll_Click(object sender, EventArgs e)
        {
            
            rolls++;
            lbl_rollscore.Text = rolls.ToString();
            if (rolls == 3)
            {
                btn_Roll.Enabled = false;
            }
            timer1.Start();
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                i_Array[i] = m_rnd.Next(0, 6);
                dice[i].Image = imageList1.Images[i_Array[i]];
            }

            i_rolling++;
            if (i_rolling < 10) return;
            timer1.Stop();
            //int rollScore = 0;
            i_diceTotal = 0;
            diceResults.Clear();
            for (int i = 0; i <= 4; i++)
            {
                diceResults.Add(m_rnd.Next(1, 6));
                int imageNumber = diceResults[i] - 1;
                die[i].Image = this.imageList1.Images[imageNumber];
                i_diceTotal += diceResults[i];
            }
            lbl_totalscore.Text = i_diceTotal.ToString();
            
        }

        private void btn_playagain_Click(object sender, EventArgs e)
        {
            rolls = -1;
            dice = new PictureBox[5]
            {
                pictureBox1,
                pictureBox2,
                pictureBox3,
                pictureBox4,
                pictureBox5
            };
            for (int i = 0; i < 5; i++)
            {
                dice[i].Image = imageList1.Images[i];
                i_Array[i] = i;
            }
            rolls++;
            btn_Roll.Enabled = true;
            lbl_totalscore.Text = "0";
            lbl_rollscore.Text = "0";
        }
</picturebox></picturebox></int></int>


这篇关于骰子游戏,图片列表和图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆