程式設計作業
程式設計工藝大師
按一下按鈕移動,再按一下按鈕回到原來位置
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 C1
{
public partial class Form1 : Form
{
bool buttonSwitch = false;
int bt2X, bt2Y;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!buttonSwitch)
{
bt2X = button2.Location.X;
bt2Y = button2.Location.Y;
button2.Location = new Point(200, 200);
}
else
button2.Location = new Point(bt2X, bt2Y);
if(buttonSwitch)
buttonSwitch = false;
else
buttonSwitch = true;
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
===============================
紅黃綠燈輪播
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 C2
{
public partial class Form1 : Form
{
int time=0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "";
button2.Text = "";
button3.Text = "";
button1.BackColor = Color.Black;
button2.BackColor = Color.Black;
button3.BackColor = Color.Black;
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
time += 1;
if(time % 30 <= 10)
button1.BackColor = Color.Red;
else
button1.BackColor = Color.Black;
if(time % 30 <= 20 && time % 30>10)
button2.BackColor = Color.Orange;
else
button2.BackColor = Color.Black;
if (time % 30 <= 30 && time % 30>20)
button3.BackColor = Color.Green;
else
button3.BackColor = Color.Black;
}
}
}
===============================
擲骰賽車
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 C3
{
public partial class Form1 : Form
{
int score1 = 0, score2 = 0;
bool clickBoo = true;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button3.Text = "A車擲骰";
button1.Text = "A車";
button2.Text = "B車";
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
Random rand = new Random();
int carA, carB;
carA = rand.Next(1, 6);
carB = rand.Next(1, 6);
if (clickBoo)
{
button3.Text = "A車前進 "+ carA + " 格\n" + "B車擲骰";
score1 += carA;
for (int i = 0; i <= carA; i++)
button1.Left += 5;
}
if (!clickBoo)
{
button3.Text = "B車前進 " + carB + " 格\n" + "A車擲骰";
score2 += carB;
for (int i = 0; i <= carB; i++)
button2.Left += 5;
}
if (clickBoo)
clickBoo = false;
else
clickBoo = true;
textBox1.Text = "A車目前共 " + score1 + " 格";
textBox2.Text = "B車目前共 " + score2 + " 格";
if(clickBoo)
if (score1 >= 30 && score1 > score2)
MessageBox.Show("A車獲勝");
else if (score2 >=30 && score2 > score1)
MessageBox.Show("B車獲勝");
else if (score1 >= 30 && score2 >= 30 && score2 == score1)
MessageBox.Show("平手");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace C3
{
public partial class Form1 : Form
{
int score1 = 0, score2 = 0;
bool clickBoo = true;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button3.Text = "A車擲骰";
button1.Text = "A車";
button2.Text = "B車";
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
Random rand = new Random();
int carA, carB;
carA = rand.Next(1, 6);
carB = rand.Next(1, 6);
if (clickBoo)
{
button3.Text = "A車前進 "+ carA + " 格\n" + "B車擲骰";
score1 += carA;
for (int i = 0; i <= carA; i++)
button1.Left += 5;
}
if (!clickBoo)
{
button3.Text = "B車前進 " + carB + " 格\n" + "A車擲骰";
score2 += carB;
for (int i = 0; i <= carB; i++)
button2.Left += 5;
}
if (clickBoo)
clickBoo = false;
else
clickBoo = true;
textBox1.Text = "A車目前共 " + score1 + " 格";
textBox2.Text = "B車目前共 " + score2 + " 格";
if(clickBoo)
if (score1 >= 30 && score1 > score2)
MessageBox.Show("A車獲勝");
else if (score2 >=30 && score2 > score1)
MessageBox.Show("B車獲勝");
else if (score1 >= 30 && score2 >= 30 && score2 == score1)
MessageBox.Show("平手");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
===============================
圈叉遊戲
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 C4
{
public partial class Form1 : Form
{
bool clickSwitch = true;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";
button10.Text = "重玩";
}
private void clickOXfunction()
{
if ((button1.Text == "O" && button2.Text == "O" && button3.Text == "O") || (button4.Text == "O" && button5.Text == "O" && button6.Text == "O") || (button7.Text == "O" && button8.Text == "O" && button9.Text == "O") || (button1.Text == "O" && button4.Text == "O" && button7.Text == "O") || (button2.Text == "O" && button5.Text == "O" && button8.Text == "O") || (button3.Text == "O" && button6.Text == "O" && button9.Text == "O") || (button1.Text == "O" && button5.Text == "O" && button9.Text == "O") || (button3.Text == "O" && button5.Text == "O" && button7.Text == "O"))
{
MessageBox.Show("O贏");
}
else if ((button1.Text == "X" && button2.Text == "X" && button3.Text == "X") || (button4.Text == "X" && button5.Text == "X" && button6.Text == "X") || (button7.Text == "X" && button8.Text == "X" && button9.Text == "X") || (button1.Text == "X" && button4.Text == "X" && button7.Text == "X") || (button2.Text == "X" && button5.Text == "X" && button8.Text == "X") || (button3.Text == "X" && button6.Text == "X" && button9.Text == "X") || (button1.Text == "X" && button5.Text == "X" && button9.Text == "X") || (button3.Text == "X" && button5.Text == "X" && button7.Text == "X"))
MessageBox.Show("X贏");
else
if (button1.Enabled == false && button2.Enabled == false && button3.Enabled == false && button4.Enabled == false && button5.Enabled == false && button6.Enabled == false && button7.Enabled == false && button8.Enabled == false && button9.Enabled == false)
MessageBox.Show("平手");
}
private void button1_Click(object sender, EventArgs e)
{
if (clickSwitch)
button1.Text = "O";
else
button1.Text = "X";
button1.Enabled = false;
clickOXfunction();
if (clickSwitch)
clickSwitch = false;
else
clickSwitch = true;
}
private void button2_Click(object sender, EventArgs e)
{
if (clickSwitch)
button2.Text = "O";
else
button2.Text = "X";
button2.Enabled = false;
clickOXfunction();
if (clickSwitch)
clickSwitch = false;
else
clickSwitch = true;
}
private void button3_Click(object sender, EventArgs e)
{
if (clickSwitch)
button3.Text = "O";
else
button3.Text = "X";
button3.Enabled = false;
clickOXfunction();
if (clickSwitch)
clickSwitch = false;
else
clickSwitch = true;
}
private void button4_Click(object sender, EventArgs e)
{
if (clickSwitch)
button4.Text = "O";
else
button4.Text = "X";
button4.Enabled = false;
clickOXfunction();
if (clickSwitch)
clickSwitch = false;
else
clickSwitch = true;
}
private void button5_Click(object sender, EventArgs e)
{
if (clickSwitch)
button5.Text = "O";
else
button5.Text = "X";
button5.Enabled = false;
clickOXfunction();
if (clickSwitch)
clickSwitch = false;
else
clickSwitch = true;
}
private void button6_Click(object sender, EventArgs e)
{
if (clickSwitch)
button6.Text = "O";
else
button6.Text = "X";
button6.Enabled = false;
clickOXfunction();
if (clickSwitch)
clickSwitch = false;
else
clickSwitch = true;
}
private void button7_Click(object sender, EventArgs e)
{
if (clickSwitch)
button7.Text = "O";
else
button7.Text = "X";
button7.Enabled = false;
clickOXfunction();
if (clickSwitch)
clickSwitch = false;
else
clickSwitch = true;
}
private void button8_Click(object sender, EventArgs e)
{
if (clickSwitch)
button8.Text = "O";
else
button8.Text = "X";
button8.Enabled = false;
clickOXfunction();
if (clickSwitch)
clickSwitch = false;
else
clickSwitch = true;
}
private void button9_Click(object sender, EventArgs e)
{
if (clickSwitch)
button9.Text = "O";
else
button9.Text = "X";
button9.Enabled = false;
clickOXfunction();
if (clickSwitch)
clickSwitch = false;
else
clickSwitch = true;
}
private void button10_Click(object sender, EventArgs e)
{
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
clickSwitch = true;
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";
}
}
}
===============================
動態生成九九乘法表
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 C6
{
public partial class Form1 : Form
{
Button[,] button1 = new Button[10, 10];
bool clickSwitch = true;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
button1[i, j] = new Button();
button1[i, j].Text = i + " X " + j;
button1[i, j].Location = new System.Drawing.Point(50 * i, 50 * j);
button1[i, j].Size = new System.Drawing.Size(50, 50);
this.Controls.Add(button1[i, j]);
}
}
}
}
}










