I'm having a difficult time getting my graphics to clear. It's for my lightcycle game(check signature for full file). At then end of the game, I reset everything back to the original state, but for whatever reason it still paints the last rectangle from the previous game, no matter what I do. This is how I'm painting the form:
This is the check end game sub:
And here is the newgame sub where I set everything back:
I've tried adding a conditional statement to check if the game is playing or not, and if it aint then clear the graphics to the form's backcolor, but that still doesn't get rid of the last point of the previous game. Could anybody help me find my error?
Code:
Dim g As Graphics = e.Graphics
For Each trail As Point In p1trail
Dim rect As New Rectangle(trail, New Size(20, 20))
g.FillRectangle(New SolidBrush(p1.BackColor), rect)
Next
For Each trail As Point In p2trail
Dim rect As New Rectangle(trail, New Size(20, 20))
g.FillRectangle(New SolidBrush(p2.BackColor), rect)
NextCode:
Dim endGame As Boolean = False
For Each trail As Point In bothtrail
Dim rect As New Rectangle(trail, New Size(15, 15))
If p1.Bounds.IntersectsWith(rect) AndAlso p1trail.Count > 1 Then
tmr1.Stop()
endGame = True
MessageBox.Show("Player1 Lost")
Exit For
ElseIf p2.Bounds.IntersectsWith(rect) AndAlso p2trail.Count > 1 Then
tmr1.Stop()
endGame = True
MessageBox.Show("Player2 Lost")
Exit For
End If
Next
If p1.Left < 0 OrElse p1.Top < 0 OrElse p1.Left > Me.Width OrElse p1.Top > Me.Height Then
tmr1.Stop()
endGame = True
MessageBox.Show("Player1 Lost")
ElseIf p2.Left < 0 OrElse p2.Top < 0 OrElse p2.Left > Me.Width OrElse p2.Top > Me.Height Then
tmr1.Stop()
endGame = True
MessageBox.Show("Player2 Lost")
End If
If endGame Then
Call newGame()
End IfCode:
p1 = New PictureBox
p2 = New PictureBox
playing = False
p1trail.Clear()
p2trail.Clear()
bothtrail.Clear()
p1Direct = PlayerDirection.Right
p2Direct = PlayerDirection.Left
Me.Refresh()
With p1
.BackColor = Color.Blue
.Location = New Point(CInt(Me.Width / 2 - 10 - Me.Width / 5), CInt(Me.Height / 2))
.Size = New Size(20, 20)
End With
With p2
.BackColor = Color.Red
.Location = New Point(CInt(Me.Width / 2 + 10 + Me.Width / 5), CInt(Me.Height / 2))
.Size = New Size(20, 20)
End With
p1trail.Add(p1.Location)
p2trail.Add(p2.Location)
bothtrail.AddRange({p1.Location, p2.Location})
Me.Controls.AddRange({p1, p2})
Me.Refresh()