下載/瀏覽Download

Download Report

Transcript 下載/瀏覽Download

計算機程式及實習
期末報告之口頭報告ppt製作
題目:踩地雷
南台科技大學
機械工程系
奈米一乙
學號:49914005
學生:謝明翰
踩地雷遊戲
踩地雷遊戲是ㄧ種
機率加運算的遊戲,
在方塊中隨意點選1
格,點錯的話,就
會有紅色地雷跑出
來,非常好玩,是
個不錯的小遊戲
系統功能說明
執行遊戲時,會出現
10*15的地雷方格,在
任意選取,開始進行遊
戲,輸了就會顯示地雷
爆炸
程式碼設計
Public Class Form1
Private Const gX As Integer = 15 '遊戲盤面
大小,可自行更改設定
Private Const gY As Integer = 10
Private Const bn As Double = 0.2 '地雷在遊
戲盤面的比例數,預設20%
Dim gb As GameButton(,)
Dim boomNum As Integer = (gX * gY) * bn '
地雷數量
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i, j As Integer
gb = New GameButton(gX - 1, gY - 1) {} '產生GameButton物件陣列
'建立GameButton物件,並設定位置座標、物件索引值
For j = 0 To gb.GetLength(1) - 1
For i = 0 To gb.GetLength(0) - 1
gb(i, j) = New GameButton()
gb(i, j).Left = i * gb(i, j).Width + 1
gb(i, j).Top = j * gb(i, j).Height + 1
gb(i, j).IndexX = i
gb(i, j).IndexY = j
Controls.Add(gb(i, j))
AddHandler (gb(i, j).Click), AddressOf Gb_Click '所有物件事件統
一指向同一Sub
Next
Next
Call PutBoom() '放地雷
End Sub
'放地雷
Private Sub PutBoom()
Dim i As Integer
Dim tempAry() As Integer = New Integer(gX * gY - 1) {} '讓亂數地雷不
會放同一個位置的一維陣列
Dim temp = tempAry.Length - 1
Dim rndNum As Integer
'設定檢查不重覆放地雷的陣列
For i = 0 To tempAry.Length - 1
tempAry(i) = i
Next
'放總鍵鈕數一定比率的地雷
Randomize()
For i = 1 To boomNum
rndNum = Int(Rnd() * temp)
gb(tempAry(rndNum) Mod gX, Int(tempAry(rndNum) /
gX)).haveBoom = True
tempAry(rndNum) = tempAry(temp)
temp -= 1
Next
End Sub
'遊戲結束,並顯示地雷位置
Private Sub GameOver()
Dim i, j As Integer
For j = 0 To gb.GetLength(1) - 1
For i = 0 To gb.GetLength(0) - 1
If gb(i, j).haveBoom Then
gb(i, j).pic = GameButton.picSort.PicElse
gb(i, j).BackgroundImage =
My.Resources.Resource1.boom
End If
gb(i, j).Enabled = False '取消按鍵作用
Next
Next
End Sub
Private Sub Gb_Click(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
Dim g As GameButton = sender
Dim surplusBox As Integer = 0 '剩餘的鍵盤數
Dim i, j As Integer
If e.Button = Windows.Forms.MouseButtons.Left Then '點滑鼠左鍵
If Not (g.pic = GameButton.picSort.PicStop) Then
'沒插旗才能點
If g.haveBoom Then
Call GameOver()
g.BackgroundImage = My.Resources.Resource1.boomclick
MsgBox("Game Over!")
End If
removeBoom(g)
End If
End If
'檢查剩餘鍵盤數
For j = 0 To gb.GetLength(1) - 1
For i = 0 To gb.GetLength(0) - 1
If gb(i, j).pic =
GameButton.picSort.PicBox Or gb(i, j).pic =
GameButton.picSort.PicStop Then
surplusBox += 1
End If
Next
Next
If surplusBox = boomNum Then
MsgBox("過關了!")
End If
End Sub
Private Sub removeBoom(ByVal g As GameButton)
Dim i, j As Integer
Dim x, y As Integer
'周圍八點的(X, Y)座標
Dim boomSum As Integer = 0
If g.pic = GameButton.picSort.PicBox Then '正常按鍵才作用
'檢查周圍地雷數量
For j = -1 To 1
For i = -1 To 1
x = g.IndexX + i
y = g.IndexY + j
If (Not (i = 0 And j = 0)) And x >= 0 And x < gX And y >= 0 And y
< gY Then
If gb(x, y).haveBoom = True Then boomSum += 1
End If
Next
Next
'依檢查數量設定數字圖片
Select Case boomSum
Case 0 : g.BackgroundImage = My.Resources.Resource1.space
Case 1 : g.BackgroundImage = My.Resources.Resource1._01
Case 2 : g.BackgroundImage = My.Resources.Resource1._02
Case 3 : g.BackgroundImage = My.Resources.Resource1._03
Case 4 : g.BackgroundImage = My.Resources.Resource1._04
Case 5 : g.BackgroundImage = My.Resources.Resource1._05
Case 6 : g.BackgroundImage = My.Resources.Resource1._06
Case 7 : g.BackgroundImage = My.Resources.Resource1._07
Case 8 : g.BackgroundImage = My.Resources.Resource1._08
End Select
g.pic = GameButton.picSort.PicElse
'遇到周圍沒有地雷的點,再以遞迴進入周圍其他點檢查
If boomSum = 0 Then
For j = -1 To 1
For i = -1 To 1
x = g.IndexX + i
y = g.IndexY + j
'進入遞迴
If (Not (i = 0 And j = 0)) And x >= 0
And x < gX And y >= 0 And y < gY Then
If gb(x, y).pic =
GameButton.picSort.PicBox Then
removeBoom(gb(x, y))
End If
End If
Next
Next
End If
End If
End Sub
End Class
'設定新型別GameButton,並繼承PictureBox
Public Class GameButton
Inherits PictureBox
Public buttonStatus As Integer '
Public haveBoom As Boolean
'是否有地雷
Public pic As picSort
'圖片的狀態
Public IndexX, IndexY As Integer '物件二維陣列的
索引值
'圖片的狀態列舉
Enum picSort
PicBox '未點選的按鍵
PicStop '插旗不可點選
PicElse
End Enum
Public Sub New()
AddHandler (Me.Click), AddressOf GameButton_Click
AddHandler (Me.MouseDown), AddressOf
GameButton_MouseDown 'GameButton的MouseDown事件
haveBoom = False '預設沒地雷
Width = 25
'預設大小
Height = 25
Visible = True
'預設為未點選按鍵圖片
pic = picSort.PicBox
BackgroundImage = My.Resources.Resource1.box
End Sub
 'GameButton型別物件,點下滑鼠左鍵時,顯示下
凹圖片
 Private Sub GameButton_MouseDown(ByVal
sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)

If e.Button =
Windows.Forms.MouseButtons.Left Then

If pic = picSort.PicBox Then

BackgroundImage =
My.Resources.Resource1.boxclick

End If

End If
 End Sub












Private Sub GameButton_Click(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Right
Then
If pic = picSort.PicBox Then
'插旗
pic = picSort.PicStop
BackgroundImage =
My.Resources.Resource1._stop
ElseIf pic = picSort.PicStop Then '拆旗
pic = picSort.PicBox
BackgroundImage =
My.Resources.Resource1.box
End If
End If
End Sub
End Class
心得
Visual Basic 還滿簡單的,因為它的介
面較人性化,在邏輯方面就比較有概念了,
Visual Basic 的介面有許多中文解釋,而這
次老師叫我們設計遊戲程式,雖然有點難
度,但是還是要實際去操作才明白其中的
奧妙,謝謝老師這學期教我們這多VB程式
的應用,讓我們了解更多,能設計出更多
更不同的東西,遊戲也是其中之一,因為
大家都很喜歡玩遊戲,所以,現在才之到,
創造一個好遊戲試不簡單的。