码迷,mamicode.com
首页 > 其他好文 > 详细

骰子传奇

时间:2017-10-12 15:35:58      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:blog   task   ace   als   splay   visible   for   exit   分享   

我觉得题目难度还行,计划用40分钟做出来

想通过可视化界面去展现骰子随机的结果和更良好的交互体验

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

技术分享
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;

namespace DiceGanme
{
    public partial class Form1 : Form
    {

        int dice = 0, dice2 = 0;

        PictureBox[] picturebox = new PictureBox[4];
       
        Dice ran = new Dice();


        public Form1()
        {
            InitializeComponent();

            picturebox[0] = pictureBox2;
            picturebox[1] = pictureBox3;
            picturebox[2] = pictureBox4;
            picturebox[3] = pictureBox5;

        }

        private void 一个ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox2.Visible = true;
            pictureBox3.Visible = false;
            pictureBox4.Visible = false;
            pictureBox5.Visible = false;

            dice = 1;
        }

        private void 两个ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox2.Visible = true;
            pictureBox3.Visible = true;
            pictureBox4.Visible = false;
            pictureBox5.Visible = false;

            dice = 2;
        }

        private void 三个ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox2.Visible = true;
            pictureBox3.Visible = true;
            pictureBox4.Visible = true;
            pictureBox5.Visible = false;

            dice = 3;
        }

        private void 四个ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox2.Visible = true;
            pictureBox3.Visible = true;
            pictureBox4.Visible = true;
            pictureBox5.Visible = true;

            dice = 4;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (dice2 = dice; dice2 > 0; dice2--)
            {
                picturebox[dice2 - 1].Image = Image.FromFile("C:\\Users\\Benjamin\\documents\\visual studio 2017\\Projects\\DiceGanme\\DiceGanme\\Resources\\timg1.gif");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (dice2 = dice; dice2 > 0; dice2--)
            {
                picturebox[dice2-1].Image = Image.FromFile(Arithmetic.DiceJudge(ran.DiceCreat()));
            }
        }
    }

}
Form1
技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiceGanme
{
    class Arithmetic
    {
        public static string DiceJudge(int dice)
        {
            string filename=null;
            switch (dice)
            {
                case 1: filename = "C:\\Users\\Benjamin\\documents\\visual studio 2017\\Projects\\DiceGanme\\DiceGanme\\Resources\\1.png";break;
                case 2: filename = "C:\\Users\\Benjamin\\documents\\visual studio 2017\\Projects\\DiceGanme\\DiceGanme\\Resources\\2.png"; break;
                case 3: filename = "C:\\Users\\Benjamin\\documents\\visual studio 2017\\Projects\\DiceGanme\\DiceGanme\\Resources\\3.png"; break;
                case 4: filename = "C:\\Users\\Benjamin\\documents\\visual studio 2017\\Projects\\DiceGanme\\DiceGanme\\Resources\\4.png"; break;
                case 5: filename = "C:\\Users\\Benjamin\\documents\\visual studio 2017\\Projects\\DiceGanme\\DiceGanme\\Resources\\5.png"; break;
                case 6: filename = "C:\\Users\\Benjamin\\documents\\visual studio 2017\\Projects\\DiceGanme\\DiceGanme\\Resources\\6.png"; break;
            }
            return filename;
        }
    }
}
Arithmetic
技术分享
using System;
using System.Collections.Generic;
using Sysusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiceGanme
{
    class Dice
    {
        private Random dice = new Random();

        public int DiceCreat()
        {
            int x = dice.Next(1, 6);
            return x;
        }
    }
}tem.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiceGanme
{
    class Dice
    {
        private Random dice = new Random();

        public int DiceCreat()
        {
            int x = dice.Next(1, 6);
            return x;
        }
    }
}
Dice

From1主要是界面的代码段,包括在按钮点击事件中调用Arithmetic中判断返回贴图的方法,以及Dice返回随机数的方法。

Arithmetic中有一个静态方法来判断当前是什么数值,并返回相应的贴图

Dice则返回一个随机数

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

和自己预估的时间差不多,主要耗费在贴图的返回算法,和界面各个按钮之间的关系协调上。

技术分享

点击一个按钮

技术分享

点击两个按钮

技术分享

点击三个按钮

技术分享

点击四个按钮

技术分享

点击停止

技术分享

点击重新开始

技术分享

 

骰子传奇

标签:blog   task   ace   als   splay   visible   for   exit   分享   

原文地址:http://www.cnblogs.com/BenjaminBardon/p/7656209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!