using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int sum1 = 0;
int sum2 = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) // button1_Click當成骰骰子
{
int d1, d2,dsum;
Random ran = new Random();
d1 = ran.Next(1, 7);
d2 = ran.Next(1, 7); //骰子隨機變數為1~6
label1.Text = Convert.ToString(d1);
label2.Text = Convert.ToString(d2); //顯示骰子骰到多少
dsum = d1 + d2; //兩顆骰子合計要走幾步
for (int i = 1; i <= dsum; i++)
{
button2.Left = sum1+i; //讓button2移動sum1+i
Thread.Sleep(100); //Delay 0.1秒 移動速度
Application.DoEvents(); //timer的控制項
}
sum1 = sum1 + dsum;
textBox1.Text = Convert.ToString(sum1);
if (button2.Left >= 100) //設100為終點
{ MessageBox.Show("button2 win"); } //到終點則win
}
private void button4_Click(object sender, EventArgs e)
{ //判斷方法同上
int d1, d2, dsum;
Random ran = new Random();
d1 = ran.Next(1, 7);
d2 = ran.Next(1, 7);
label1.Text = Convert.ToString(d1);
label2.Text = Convert.ToString(d2);
dsum = d1 + d2;
for (int i = 1; i <= dsum; i++)
{
button3.Left = sum2 + i;
Thread.Sleep(100); //Delay 0.1秒
Application.DoEvents();
}
sum2 = sum2 + dsum;
textBox1.Text = Convert.ToString(sum2);
if (button3.Left >= 100)
{ MessageBox.Show("button3 win"); }
}
}
}
沒有留言:
張貼留言