骰子传奇
我覺得題目難度還行,計劃用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;}} } DiceFrom1主要是界面的代碼段,包括在按鈕點擊事件中調用Arithmetic中判斷返回貼圖的方法,以及Dice返回隨機數的方法。
Arithmetic中有一個靜態方法來判斷當前是什么數值,并返回相應的貼圖
Dice則返回一個隨機數
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
和自己預估的時間差不多,主要耗費在貼圖的返回算法,和界面各個按鈕之間的關系協調上。
點擊一個按鈕
點擊兩個按鈕
點擊三個按鈕
點擊四個按鈕
點擊停止
點擊重新開始
?
轉載于:https://www.cnblogs.com/BenjaminBardon/p/7656209.html
總結
- 上一篇: java实现定时自动打卡脚本_andro
- 下一篇: 60岁首席工程师被SpaceX边缘化,主