Results 1 to 9 of 9

Thread: Visual Basic 2010 School assignment racing car game

  1. #1
    New Member
    Join Date
    Aug 12
    Posts
    5

    Wink 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

  2. #2
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,957

    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.

  3. #3
    New Member
    Join Date
    Aug 12
    Posts
    5

    Wink 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

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,564

    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

  5. #5
    Frenzied Member Evil_Giraffe's Avatar
    Join Date
    Aug 02
    Location
    Suffolk, UK
    Posts
    1,880

    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.

  6. #6
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,957

    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

  7. #7
    New Member
    Join Date
    Aug 12
    Posts
    5

    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:

  8. #8
    New Member
    Join Date
    Aug 12
    Posts
    5

    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

  9. #9
    New Member
    Join Date
    Aug 12
    Posts
    5

    Re: Visual Basic 2010 School assignment racing car game

    thanks everyone for your help, ive got it all working

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •