|
-
Aug 8th, 2012, 09:32 PM
#1
Thread Starter
New Member
Visual Basic 2010 School assignment racing car game
Can anyone help with this game as the lap timer wont work properly and keys wont work properly. thanks
Public Class Form1
Private Sub Reset()
' starting positions
picRedCar.Left = Me.Width - picRedCar.Width - 10
picBlueCar.Left = Me.Width - picBlueCar.Width - 10
lblRedLaps.Text = "0" ' reset lap count
lblBlueLaps.Text = "0"
End Sub
Private Sub tmrRace_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrRace.Tick
If picRedCar.Left + picRedCar.Width < 0 Then
picRedCar.Left = Me.Width ' start next lap
lblRedLaps.Text -= 1 ' reduce lap count
If lblRedLaps.Text = "0" Then
tmrRace.Enabled = False ' stop race
MessageBox.Show("Red wins!", "Rally cars", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Reset()
End If
End If
If picBlueCar.Left + picBlueCar.Width < 0 Then
picBlueCar.Left = Me.Width ' start next lap
lblBlueLaps.Text -= 1 ' Reduce lap count
If lblBlueLaps.Text = "0" Then
tmrRace.Enabled = False ' stop race
MessageBox.Show("Blue wins!", "Rally cars", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Reset()
End If
End If
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
tmrRace.Start() ' turn on timer
btnStart.Enabled = False
End Sub
Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
Select Case e.KeyChar
Case "I", "i"
picRedCar.Top -= 4 'decrement by 4
Case "M", "m"
picRedCar.Top += 4 'decrement by 4
Case "J", "j"
picRedCar.Left -= 8 'decrement by 8
Case "D", "d"
picBlueCar.Left -= 8 'decrement by 8
Case "R", "r"
picBlueCar.Top -= 4 'decrement by 4
Case "C", "c"
picBlueCar.Top += 4 'decrement by 4
End Select
End Sub
End Class
-
Aug 8th, 2012, 11:01 PM
#2
Re: Visual Basic 2010 School assignment racing car game
Well first of all this is VB.Net code and you have posted in the VB6 section of the forum which is for older version of VB than what you are using.
What is your timer.interval set to?
Do you have the keypreview property set to true for your form?
Most important what do you mean when you say don't work properly?
Last edited by DataMiser; Aug 8th, 2012 at 11:09 PM.
-
Aug 9th, 2012, 03:43 AM
#3
Thread Starter
New Member
Re: Visual Basic 2010 School assignment racing car game
Hi
Thanks for you help. Sorry about the wrong area, I thought I had posted it correctly aggghh. It is written in Visual Basic 2010.
The timer.interval is set to 20.
I want the cars to move when you press various keys on the keyboard, but when I use the lap timer in the first section and the third section (private sub) the cars won't move.
Excuse my ignorance (I am 15) how to do I find the keypreview to set this up?
Thankssssss
-
Aug 9th, 2012, 04:28 AM
#4
Re: Visual Basic 2010 School assignment racing car game
Welcome to VBForums 
Thread moved from the 'VB6 and Earlier' forum to the 'VB.Net' (VB2002 and later) forum
-
Aug 9th, 2012, 07:16 AM
#5
Re: Visual Basic 2010 School assignment racing car game
One problem is that you're storing the lap counter in the Text property of the textboxes. That's a string, and therefore not a great choice for some numeric data that you want to manipulate as numeric (subtracting one at the end of each lap). I would suggest that you declare two Integer fields (that is, variables that are defined outside any of the subs but inside the Form). This can then be referenced from both the Reset sub (surely it should be setting the value to something greater than zero?) and the tmrRace_Tick sub. In Reset when you set the initial value and in tmeRace_Tick when you change its value, at both points immediately follow with assigning the value of the lapCounter field (converted to its string representation) to the relevant textbox's Text property.
-
Aug 9th, 2012, 09:16 AM
#6
Re: Visual Basic 2010 School assignment racing car game
Excuse my ignorance (I am 15) how to do I find the keypreview to set this up?
KeyPreview is a form property you can set this either by settign it in the property pain of the form. e.g. click the form then go over to the properties or right click on the form and choose properties then locate the keypreview and set it to true in the property window.
Or in your form load event you can add
Code:
Me.KeyPreview = True
This will allow your form to process key strokes even when a control on the form has focus
-
Aug 11th, 2012, 10:10 PM
#7
Thread Starter
New Member
Re: Visual Basic 2010 School assignment racing car game
Ok Thanks for your help. I have set the Keypreview to TRUE and my lap counter still won't change. The car just runs off the edge of the screen and if you press the start button the keys won't move.
Thanks :
-
Aug 11th, 2012, 10:31 PM
#8
Thread Starter
New Member
Re: Visual Basic 2010 School assignment racing car game
Thanks again, The program runs perfectly as set out below, but I have to make the cars move by keystrokes manually and keep the lap count working. Can you help with altering to make this work.
Public Class Form1
Private Sub Reset()
' starting positions
picRedCar.Left = Me.Width - picRedCar.Width - 10
picBlueCar.Left = Me.Width - picBlueCar.Width - 10
lblRedLaps.Text = "0" ' reset lap count
lblBlueLaps.Text = "0"
End Sub
Private Sub tmrRace_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrRace.Tick
Dim WhichCar As Integer
Randomize()
WhichCar = Int(Rnd() * 2) '0 or 1
If WhichCar = 0 Then
picRedCar.Left -= 8
If picRedCar.Left <= -picRedCar.Width Then
picRedCar.Left = Me.Width
lblRedLaps.Text -= 1
If lblRedLaps.Text = 0 Then
tmrRace.Enabled = False ' stop race
MessageBox.Show("Red wins!", "Rally cars", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Reset()
End If
End If
Else
picBlueCar.Left -= 8
If picBlueCar.Left <= -picBlueCar.Width Then
picBlueCar.Left = Me.Width ' start next lap
lblBlueLaps.Text -= 1 ' Reduce lap count
If lblBlueLaps.Text = "0" Then
tmrRace.Enabled = False ' stop race
MessageBox.Show("Blue wins!", "Rally cars", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Reset()
End If
End If
End If
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
tmrRace.Start() ' turn on timer
btnStart.Enabled = False
End Sub
End Class
-
Aug 12th, 2012, 12:30 AM
#9
Thread Starter
New Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|