我有关于碰撞检测的另一个问题。我目前正在使用相框来制作落下的物体,其中这些相框的图像是从我创建的图像列表中以随机方式获取的。问题是,有两种类型的图像;一条鱼和一枚硬币。我想要做的是让程序识别出图像(如果它是一枚硬币),发生某种碰撞并更新硬币程序绑定的标签。我尝试了各种各样的东西,但我似乎无法弄清楚问题的根源。任何人都可以请帮我吗?碰撞检测和图像识别问题
我到目前为止这样的代码:分别
'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
末级
实际问题是什么?你只说明了你想要的代码,而不是它目前的功能。 – Psychemaster 2014-10-08 12:14:51
我觉得程序不会识别它是不是硬币,都是平等的(鱼类)。随机化后,我怎么可能让程序识别它是否为硬币?或者甚至不是在随机化后,我认为会做..我完全失去了。可能是这个问题,不是吗? 'If PictureBox1.ImageLocation is My.Resources.coin Then cns + = 1 lbl_coins.Text + = cns End If' – Chris 2014-10-08 13:00:02
因为在此条件之后,cns(实际上是硬币计数器)没有更新。 – Chris 2014-10-08 13:01:10