在我的模拟中,行星的颜色在创建时随机选择。留下的痕迹应该与它的星球颜色相同。如果只有一颗行星,这种方法很好,但是当添加一颗新行星时,每颗行星的踪迹都与新行星的颜色相同。以下是一些截图,用于演示我的意思:https://imgur.com/a/EVh5o
如果有意义的话,我每个星球都有一条与自身颜色相同的痕迹。以下是我的代码的相关部分:
Public Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles picSpace.Paint
For Each sun In sunsList
e.Graphics.FillEllipse(Brushes.Yellow, CInt(sun.positionX - 10), CInt(sun.positionY - 30), 20, 20)
Next
For Each planet In planetsList
Dim planetFill As Brush = New SolidBrush(planet.colour)
Dim trailColour As Pen = New Pen(planet.colour)
e.Graphics.FillEllipse(planetFill, planet.displayX - 5, planet.displayY - 5, 10, 10)
For count As Integer = 0 To counter
e.Graphics.DrawEllipse(trailColour, trail(0, count), trail(1, count), 1, 1)
Next
Next
End Sub
Sub Position()
planet.displayX = Math.Round(planet.positionX)
planet.displayY = Math.Round(planet.positionY)
trail(0, counter) = planet.displayX
trail(1, counter) = planet.displayY
counter += 1
ReDim Preserve trail(1, counter)
End Sub
Private Sub space_Click(sender As Object, e As EventArgs) Handles picSpace.Click
If hsbSimulationSpeed.Value > 0 Then
Timer1.Enabled = True
End If
If chkAddPlanets.Checked = True Then
planet = New Body
numberOfPlanets += 1
planet.colour = GetRandomColour()
planet.positionX = MousePosition.X
planet.positionY = MousePosition.Y - 25
planet.velocityX = txtVelocityX.Text * 1000
planet.velocityY = txtVelocityY.Text * 1000
planetsList.Add(planet)
ElseIf chkAddSuns.Checked = True Then
sun = New Body
numberOfSuns += 1
sun.positionX = MousePosition.X
sun.positionY = MousePosition.Y
sun.mass = hsbSunMass.Value * 5 * (10 ^ 29)
sunsList.Add(sun)
End If
End Sub
Function GetRandomColour() As Color
Dim rand As New Random
Return Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0,
256))
End Function
有没有一种简单的方法可以解决这个问题,还是需要彻底改变生成路径的方式?
答案 0 :(得分:0)
问题是你只有1个trail()列表,用于所有行星。因此,在您的绘画循环中,尽管行星环为每个行星选择不同的笔,但它们都在相同的坐标中绘制。最后一颗行星将覆盖第一颗行星。据推测,trail()包含所有行星的坐标 - 即所有列在一起的轨迹。
每个星球都需要有一个踪迹清单,作为行星物体的一部分,以使其发挥作用。