下載/瀏覽

Download Report

Transcript 下載/瀏覽

計算機程式及實習
比大小
奈米一甲
學號:49914032
姓名:張博翔
老師:謝慶存
遊戲說明
•
•
•
•
•
打開檔案之後按下
之後即可開始遊戲
1號玩家請按
即可出現數字
2號玩家請按
即可出現數字
接下來看比數,數字大者獲勝。
按下
按鍵即可退出遊戲。
遊戲開啟畫面
程式碼
•
•
Public Class Form1
Dim a, b As Integer
•
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim btn(2) As Button
btn(1) = Button1 : btn(2) = Button2
•
•
•
•
•
•
•
•
•
If sender.Equals(Button1) Then
Randomize()
a = Int(Rnd() * 99 + 1)
TextBox1.Text = "你的數字昰" & vbCrLf & vbCrLf & a
TextBox1.Font = New Font("新細明體", 10, FontStyle.Bold)
End If
End Sub
• Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
•
Dim btn(2) As Button
•
btn(1) = Button1 : btn(2) = Button2
•
•
•
•
•
If sender.Equals(Button2) Then
Randomize()
b = Int(Rnd() * 99 + 1)
TextBox2.Text = "你的數字昰" & vbCrLf & vbCrLf & b
TextBox2.Font = New Font("新細明體", 10, FontStyle.Bold)
•
End If
•
End Sub
• Private Sub btnstart_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnstart.Click
•
Dim btn(2) As Button
•
btn(1) = Button1 : btn(2) = Button2
•
Dim txt(2) As TextBox
•
txt(1) = TextBox1 : txt(2) = TextBox2
•
btnstart.Text = "再玩一次"
•
For i = 1 To btn.GetUpperBound(0)
•
btn(i).Enabled = True
•
For j = 1 To txt.GetUpperBound(0)
•
txt(j).Text = ""
•
Next
•
Next
•
End Sub
• Private Sub Form1_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
•
Dim btn(2) As Button
•
btn(1) = Button1 : btn(2) = Button2
•
For i = 1 To btn.GetUpperBound(0)
•
btn(i).Enabled = False
•
btn(i).Tag = i
•
•
•
Next
For j = 2 To btn.GetUpperBound(0)
AddHandler btn(j).Click, AddressOf btnEvent
•
Next
•
End Sub
• Sub Buttonfail()
•
Dim btn(2) As Button
•
btn(1) = Button1 : btn(2) = Button2
•
For i As Integer = 1 To btn.GetUpperBound(0)
•
btn(i).Enabled = False
•
Next
•
End Sub
•
Private Sub btnend_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnend.Click
•
MsgBox("你確定要結束嗎?")
•
Console.ReadLine()
•
End
•
End Sub
• Private Sub btnEvent(ByVal sender As System.Object, ByVal e
As System.EventArgs)
•
•
Dim btnHit As Button
btnHit = CType(sender, Button)
•
If a > b Then
•
•
•
•
Buttonfail()
MsgBox("1號玩家贏了!!!")
Return
End If
•
•
•
•
•
•
•
•
•
If a < b Then
Buttonfail()
MsgBox("2號玩家贏了!!! ")
Return
End If
If a = b Then
MsgBox("平手!!!平手!!! ")
End If
End Sub
• Private Sub Label1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Label1.Click
•
End Sub
• End Class
心得
想不到我們平常玩的遊戲就是這麼完成的
,平常只是無聊的時候碰一下,根本不會想到這
些製作過程,這份作業讓我了解了許多,原來
遊戲的背後大有學問!!