Arrays 有没有一种方法可以轻松地在对象之间循环?
我想对照其他坐标列表检查一个坐标对象列表。我以前就是这样写的:所有这些都是Arrays 有没有一种方法可以轻松地在对象之间循环?,arrays,vb.net,class,loops,object,Arrays,Vb.net,Class,Loops,Object,我想对照其他坐标列表检查一个坐标对象列表。我以前就是这样写的:所有这些都是Ship对象,CheckForCollisions在Ship类中 'AIR CRAFT CARRIER If AirCraftCarrier.has_moved And Battleship.has_moved Then AirCraftCarrier.CheckForCollisions(Battleship) ElseIf AirCraftCarrier.has_moved And Submarine.ha
Ship
对象,CheckForCollisions
在Ship
类中
'AIR CRAFT CARRIER
If AirCraftCarrier.has_moved And Battleship.has_moved Then
AirCraftCarrier.CheckForCollisions(Battleship)
ElseIf AirCraftCarrier.has_moved And Submarine.has_moved Then
AirCraftCarrier.CheckForCollisions(Submarine)
ElseIf AirCraftCarrier.has_moved And Destroyer.has_moved Then
AirCraftCarrier.CheckForCollisions(Destroyer)
ElseIf AirCraftCarrier.has_moved And PatrolBoat.has_moved Then
AirCraftCarrier.CheckForCollisions(PatrolBoat)
End If
'BATTLESHIP
If Battleship.has_moved And AirCraftCarrier.has_moved Then
Battleship.CheckForCollisions(AirCraftCarrier)
ElseIf Battleship.has_moved And Submarine.has_moved Then
Battleship.CheckForCollisions(Submarine)
ElseIf Battleship.has_moved And Destroyer.has_moved Then
Battleship.CheckForCollisions(Destroyer)
ElseIf Battleship.has_moved And PatrolBoat.has_moved Then
Battleship.CheckForCollisions(PatrolBoat)
End If
'etc., there's 3 more that look exactly this this.
但我肯定更愿意使用循环来实现这一点。这也不管用。它只会检查第一艘船(AirCraftCarrier对象),然后忽略其余的。只有和ACC相撞的船只才会登记,如果它们彼此相撞,什么也不会发生
这是我写的一个例子,我自己尝试并实现它。我在考虑使用ID并循环使用它们,但我不知道如何做到这一点。你们有什么概念可以介绍给我,你们认为对我有帮助吗
Public Class Main
Dim acc As Ship
Dim bs As Ship
Dim sm As Ship
Dim ds As Ship
Dim pb As Ship
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
acc = New Ship(1, {{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}})
bs = New Ship(2, {{0, 4}, {1, 4}, {2, 4}, {3, 4}})
sm = New Ship(3, {{4, 5}, {4, 6}, {4, 7}})
ds = New Ship(4, {{3, 6}, {4, 6}, {5, 6}})
pb = New Ship(5, {{7, 7}, {7, 8}})
End Sub
End Class
Public Class Ship
Dim _id As Integer
Public Property id() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Dim _magnitude(,) As Integer
Public Property magnitude As Integer(,)
Get
Return _magnitude
End Get
Set(ByVal value As Integer(,))
_magnitude = value
End Set
End Property
Public Sub New(ByVal idp As Integer, ByVal magnitudep As Integer(,))
id = idp
magnitude = magnitudep
End Sub
End Class
你能这样做吗
'A list of all the ships
Dim ships As Ship() = _
{ AirCraftCarrier, Battleship, Submarine, _
Destroyer, PatrolBoat}
Dim query = _
From ship1 in ships ' iterate thru the ships
From ship2 in ships ' iterate thru the ships
Where ship1 <> ship2 ' keep only if both ships are not the same ship
Where ship1.has_moved 'keep if ship1 has moved
Where ship2.has_moved 'keep if ship2 has moved
Select New With { ship1, ship2 }
For Each s in query
' for each pair of moved ships check for collision
s.ship1.CheckForCollisions(s.ship2)
Next
“所有船只的列表
将船舶设置为船舶()_
{航空母舰、战列舰、潜艇、_
驱逐舰,巡逻艇}
Dim查询=_
从ships中的ship1迭代到ships
在船舶的迭代中从船舶2开始
其中,只有当两艘船不是同一艘船时,才保留ship1 ship2
ship1.has_移动的位置“如果ship1已移动,则保留”
ship2.has_移动的位置“如果ship2已移动,则保留”
使用{ship1,ship2}选择新建
对于查询中的每个
“对于每对移动的船舶,检查碰撞情况
s、 船舶1.碰撞检查(s.ship2)
下一个
如果你有一个GameBoard类知道每艘船的位置,你可以在它移动之前检查目的地是否有其他船只,而不是检查每艘船是否发生碰撞。否则,您可以将船舶存储在一个列表中,并根据刚刚移动的船舶迭代该列表。你可能想研究继承——战列舰类应该知道自己的统计数据,而不是在ctor中被告知。继承将允许一个潜艇类,它是一种船的类型,只实现潜艇特有的东西,比如鱼雷。我如何让游戏板知道每艘船的位置?我曾想过尝试一下,但我不知道怎么做。当一艘船想要移动时,它会有一个128x128(?)的静态阵列。选中move(x,y)如果是真的,它可以移动到那里;然后在移动.ShipMoved(Me,x,y)
和游戏之后,将该位置标记为已识别的船所占据。Game类可以处理其他事情,如IsInRangeOf
。这在很大程度上取决于编写游戏还是学习编程。在过去的2-3天里,我看到了这种增长,我不得不问:究竟是什么规模?在这种情况下,那些数组可以被一个简单的大小对象所取代:1x6、3x1、1x3、2x1、1x2(我想)。然后,游戏类可以根据船舶的方向和当前的X,Y,从中创建一个Rect。然后,使用矩形的IntersectsWith
功能进行碰撞检查非常简单。游戏只需在(船只)列表中循环,看看这艘船的矩形是否与其他任何船只的矩形相交。你能解释一下吗?我不完全明白。@alexanderd5398-这有帮助吗?有一点。船舶列表中的下划线是什么意思?行继续字符只允许您在下一行继续?