0% found this document useful (0 votes)
64 views3 pages

Visual Basic Car Racing Game Code

This code defines a car racing game with the following key elements: - There are picture boxes representing the road that scroll upwards, increasing in speed based on the player's score. - The player controls a car with left and right arrow keys to avoid enemy cars that move down the screen. - Collision with an enemy car triggers a "game over" sequence, otherwise the player's score increments when an enemy car passes. - The replay button resets the game state and reloads the form to allow restarting the game.

Uploaded by

Bryan Siburian
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views3 pages

Visual Basic Car Racing Game Code

This code defines a car racing game with the following key elements: - There are picture boxes representing the road that scroll upwards, increasing in speed based on the player's score. - The player controls a car with left and right arrow keys to avoid enemy cars that move down the screen. - Collision with an enemy car triggers a "game over" sequence, otherwise the player's score increments when an enemy car passes. - The replay button resets the game state and reloads the form to allow restarting the game.

Uploaded by

Bryan Siburian
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Public Class Form1

Dim speed As Integer


Dim road(7) As PictureBox
Dim Score As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles [Link]
speed = 3
road(0) = PictureBox1
road(1) = PictureBox2
road(2) = PictureBox3
road(3) = PictureBox4
road(4) = PictureBox5
road(5) = PictureBox6
road(6) = PictureBox7
road(7) = PictureBox8
End Sub

Private Sub RoadMover_Tick(sender As Object, e As EventArgs) Handles [Link]


For x As Integer = 0 To 7
road(x).Top += 2
If road(x).Top >= [Link] Then
road(x).Top = -road(x).Height
End If
Next
If Score > 10 And Score < 25 Then
speed = 4
End If
If Score > 25 And Score < 40 Then
speed = 5
End If
If Score > 40 Then
speed = 6
End If
Speed_Text.Text = "Speed " & speed
If ([Link]([Link])) Then
gameOver()
End If
If ([Link]([Link])) Then
gameOver()
End If
If ([Link]([Link])) Then
gameOver()
End If

End Sub
Private Sub gameOver()
Replay_Button.Visible = True
[Link] = True
End_Text.Visible = True
[Link]()
Enemy1_Mover.Stop()
Enemy2_Mover.Stop()
Enemy3_Mover.Stop()
End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles [Link]


If [Link] = [Link] Then
Right_mover.Start()
End If
If [Link] = [Link] Then
Left_mover.Start()
End If
End Sub

Private Sub Right_mover_Tick(sender As Object, e As EventArgs) Handles


Right_mover.Tick
If ([Link].X < 190) Then
[Link] += 5
End If
End Sub

Private Sub Left_mover_Tick(sender As Object, e As EventArgs) Handles Left_mover.Tick


If ([Link].X > 0) Then
[Link] -= 5
End If
End Sub

Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles [Link]


Right_mover.Stop()
Left_mover.Stop()
End Sub

Private Sub car_Click(sender As Object, e As EventArgs) Handles [Link]

End Sub

Private Sub Enemy1_Mover_Tick(sender As Object, e As EventArgs) Handles


Enemy1_Mover.Tick

[Link] += speed / 2
If [Link] >= [Link] Then
Score += 1
Score_Text.Text = "Score " & Score
[Link] = -(CInt([Link](Rnd() * 150)) + [Link])
[Link] = CInt([Link](Rnd() * 50)) + 0

End If
End Sub

Private Sub Enemy2_Mover_Tick(sender As Object, e As EventArgs) Handles


Enemy2_Mover.Tick
[Link] += speed
If [Link] >= [Link] Then
Score += 1
Score_Text.Text = "Score " & Score
[Link] = -(CInt([Link](Rnd() * 150)) + [Link])
[Link] = CInt([Link](Rnd() * 50)) + 100
End If

End Sub

Private Sub Enemy3_Mover_Tick(sender As Object, e As EventArgs) Handles


Enemy3_Mover.Tick
[Link] += speed * 3 / 2
If [Link] >= [Link] Then
Score += 1
Score_Text.Text = "Score " & Score
[Link] = -(CInt([Link](Rnd() * 150)) + [Link])
[Link] = CInt([Link](Rnd() * 40)) + 150
End If

End Sub

Private Sub Replay_Button_Click(sender As Object, e As EventArgs) Handles


Replay_Button.Click
Score = 0
[Link]()
InitializeComponent()
Form1_Load(e, e)

End Sub
End Class

You might also like