我有关于碰撞检测的另一个问题。我目前正在使用图片框制作掉落的物体,其中这些图片框的图像是从我也创建的图像列表中随机取出的。问题是,有两种类型的图像;一条鱼和一枚硬币。我想要做的是让程序识别出某个碰撞所具有的图像(如果它是硬币),并更新硬币程序被绑定的标签。我尝试了各种各样的东西,但我似乎无法弄清楚问题的根源。有人可以帮帮我吗?
到目前为止我有这个代码:
'Collision
If PictureBox1.Bounds.IntersectsWith(play_avatar.Bounds) Then
If PictureBox1.ImageLocation Is My.Resources.coin Then
cns += 1
lbl_coins.Text += cns
End If
PictureBox1.Top -= Panel1.Height
PictureBox1.Image.Dispose()
PictureBox1.Image = ImageList1.Images(rno1)
scr += 1
lbl_score.Text += scr
End If
If PictureBox2.Bounds.IntersectsWith(play_avatar.Bounds) Then
If PictureBox1.ImageLocation Is My.Resources.coin Then
cns += 1
lbl_coins.Text += cns
End If
PictureBox2.Top -= Panel1.Height
PictureBox2.Image.Dispose()
PictureBox2.Image = ImageList1.Images(rno2)
scr += 1
lbl_score.Text += scr
End If
If PictureBox3.Bounds.IntersectsWith(play_avatar.Bounds) Then
If PictureBox3.ImageLocation Is My.Resources.coin Then
cns += 1
lbl_coins.Text += cns
End If
PictureBox3.Top -= Panel1.Height
PictureBox3.Image.Dispose()
PictureBox3.Image = ImageList1.Images(rno3)
scr += 1
lbl_score.Text += scr
End If
这是图像列表及其随机函数:
ImageList1.Images.Add(My.Resources.coin)
ImageList1.Images.Add(My.Resources.f1)
ImageList1.Images.Add(My.Resources.f2)
ImageList1.Images.Add(My.Resources.f3)
ImageList1.Images.Add(My.Resources.f4)
ImageList1.Images.Add(My.Resources.f5)
ImageList1.Images.Add(My.Resources.f6)
ImageList1.Images.Add(My.Resources.f7)
ImageList1.Images.Add(My.Resources.f8)
ImageList1.Images.Add(My.Resources.f9)
Dim cns As Integer = 0
Dim rnd As New Random
Dim rno1 As Integer
Dim rno2 As Integer
Dim rno3 As Integer
rno1 = rnd.Next(0, 10)
rno2 = rnd.Next(0, 10)
rno3 = rnd.Next(0, 10)
编辑:
这是整个代码。
Imports System.IO
Public Class EClassic
Dim images() As String
Dim rnd As New Random
Dim rno1 As Integer
Dim rno2 As Integer
Dim rno3 As Integer
Dim flist As New List(Of PictureBox)
Dim dlist As New List(Of Image)
Dim scr As Integer = 0
Dim life As Integer = 5
Dim cns As Integer = 0
Dim picturebox1ImageIndex, picturebox2ImageIndex, picturebox3ImageIndex As Integer
Private Sub EClassic_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With flist
.Add(PictureBox1)
.Add(PictureBox2)
.Add(PictureBox3)
End With
lbl_score.Text = scr
lbl_life.Text = life
lbl_coins.Text = cns
End Sub
Private Sub EClassic_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Dim c As Integer = Panel1.ClientSize.Width
Dim res As Integer
Dim res2 As Integer
res2 = c - c + 100
res = c / 2
Select Case e.KeyCode
Case Keys.Left
If play_avatar.Left > res2 Then
play_avatar.Left -= 100
ElseIf play_avatar.Left < res2 Then
play_avatar.Left -= 0
End If
Case Keys.Right
If play_avatar.Left < res Then
play_avatar.Left += 100
ElseIf play_avatar.Left > res Then
play_avatar.Left -= 0
End If
End Select
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim mov As Integer = rnd.Next(0, flist.Count - 0)
Me.flist(mov).Top += 20
'Fish - Dragon Collision
If PictureBox1.Bounds.IntersectsWith(play_avatar.Bounds) Then
PictureBox1.Image.Dispose()
PictureBox1.Top -= Panel1.Height
rno1 = rnd.Next(0, 9)
PictureBox1.Image = ImageList1.Images(rno1)
picturebox1ImageIndex = rno1
scr += 1
lbl_score.Text += scr
If picturebox1ImageIndex = 0 Then '0 is the index of coin in ImageList1
cns += 1
lbl_coins.Text += cns
End If
End If
If PictureBox2.Bounds.IntersectsWith(play_avatar.Bounds) Then
PictureBox2.Image.Dispose()
PictureBox2.Top -= Panel1.Height
rno2 = rnd.Next(0, 9)
PictureBox2.Image = ImageList1.Images(rno2)
picturebox2ImageIndex = rno2
scr += 1
lbl_score.Text += scr
If picturebox2ImageIndex = 0 Then
lbl_coins.Text += cns
End If
End If
If PictureBox3.Bounds.IntersectsWith(play_avatar.Bounds) Then
PictureBox3.Image.Dispose()
PictureBox3.Top -= Panel1.Height
rno3 = rnd.Next(0, 9)
PictureBox3.Image = ImageList1.Images(rno3)
picturebox3ImageIndex = rno3
scr += 1
lbl_score.Text += scr
If picturebox3ImageIndex = 0 Then
cns += 1
lbl_coins.Text += cns
End If
End If
'Fish - Line Collision
If PictureBox1.Bounds.IntersectsWith(line.Bounds) Then
life -= 1
PictureBox1.Top -= Panel1.Height
lbl_life.Text = life
End If
If PictureBox2.Bounds.IntersectsWith(line.Bounds) Then
life -= 1
PictureBox2.Top -= Panel1.Height
lbl_life.Text = life
End If
If PictureBox3.Bounds.IntersectsWith(line.Bounds) Then
life -= 1
PictureBox3.Top -= Panel1.Height
lbl_life.Text = life
End If
If life = 0 Then
Timer1.Stop()
GameOver.Show()
Exit Sub
End If
End Sub
结束班
答案 0 :(得分:0)
PictureBox1.ImageLocation My.Resources.coin 在两个方面有误:
1. comparing string with bitmap. Turn on option strict
2. even if you do PictureBox1.Image Is My.Resources.coin it will always be false because it is a different object
你应该为每个图片框使用一个变量,例如picturebox1ImageIndex,picturebox2ImageIndex和compare
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim mov As Integer = rnd.Next(0, flist.Count - 0)
Me.flist(mov).Top += 20
'Fish - Dragon Collision
If PictureBox1.Bounds.IntersectsWith(play_avatar.Bounds) Then
'PictureBox1.Image.Dispose() ' you dont need that
PictureBox1.Top -= Panel1.Height '<- place break point. See if this code is triggered
rno1 = rnd.Next(0, 9)
PictureBox1.Image = ImageList1.Images(rno1) '<- place break point check rno1
picturebox1ImageIndex = rno1
scr += 1 '<- place break point check picturebox1ImageIndex
lbl_score.Text += scr
If picturebox1ImageIndex = 0 Then '<- place break point
cns += 1 '<- place break point
lbl_coins.Text += cns
End If
End If
If PictureBox2.Bounds.IntersectsWith(play_avatar.Bounds) Then
'PictureBox2.Image.Dispose() ' you dont need that
PictureBox2.Top -= Panel1.Height '<- place break point. See if this code is triggered
rno2 = rnd.Next(0, 9)
PictureBox2.Image = ImageList1.Images(rno2) '<- place break point check rno2
picturebox2ImageIndex = rno2
scr += 1 '<- place break point check picturebox2ImageIndex
lbl_score.Text += scr
If picturebox2ImageIndex = 0 Then '<- place break point
lbl_coins.Text += cns '<- place break point
End If
End If
If PictureBox3.Bounds.IntersectsWith(play_avatar.Bounds) Then
'PictureBox3.Image.Dispose() ' you dont need that
PictureBox3.Top -= Panel1.Height '<- place break point. See if this code is triggered
rno3 = rnd.Next(0, 9)
PictureBox3.Image = ImageList1.Images(rno3) '<- place break point check rno3
picturebox3ImageIndex = rno3
scr += 1 '<- place break point check picturebox3ImageIndex
lbl_score.Text += scr
If picturebox3ImageIndex = 0 Then '<- place break point
cns += 1 '<- place break point
lbl_coins.Text += cns
End If
End If
'Fish - Line Collision
If PictureBox1.Bounds.IntersectsWith(line.Bounds) Then
life -= 1
PictureBox1.Top -= Panel1.Height
lbl_life.Text = life
End If
If PictureBox2.Bounds.IntersectsWith(line.Bounds) Then
life -= 1
PictureBox2.Top -= Panel1.Height
lbl_life.Text = life
End If
If PictureBox3.Bounds.IntersectsWith(line.Bounds) Then
life -= 1
PictureBox3.Top -= Panel1.Height
lbl_life.Text = life
End If
If life = 0 Then
Timer1.Stop()
GameOver.Show()
Exit Sub
End If
End Sub