【问题标题】:Is there a way to easily loop through objects?有没有办法轻松循环对象?
【发布时间】:2014-05-14 02:29:48
【问题描述】:

我想对照其他所有坐标列表检查对象的坐标列表。我之前是这样写的:所有这些都是Ship对象,而CheckForCollisionsShip类中。

'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

【问题讨论】:

  • 而不是检查与每艘船的碰撞,如果你有一个知道每艘船在哪里的 GameBoard 类,你可以用它检查目的地是否有另一艘船 在它移动之前。否则,您可以将船只存储在一个列表中,并根据刚刚移动的那个迭代列表。您可能想研究继承 - BattleShip 类应该知道它自己的统计数据,而不是在 ctor 中被告知这些东西。继承将允许作为 Ship 类型的 Submarine 类仅实现潜艇独有的东西,例如鱼雷。
  • 如何让游戏板知道每艘船的位置?我曾短暂地想过尝试,但我不知道怎么做。
  • 棋盘有一个 128x128 (?) 的静态数组,当一艘船想要移动它时,它会 GameBrd.CheckMove(x, y) 如果为真它可以移动到那里;然后在移动.ShipMoved(Me, x, y) 和游戏之后,将该位置标记为被识别的船占据。 Game 类可以处理诸如IsInRangeOf 之类的其他事情。很大程度上取决于重点是写游戏还是学习编程。
  • 在过去 2-3 天看到这种增长,我不得不问:到底是什么数量级?
  • 在这种情况下,这些数组可以替换为简单的Size 对象:1x6、3x1、1x3、2x1、1x2(我认为)。然后,Game 类可以根据船的方向和当前 X、Y 从它创建一个 Rect。然后使用RectangleIntersectsWith 函数进行碰撞检查非常简单。游戏只需遍历 List(Of Ships) 并查看 thisShip.IntersectsWith 的矩形是否与任何其他船的矩形相同。

标签: arrays vb.net class loops object


【解决方案1】:

你能做这样的事情吗?

'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

【讨论】:

  • 你能解释一下吗?我不完全明白。
  • @alexanderd5398 - 这有帮助吗?
  • 一点。舰船列表中的下划线是什么意思?
  • 行继续符
  • 它只是让你在下一行继续?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
相关资源
最近更新 更多