作者koalaflsh (刷子)
看板Visual_Basic
标题Re: [.NET] 捉小偷程式码更改
时间Wed May 25 13:49:23 2016
Imports System.Text 'Encode会用到
Public Class MainForm
#Region "Variables Declaration"
'储存状态用变数
Public UserName As String '使用者名称
'游戏用变数
Private createTime As Integer '要产生地鼠的剩余时间
Private userColor As Color '玩家和按钮颜色
Private score As Integer '玩家分数
#End Region
#Region "Game Functions"
Private Delegate Sub DelegateInitialize()
'初始化游戏状态
Public Sub Initialize()
'初始化
score = 0
Label_UserScoreNum.Text = "0"
'将所有洞清空
Dim i As Integer
For i = 0 To Panel_GameArea.Controls.Count - 1
If Panel_GameArea.Controls(i).Name.Substring(0, 4) = "Hole" Then
Dim tmpPic As PictureBox = Panel_GameArea.Controls(i)
tmpPic.Image = ImageList_Rat.Images.Item(0)
tmpPic.Tag = 0
End If
Next
End Sub
'开始计时
Private Delegate Sub DelegateStartTimer()
Private Sub StartTimer()
Label_Time.Text = "20"
Timer_Game.Enabled = True
Timer_Thief.Enabled = True
Timer_elag.Enabled = True
End Sub
'停止计时
Private Sub StopTimer()
Label_Time.Text = "-"
Timer_Game.Enabled = False
'将所有洞清空
Dim i As Integer
For i = 0 To Panel_GameArea.Controls.Count - 1
If Panel_GameArea.Controls(i).Name.Substring(0, 4) = "Hole" Then
Dim tmpPic As PictureBox = Panel_GameArea.Controls(i)
tmpPic.Image = ImageList_Rat.Images.Item(0)
tmpPic.Tag = 0
End If
Next
End Sub
'每秒游戏计时以及随机产生地鼠
Private Sub Timer_Game_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer_Game.Tick
UpdateRatLiveTime() '更新每个地鼠存活时间
CreateRandomRat() '邀请方产生地鼠
CheckGame() '检查游戏是否结束
End Sub
Private Sub UpdateRatLiveTime()
'每秒递减每个地鼠的剩余时间,若为0则变为空洞
If Integer.Parse(Label_Time.Text) <> 0 Then
Dim i As Integer
For i = 0 To Panel_GameArea.Controls.Count - 1
If Panel_GameArea.Controls(i).Name.Substring(0, 4) = "Hole"
Then
Dim tmpPic As PictureBox = Panel_GameArea.Controls(i)
If tmpPic.Tag <> 0 Then
tmpPic.Tag -= 1
Else
tmpPic.Image = ImageList_Rat.Images.Item(0)
tmpPic.Refresh()
End If
End If
Next
Label_Time.Text = (Integer.Parse(Label_Time.Text) - 1).ToString
ElseIf Integer.Parse(Label_Time.Text) = 0 Then
StopTimer()
End If
End Sub
'受邀者产生地鼠
Private Sub CreateRat(ByVal CreateName As String)
Dim i As Integer
For i = 0 To Panel_GameArea.Controls.Count - 1
If Panel_GameArea.Controls(i).Name = CreateName Then
Dim tmpPic As PictureBox = Panel_GameArea.Controls(i)
tmpPic.Image = My.Resources.peace
tmpPic.Refresh()
tmpPic.Tag = 3
ElseIf Panel_GameArea.Controls(i).Name = CreateName Then
Dim tmpPic2 As PictureBox = Panel_GameArea.Controls(i)
tmpPic2.Image = My.Resources.elag
tmpPic2.Refresh()
tmpPic2.Tag = 3
Exit For
End If
Next
End Sub
Private Sub CreateRandomRat()
Dim index As Integer
If createTime = 0 Then
Randomize()
createTime = Rnd() * 5
Else
createTime -= 1
End If
If createTime <> 0 Then
While True
Randomize()
index = Rnd() * 4
Dim tmpPic As PictureBox = Panel_GameArea.Controls(index)
Dim tmpPic2 As PictureBox = Panel_GameArea.Controls(index)
'假如是空洞,才产生地鼠
If tmpPic.Tag = 0 Then
tmpPic.Image = My.Resources.peace
tmpPic.Refresh()
tmpPic.Tag = 1
Else : tmpPic2.Tag = 0
tmpPic2.Image = My.Resources.elag
tmpPic2.Refresh()
tmpPic2.Tag = 0
Exit While
End If
End While
End If
End Sub
'检查游戏是否结束
'
Private Sub CheckGame()
If Integer.Parse(Label_Time.Text) = 0 Then
StopTimer()
MsgBox("您的成绩:" + score.ToString)
End If
End Sub
'当玩家打中地鼠时事件
Public Sub ratClick(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Hole1.Click, Hole2.Click, _
Hole3.Click, Hole4.Click, Hole5.Click, Hole6.Click, Hole7.Click,
Hole8.Click, Hole9.Click
Dim tmpPic As PictureBox = sender
Dim tmpPic2 As PictureBox = sender
'以地鼠的时间标签来判断 (也可以用图片判断)
If tmpPic.Tag <> 0 Then
'加分
score += 50
Label_UserScoreNum.Text = score.ToString
'清空地洞
tmpPic.Tag = 0
tmpPic.Image = ImageList_Rat.Images.Item(0)
ElseIf tmpPic2.Tag <> 0 Then
'加分
score -= 50
Label_UserScoreNum.Text = score.ToString
'清空地洞
tmpPic2.Tag = 0
tmpPic2.Image = ImageList_Rat.Images.Item(0)
End If
End Sub
'游戏结束,回覆之前状态
Private Sub EndGame()
score = 0
Label_UserScoreNum.Text = "0"
StopTimer()
End Sub
#End Region
'当游戏视窗载入便开始游戏
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Initialize()
StartTimer()
End Sub
End Class
我自己改後,还是没有办法有这样的效果,反而是只有小偷的图片是加分的,是哪里改错
吗?也谢谢之前的两位大大的回答了!
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 59.115.103.179
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Visual_Basic/M.1464155366.A.3C4.html
1F:→ MOONRAKER: 改了tag当然别的地方也要改阿 05/25 14:18
2F:→ MOONRAKER: 计分的地方要对应tag阿 你去游泳池会只脱上衣吗 05/25 14:19