10/17第四週 利用隨機擲骰做車子比賽
首先,利用工具箱建立四個button、一個Textbox、一個label
其中button1表車子1、button2表骰子1、button3表車子2、button4表骰子2
然後進入buttin2程式
一開始先在宣告位置那建入一類別 using System.Threading;
然後回到button2的程式輸入骰子的條件
並在最後輸入比大小的條件
同理進入button4的程式
依樣畫葫蘆仿照button2的程式
*記得改變數名稱
Ex:
using System;
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
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
int d1, d2, sum = 0;
Random ran = new Random();
d1 = ran.Next(1, 7);
d2 = ran.Next(1, 7);
label1.Text = Convert.ToString(d1);
textBox1.Text = Convert.ToString(d2);
for (int i = 0; i <= d1 + d2; i++)
{
sum = sum + d1 + d2;
Thread.Sleep(1000);
Application.DoEvents();
button1.Left = button1.Left + i;
}
if (button1.Left >= 80)
MessageBox.Show("car1贏");
}
private void button4_Click(object sender, EventArgs e)
{
int d3, d4, sum = 0;
Random ran = new Random();
d3 = ran.Next(1, 7);
d4 = ran.Next(1, 7);
label1.Text = Convert.ToString(d3);
textBox1.Text = Convert.ToString(d4);
for (int i = 0; i <= d3+d4; i++)
{
sum = sum + d3 + d4;
Thread.Sleep(1000);
Application.DoEvents();
button3.Left = button3.Left + i;
}
if (button3.Left >= 80)
MessageBox.Show("car2贏");
}
}
}
沒有留言:
張貼留言