『目標』做出一個隨機骰子,其結果將決定Player1、Player2前進步數,順序輪流且至設定終點時跳出勝利宣告。

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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
int ssum1 = 0;
int ssum2 = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int d1, d2,dsum;
Random ran = new Random();
d1 = ran.Next(1, 7);
d2 = ran.Next(1, 7);
label1.Text = Convert.ToString(d1);
label2.Text = Convert.ToString(d2);
dsum = d1 + d2;
for (int i = 1; i <= d1 + d2; i++)
{
button2.Left = i+ssum1;
Thread.Sleep(100); //Delay 1秒
Application.DoEvents();
}
ssum1 = ssum1 + dsum;
textBox1.Text = Convert.ToString(ssum1);
if (button2.Left >= 30)
MessageBox.Show("甲贏");

}
private void button4_Click(object sender, EventArgs e)
{
int d1, d2, dsum;
Random ran = new Random();
d1 = ran.Next(1, 7);
d2 = ran.Next(1, 7);
label1.Text = Convert.ToString(d1);
label2.Text = Convert.ToString(d2);
dsum = d1 + d2;
for (int i = 1; i <= d1 + d2; i++)
{
button3.Left = i + ssum2;
Thread.Sleep(100); //Delay 1秒
Application.DoEvents();
}
ssum2 = ssum2 + dsum;
textBox1.Text = Convert.ToString(ssum2);
if (button3.Left >= 30)
MessageBox.Show("乙贏");
}
}
}

wadj223436 發表在 痞客邦 留言(0) 人氣()

『目標』做出一個隨機骰子,其結果將決定Player1、Player2前進步數,順序輪流且至設定終點時跳出勝利宣告。

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,r;
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 = 1; i <= d1+d2; i++)
{

//Console.WriteLine(i);
Thread.Sleep(1000); //Delay 1秒
button1.Left = i;
Application.DoEvents();

}

if (button1.Left>=15)
MessageBox.Show("甲贏");
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
int d1, d2;
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 = 1; i <= d1 + d2; i++)
{
//Console.WriteLine(i);
Thread.Sleep(1000); //Delay 1秒
button3.Left = i;
Application.DoEvents();
}
if (button3.Left >= 15)
MessageBox.Show("乙贏");
}
}
}

wadj223436 發表在 痞客邦 留言(0) 人氣()

『目標』假設一button為車子,藉由按鍵觸發使其左右移動

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;
int d = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
button1.Left = 100*c;
if(button1.Left>460)
{
d = -1;
}
if (button1.Left < 0)
{
d = 1;
}
c = c + d;
}
}
}

wadj223436 發表在 痞客邦 留言(0) 人氣()

『目標』建立紅綠燈燈號轉換功能,藉由按鍵觸發開關

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, r;
public Form1()
{
InitializeComponent();
c = 0;
}
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(0, 0);
button2.BackColor = Color.Black;
}
if (r == 1)
{
button1.Location = new Point(0, 0);
button2.BackColor = Color.Black;
}
else
{
button1.Location = new Point(100, 0);
button2.BackColor = Color.Yellow;
}
}
private void button3_Click(object sender, EventArgs e)
{
c = c + 1;
r = c % 3;
label1.Text = "r=" + r;
if (r == 0)
{
button1.Location = new Point(0, 0);
button3.BackColor = Color.Black;
}
if (r == 1)
{
button1.Location = new Point(0, 0);
button3.BackColor = Color.Red;
}
else
{
button1.Location = new Point(100, 0);
button3.BackColor = Color.Black;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
c = c + 1;
r = c % 3;
label1.Text = "r=" + r;
if (r >= 1)
{
button3.BackColor = Color.Black;
button4.BackColor = Color.Yellow;
button5.BackColor = Color.Black;
if (r == 2)
{
button3.BackColor = Color.Red;
button4.BackColor = Color.Black;
button5.BackColor = Color.Black;
}
}
else
{
button3.BackColor = Color.Black;
button4.BackColor = Color.Black;
button5.BackColor = Color.Green;
}
}
}
}

wadj223436 發表在 痞客邦 留言(0) 人氣()

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
class test
{


public static void main(String[] args)
{
JFrame jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(600, 200);
JPanel jplPanel = new JPanel();
int i;
int j=1;
int num1 , num2;
JButton jbnButton[] = new JButton[16];
JButton changeButton[] = new JButton[3];
jplPanel.setLayout (new GridLayout(4, 4));
for(i = 0;i<=15;i++){
Random ran = new Random();
jbnButton[i]=new JButton("Button"+(ran.nextInt(16)+1));
jplPanel.add(jbnButton[i]);
String s = Integer.toString(i+1);
jbnButton[i].addActionListener(new ActionListener() {

wadj223436 發表在 痞客邦 留言(0) 人氣()

«123