下載/瀏覽Download
Download
Report
Transcript 下載/瀏覽Download
計算機程式與實習
題目:米奇、米妮 數字PK
南台科技大學
機械工程系
奈米一甲
學號:49914011
姓名:陳品蓉
老師:謝慶存
使用說明
打開檔案之後按下
之後即可開始遊戲
米奇請按
即可出現數字
米妮請按
即可出現數字
接下來看比數,數字大者獲勝,小者則輸。
按下
按鍵即可退出遊戲。
輸出介面
程式碼設計
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
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)
Next
AddHandler btn(j).Click, AddressOf btnEvent
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
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("米奇贏了!!!")
Return
End If
If a < b Then
Buttonfail()
MsgBox("米妮贏了!!! ")
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
參考文獻
1.Visual Basic 2008實力養成暨評量解題秘笈 桂思強 編
碁峰 2010
2.Visual Basic 2008教學範本古頤榛 編著 碁峰資訊2009
3.學會寫程式Visual Basic 2008( Idea 3小組 碁峰 2010
4.Visual Basic 2008 程式設計作者: 葉倍宏 全華2008
5.Visual Basic程式設計余忠潔 全華2008