2014年12月2日 星期二

丟骰子2台車比賽

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 WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
        int s1sum = 0;
        int s2sum = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //button1=User1,button2=Car1
            int dsum, d1, d2;
            //d1=骰子1,d2=骰子2,dsum=兩個骰子總合
            Random iran = new Random();
            d1 = iran.Next(1, 7);
            //隨機變數1~6
            d2 = iran.Next(1, 7);
            //隨機變數1~6
            label1.Text = Convert.ToString(d1);
            //骰子1點數
            label2.Text = Convert.ToString(d2);
            //骰子2點數
            dsum = d1 + d2;
            for (int i = 1; i <= dsum; i++)
            {
                button2.Left = (s1sum + i) * 5;
                Thread.Sleep(100);
                Application.DoEvents();
            }
            s1sum = s1sum + dsum;
            textBox1.Text = Convert.ToString(dsum);
            textBox2.Text = Convert.ToString(s1sum);
            //textBox2表示Cra1總共走了多少步
            if (button2.Left >= 350)
            {
                MessageBox.Show("User1 Win.");
                //當Car1到達終點時顯示User1 Win.
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //button4=User2,button3=Car2
            int dsum, d1, d2;
            //d1=骰子1,d2=骰子2,dsum=兩個骰子總合
            Random iran = new Random();
            d1 = iran.Next(1, 7);
            //隨機變數1~6
            d2 = iran.Next(1, 7);
            //隨機變數1~6
            label1.Text = Convert.ToString(d1);
            //骰子1點數
            label2.Text = Convert.ToString(d2);
            //骰子2點數
            dsum = d1 + d2;
            for (int i = 1; i <= dsum; i++)
            {
                button3.Left = (s2sum + i) * 5;
                Thread.Sleep(100);
                Application.DoEvents();
            }
            s2sum = s2sum + dsum;
            textBox1.Text = Convert.ToString(dsum);
            textBox2.Text = Convert.ToString(s2sum);
            //textBox2表示Cra2總共走了多少步
            if (button3.Left >= 350)
            {
                MessageBox.Show("User2 Win.");
                //當Car2到達終點時顯示User2 Win.
            }
        }
    }
}

C# 跑馬燈

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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        int c;
        int d = 1;
        public Form1()
        {
            InitializeComponent();
        }

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

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

VB 點一下跑一下

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年12月1日 星期一

C# 紅綠燈+音樂插入

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 a, b;
        public Form1()
        {
            InitializeComponent();
            a = 0;
            axWindowsMediaPlayer1.settings.setMode("loop", true);
            //執行自動撥音樂

        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = "bn" + b;
            a = a + 1;
            b = a % 3;
            //找b的餘數
            if (b == 0)
            {
                button1.Location = new Point(100, 20);
                button3.BackColor = Color.Red;              
                button4.BackColor = Color.Black;
                button2.BackColor = Color.Black;
                //button 變顏色
            }
            else if (b == 1)
            {
                button1.Location = new Point(13, 20);
                button3.BackColor = Color.Black;
                button4.BackColor = Color.Yellow;
                button2.BackColor = Color.Black;
            }
            else
            {
                button1.Location = new Point(100, 20);
                button3.BackColor = Color.Black;
                button4.BackColor = Color.Black;
                button2.BackColor = Color.Green;
            }

        }

        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.URL = "music.mp3";
            //MP3的路徑和名稱
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }

    }
}

Unity 移動方塊

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
transform.Translate (0, 0, Time.deltaTime);
}
}

C# 移動和彈出視窗

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(100, 100);
            //按button2把button1移動到(100,100)
        }
    }

2014年11月13日 星期四

C# 自動產生2*2按鈕+99乘法表

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 a;
        Button[,] buttons = new Button[10, 10];
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 1; i < 10; i++)
            {
                for (int j = 1; j < 10; j++)
                {
                    buttons[i, j] = new Button();
                    this.Controls.Add(buttons[i,j]);
                    buttons[i, j].Size = new Size(50, 50);
                    a = i * j;
                    buttons[i, j].Text = Convert.ToString(a);                              
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                 
                }
            }
        }
    }
}