2014/10/17

10/17第四週 利用隨機擲骰做車子比賽

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贏");
        }
    }
}





2014/10/3

10/3第三週 物體來回移動

10/3第三週  物體來回移動

visual C++

首先,先至工具箱建立兩個button、一個Timer

進入button2程式
先在public partial class Form1 : Form裡 輸入int c=0,s;
在到button2輸入移動的條件

然後回到表單點入Timer的程式
將剛剛的條件複製進來,即可執行

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int c, s = 1;

        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button1.Left > 430)
            {
                s = -1;
            }
            c = c + s;
            button1.Left = 50 * c;

            if (button1.Left < 0)
            {
                s = 1;
            }
            c = c + s;
            button1.Left = 50 * c;


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (button1.Left > 430)
            {
                s = -1;
            }
            c = c + s;
            button1.Left = 50 * c;

            if (button1.Left < 0)
            {
                s = 1;
            }
            c = c + s;
            button1.Left = 50 * c;

        }
    }
}







Excel VB

首先,至檢視點選錄製聚集
在表單輸入1後按Enter,並至檢視停止錄製聚集
在點選檢視的檢視聚集做編輯
左上點選插入-->自訂表單
選擇空白按鈕並新增兩個
點選buttin2後進入程式輸入條件,即可執行
Ex:
Dim c, d As Integer

Private Sub CommandButton2_Click()

If CommandButton1.Left > 150 Then
d = -1
End If
If CommandButton1.Left < 50 Then
d = 1
End If
c = c + d
CommandButton1.Left = 50 * c

End Sub





2014/9/26

9/26第二週 製作紅綠燈+圖片+音樂

9/26第二週 製作紅綠燈+圖片+音樂

紅綠燈

首先,先進入表單 在表單程式輸入 int c = 0 , r ;

再來利用工具箱建立四個button與一個label

進入button2輸入開關所需條件

時間器Timer

條件輸入完後在表單上建立一時間器Timer

進入Timer將剛才的程式複製進去

回到表單點選Timer的屬性視窗
將Enabled打開,另外,Interval一秒為1000單位

圖片

選擇button1屬性的Image功能,點選本機資源-->匯入,並選擇想要的圖片
按下確定即完成圖片設定

音樂





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;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        int c = 0, r;

        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            c = c + 1;
            r = c % 3;
            label1.Text = "r=" + r;

            if (r == 0)
            {
                button1.Location = new Point(10, 30);
                button2.BackColor = Color.Red;
                button3.BackColor = Color.Black;
                button4.BackColor = Color.Black;
            }
            else if (r == 1)
            {
                button1.Location = new Point(0, 30);
                button2.BackColor = Color.Black;
                button3.BackColor = Color.Yellow;
                button4.BackColor = Color.Black;
            }
            else
            {
                button1.Location = new Point(0, 0);
                button2.BackColor = Color.Black;
                button3.BackColor = Color.Black;
                button4.BackColor = Color.Green;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            c = c + 1;
            r = c % 3;
            label1.Text = "r=" + r;

            if (r == 0)
            {
                button1.Location = new Point(10, 30);
                button2.BackColor = Color.Red;
                button3.BackColor = Color.Black;
                button4.BackColor = Color.Black;
            }
            else if (r == 1)
            {
                button1.Location = new Point(0, 30);
                button2.BackColor = Color.Black;
                button3.BackColor = Color.Yellow;
                button4.BackColor = Color.Black;
            }
            else
            {
                button1.Location = new Point(0, 0);
                button2.BackColor = Color.Black;
                button3.BackColor = Color.Black;
                button4.BackColor = Color.Green;
            }
        }

      
    }
}







2014/9/19

9/19第一週 初次認識C# & Unity

9/19第一週 初次認識C# & Unity




利用工具箱,點選button,建立兩個按鈕
表單進入button1,輸入MessageBox.Show("你好嗎?");
執行時可跳出對話視窗

表單進入button2,輸入button1.Location = new Point(150, 150);
執行時可讓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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("你好嗎?");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.Location = new Point(150, 150);
        }
    }
}