2014/10/24

10/24第五週 補充10/17之賽車比賽

10/24第五週  補充10/17之賽車比賽

首先,利用工具箱建立以下物件
2個label (作為點數用)、一個textbox(作為累積總步數)、4個button(1、4為骰子,2、3為車)

然後,先在宣告欄的位置輸入using System.Threading;

接者,在 public partial class Form1 : Form裏頭輸入 int total1 = 0, total2 = 0;

再來,進入button1的程式裡輸入骰子的宣告、假設條件、迴圈以及輸贏的判斷

同理,button4也仿照button1的程式碼,即完成賽車遊戲

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
    {
        int total1 = 0, total2 = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int d1, d2, sum;
            Random ran = new Random();
            d1 = ran.Next(1, 7);
            d2 = ran.Next(1, 7);
            label1.Text = Convert.ToString(d1);
            label2.Text = Convert.ToString(d2);
            sum = d1 + d2;
            for (int i = 1; i <= sum; i++)
            {
                button2.Left = (i + total1) * 5;
                Thread.Sleep(100);
                Application.DoEvents();
            }
            total1 = total1 + sum;
            textBox1.Text = Convert.ToString(total1);

            if (button2.Left >=100)
                MessageBox.Show("甲車贏");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int d1, d2, sum;
            Random ran = new Random();
            d1 = ran.Next(1, 7);
            d2 = ran.Next(1, 7);
            label1.Text = Convert.ToString(d1);
            label2.Text = Convert.ToString(d2);
            sum = d1 + d2;
            for (int i = 1; i <= sum; i++)
            {
                button3.Left = (i + total2) * 5;
                Thread.Sleep(100);
                Application.DoEvents();
            }
            total2 = total2 + sum;
            textBox1.Text = Convert.ToString(total2);

            if (button3.Left >= 100)
                MessageBox.Show("乙車贏");
        }
    }
}





沒有留言:

張貼留言