The Great Race VB Style!

Post here to discuss programming in VB.net

Moderators: Chuckt, Garth, bitfogav

Post Reply [phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

The Great Race VB Style!

Post by brad » Fri Dec 24, 2010 3:04 am

Well, VB is coming along in leaps and bounds and I am having loads of fun learning this new language. I thought what better way to learn this new language than to re-create my very first game - The Great Race!
screen-capture.png
screen-capture.png (46.13 KiB) Viewed 20543 times
Well, It is currently in a working state with one track to drive on. This version of the game gives you a running tally of the distance you have travelled, Score and also shows you your speed using a progress bar. The faster you go, the more points you get (I.E. if you go 3000 Metres at a slow speed, you get LESS points than if you went the same distance at a faster speed) It also keeps your high score (up until you quit the game)
I have also incorporated a NES control pad to show you what keys you have pressed.

The interesting thing with VB.NET is that you can't work directly with binary numbers (which is great when working with pixel games like this) Thankfully AndyO from digital-diy has been kind enough to submit a snippit of code which helps in reading individual binary digits within a variable. The Graphic Data for this game is actually in the form of decimal numbers. I made a simple excel spreadsheet which allows you to draw the track, it then converts your track data in the required decimal data to paste into the game code.

Here's a snapshot of excel helping me out!
screen-capture-1.png
screen-capture-1.png (20.13 KiB) Viewed 20542 times
The keys are:

A = Accelerate
Z = Decelerate
N = Drive Left
M = Drive Right

You can download the EXE file and play this on your PC for yourself!
TheGreatRace.zip
(73.69 KiB) Downloaded 745 times
And Finally, The Sourcecode:

Code: Select all

Imports Microsoft.VisualBasic.PowerPacks

Public Class TheGreatRace
    Dim Pixel(8, 8) As OvalShape
    Dim TrackData(153) As Byte
    Dim Car As Byte
    Dim SomethingChanged As Boolean
    Dim ButtonLeft As Boolean = False
    Dim ButtonRight As Boolean = False
    Dim ScrollOffset As Byte
    Dim Pause As Boolean = False
    Dim DistanceTravelled As Integer
    Dim Accelerator As Byte
    Dim GameSpeed As Integer
    Dim ScorePrevious As Integer
    Dim ScoreCounter As Integer
    Dim ScoreMultiplier As Byte
    Dim LapCount As Byte
    Dim NoLeft As Boolean
    Dim NoRight As Boolean
    Dim crashed As Boolean
    Dim HighScore As Integer

    Private Function GetBit(ByVal ByteVal As Byte, ByVal BitNo As Byte) As Boolean
        Return (ByteVal And (1 << BitNo)) >> BitNo
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MessageBox.Show("Welcome to the great Race! Use A / Z to Increase / Decrease Speed and N / M To drive Left / Right")
        HighScore = 0
        GameReset()
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Dim bHandled As Boolean = False
        Select Case e.KeyCode
            Case Keys.N
                ButtonLeft = True
            Case Keys.M
                ButtonRight = True
            Case Keys.A
                PictureBoxArrowUp.Visible = True
                If ProgressBarSpeed.Value <> 100 Then
                    ScoreMultiplier += 1
                    ProgressBarSpeed.Value += 10
                End If
                Accelerator -= 2
                If Accelerator < 6 Then
                    Accelerator = 6
                End If
            Case Keys.Z
                PictureBoxArrowDown.Visible = True
                If ProgressBarSpeed.Value <> 0 Then
                    ScoreMultiplier -= 1
                    ProgressBarSpeed.Value -= 10
                End If
                Accelerator += 2
                If Accelerator > 24 Then
                    Accelerator = 24
                End If
        End Select
    End Sub

    Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        Dim bHandled As Boolean = False
        Select Case e.KeyCode
            Case Keys.N
                ButtonLeft = False
                PictureBoxArrowLeft.Visible = False
            Case Keys.M
                ButtonRight = False
                PictureBoxArrowRight.Visible = False
            Case Keys.A
                PictureBoxArrowUp.Visible = False
            Case Keys.Z
                PictureBoxArrowDown.Visible = False

        End Select
    End Sub

    Private Sub TimerGraphics_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerGraphics.Tick
        UpdateGraphics()
        UpdateScore()
        CrashMessage()
        Dim x As Byte
        Dim y As Byte
        Dim Result As Boolean ' used to grab a bit in a number
        If SomethingChanged = True Then 'This ensures that we only update the graphics when something has changed (to prevent flickering)
            LabelOdometer.Text = Str(DistanceTravelled)
            For y = 1 To 8
                For x = 1 To 8
                    Result = GetBit(TrackData(y + ScrollOffset), x - 1) ' y is the byte number then x is the bit we want within that byte, I have to take one away because I need to check bit 0 first (not bit 1)
                    If Result = True Then
                        Pixel(x, y).BackColor = Color.Crimson
                    Else
                        Pixel(x, y).BackColor = Color.Black
                    End If
                    If y = 1 Then
                        Pixel(Car, y).BackColor = Color.Green
                    End If
                    If y = 1 Then
                        Result = GetBit(TrackData(y + ScrollOffset + 1), Car - 1) 'check our location to see if we have hit a wall.
                        If Result = True Then
                            crashed = True
                            Pause = True
                        End If
                        Result = GetBit(TrackData(y + ScrollOffset), Car)
                        If Result = True Then
                            NoLeft = True
                        Else
                            NoLeft = False
                        End If
                        Result = GetBit(TrackData(y + ScrollOffset), Car - 2) 'again, we need - 2 here because the car starts at 0 whereas the data starts at 1
                        If Result = True Then
                            NoRight = True
                        Else
                            NoRight = False
                        End If
                    End If
                Next
            Next
            SomethingChanged = False
        End If
    End Sub

    Private Sub GameReset()
        ProgressBarSpeed.Value = 0
        Car = 4
        SomethingChanged = True
        ButtonLeft = False
        ButtonRight = False
        ScrollOffset = 0
        Pause = False
        DistanceTravelled = 0
        Accelerator = 24
        GameSpeed = Accelerator
        ScorePrevious = 0
        ScoreCounter = 0
        ScoreMultiplier = 1
        LapCount = 0
        LoadTrackData()
        DeclarePixels()
        Dim x As Byte
        Dim y As Byte
        For x = 1 To 8
            For y = 1 To 8
                Pixel(x, y).BackColor = Color.Black
            Next
        Next
        TimerGraphics.Start()
        TimerArrowKeys.Start()
        TimerScreenScroll.Start()
        PictureBoxArrowLeft.Visible = False
        PictureBoxArrowRight.Visible = False
        PictureBoxArrowUp.Visible = False
        PictureBoxArrowDown.Visible = False
        PictureBoxButtonA.Visible = False
        PictureBoxButtonB.Visible = False
    End Sub

    Private Sub CrashMessage()
        If crashed = True Then
            crashed = False
            MessageBox.Show("You have Crashed!")
            GameReset()
        End If
    End Sub

    Private Sub UpdateScore()
        If ScoreCounter >= HighScore Then
            HighScore = ScoreCounter
        End If
        LabelHiScoreCounter.Text = Str(HighScore)
        If Pause = False Then
            LabelLapCount.Text = Str(LapCount)
            ScorePrevious = ScoreCounter
            ScoreCounter = Str(ScoreMultiplier + ScorePrevious)
            LabelScoreTally.Text = ScoreCounter
        End If
    End Sub

    Private Sub UpdateGraphics()
        GameSpeed -= 1
        If GameSpeed = 0 Then
            GameSpeed = Accelerator
            If Pause = False Then
                DistanceTravelled += 10
                SomethingChanged = True
                ScrollOffset += 1
                If ScrollOffset = 145 Then
                    ScrollOffset = 0
                    LapCount += 1
                End If
            End If
        End If
    End Sub

    Private Sub TimerArrowKeys_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerArrowKeys.Tick
        If ButtonLeft = True And Car <> 8 And NoLeft = False And Pause = False Then
            PictureBoxArrowLeft.Visible = True
            Car += 1
            SomethingChanged = True
        End If
        If ButtonRight = True And Car <> 1 And NoRight = False And Pause = False Then
            PictureBoxArrowRight.Visible = True
            Car -= 1
            SomethingChanged = True
        End If
    End Sub

Private Sub LoadTrackData()
        TrackData(1) = 129
        TrackData(2) = 129
        TrackData(3) = 129
        TrackData(4) = 129
        TrackData(5) = 129
        TrackData(6) = 129
        TrackData(7) = 129
        TrackData(8) = 129
        TrackData(9) = 195
        TrackData(10) = 199
        TrackData(11) = 207
        TrackData(12) = 207
        TrackData(13) = 199
        TrackData(14) = 227
        TrackData(15) = 227
        TrackData(16) = 225
        TrackData(17) = 241
        TrackData(18) = 241
        TrackData(19) = 249
        TrackData(20) = 249
        TrackData(21) = 241
        TrackData(22) = 241
        TrackData(23) = 225
        TrackData(24) = 225
        TrackData(25) = 227
        TrackData(26) = 199
        TrackData(27) = 207
        TrackData(28) = 207
        TrackData(29) = 199
        TrackData(30) = 227
        TrackData(31) = 227
        TrackData(32) = 193
        TrackData(33) = 193
        TrackData(34) = 129
        TrackData(35) = 129
        TrackData(36) = 145
        TrackData(37) = 153
        TrackData(38) = 185
        TrackData(39) = 185
        TrackData(40) = 185
        TrackData(41) = 185
        TrackData(42) = 187
        TrackData(43) = 155
        TrackData(44) = 147
        TrackData(45) = 147
        TrackData(46) = 131
        TrackData(47) = 131
        TrackData(48) = 135
        TrackData(49) = 199
        TrackData(50) = 231
        TrackData(51) = 231
        TrackData(52) = 231
        TrackData(53) = 231
        TrackData(54) = 227
        TrackData(55) = 225
        TrackData(56) = 225
        TrackData(57) = 193
        TrackData(58) = 129
        TrackData(59) = 129
        TrackData(60) = 129
        TrackData(61) = 137
        TrackData(62) = 157
        TrackData(63) = 157
        TrackData(64) = 189
        TrackData(65) = 189
        TrackData(66) = 189
        TrackData(67) = 157
        TrackData(68) = 137
        TrackData(69) = 137
        TrackData(70) = 129
        TrackData(71) = 193
        TrackData(72) = 225
        TrackData(73) = 227
        TrackData(74) = 247
        TrackData(75) = 247
        TrackData(76) = 247
        TrackData(77) = 231
        TrackData(78) = 231
        TrackData(79) = 195
        TrackData(80) = 193
        TrackData(81) = 129
        TrackData(82) = 129
        TrackData(83) = 129
        TrackData(84) = 161
        TrackData(85) = 177
        TrackData(86) = 185
        TrackData(87) = 185
        TrackData(88) = 177
        TrackData(89) = 161
        TrackData(90) = 131
        TrackData(91) = 135
        TrackData(92) = 143
        TrackData(93) = 159
        TrackData(94) = 143
        TrackData(95) = 135
        TrackData(96) = 131
        TrackData(97) = 195
        TrackData(98) = 193
        TrackData(99) = 225
        TrackData(100) = 241
        TrackData(101) = 249
        TrackData(102) = 241
        TrackData(103) = 225
        TrackData(104) = 193
        TrackData(105) = 193
        TrackData(106) = 131
        TrackData(107) = 131
        TrackData(108) = 135
        TrackData(109) = 143
        TrackData(110) = 159
        TrackData(111) = 191
        TrackData(112) = 191
        TrackData(113) = 191
        TrackData(114) = 191
        TrackData(115) = 191
        TrackData(116) = 191
        TrackData(117) = 191
        TrackData(118) = 191
        TrackData(119) = 191
        TrackData(120) = 191
        TrackData(121) = 191
        TrackData(122) = 159
        TrackData(123) = 143
        TrackData(124) = 135
        TrackData(125) = 195
        TrackData(126) = 227
        TrackData(127) = 225
        TrackData(128) = 225
        TrackData(129) = 193
        TrackData(130) = 131
        TrackData(131) = 135
        TrackData(132) = 159
        TrackData(133) = 143
        TrackData(134) = 135
        TrackData(135) = 195
        TrackData(136) = 225
        TrackData(137) = 241
        TrackData(138) = 241
        TrackData(139) = 249
        TrackData(140) = 253
        TrackData(141) = 253
        TrackData(142) = 249
        TrackData(143) = 241
        TrackData(144) = 225
        TrackData(145) = 193
        TrackData(146) = 129
        TrackData(147) = 129
        TrackData(148) = 129
        TrackData(149) = 129
        TrackData(150) = 129
        TrackData(151) = 129
        TrackData(152) = 129
        TrackData(153) = 129
    End Sub

    Private Sub DeclarePixels()
        Pixel(1, 1) = OvalShape0
        Pixel(2, 1) = OvalShape1
        Pixel(3, 1) = OvalShape2
        Pixel(4, 1) = OvalShape3
        Pixel(5, 1) = OvalShape4
        Pixel(6, 1) = OvalShape5
        Pixel(7, 1) = OvalShape6
        Pixel(8, 1) = OvalShape7
        Pixel(1, 2) = OvalShape8
        Pixel(2, 2) = OvalShape9
        Pixel(3, 2) = OvalShape10
        Pixel(4, 2) = OvalShape11
        Pixel(5, 2) = OvalShape12
        Pixel(6, 2) = OvalShape13
        Pixel(7, 2) = OvalShape14
        Pixel(8, 2) = OvalShape15
        Pixel(1, 3) = OvalShape16
        Pixel(2, 3) = OvalShape17
        Pixel(3, 3) = OvalShape18
        Pixel(4, 3) = OvalShape19
        Pixel(5, 3) = OvalShape20
        Pixel(6, 3) = OvalShape21
        Pixel(7, 3) = OvalShape22
        Pixel(8, 3) = OvalShape23
        Pixel(1, 4) = OvalShape24
        Pixel(2, 4) = OvalShape25
        Pixel(3, 4) = OvalShape26
        Pixel(4, 4) = OvalShape27
        Pixel(5, 4) = OvalShape28
        Pixel(6, 4) = OvalShape29
        Pixel(7, 4) = OvalShape30
        Pixel(8, 4) = OvalShape31
        Pixel(1, 5) = OvalShape32
        Pixel(2, 5) = OvalShape33
        Pixel(3, 5) = OvalShape34
        Pixel(4, 5) = OvalShape35
        Pixel(5, 5) = OvalShape36
        Pixel(6, 5) = OvalShape37
        Pixel(7, 5) = OvalShape38
        Pixel(8, 5) = OvalShape39
        Pixel(1, 6) = OvalShape40
        Pixel(2, 6) = OvalShape41
        Pixel(3, 6) = OvalShape42
        Pixel(4, 6) = OvalShape43
        Pixel(5, 6) = OvalShape44
        Pixel(6, 6) = OvalShape45
        Pixel(7, 6) = OvalShape46
        Pixel(8, 6) = OvalShape47
        Pixel(1, 7) = OvalShape48
        Pixel(2, 7) = OvalShape49
        Pixel(3, 7) = OvalShape50
        Pixel(4, 7) = OvalShape51
        Pixel(5, 7) = OvalShape52
        Pixel(6, 7) = OvalShape53
        Pixel(7, 7) = OvalShape54
        Pixel(8, 7) = OvalShape55
        Pixel(1, 8) = OvalShape56
        Pixel(2, 8) = OvalShape57
        Pixel(3, 8) = OvalShape58
        Pixel(4, 8) = OvalShape59
        Pixel(5, 8) = OvalShape60
        Pixel(6, 8) = OvalShape61
        Pixel(7, 8) = OvalShape62
        Pixel(8, 8) = OvalShape63
    End Sub
End Class

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: The Great Race VB Style!

Post by bitfogav » Sun Jan 16, 2011 3:25 am

Wow thats a great job there Brad with VB :)

Will have to have a play with this game when I get some time, looks like theres alot of changes on the site to catch up with.

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: The Great Race VB Style!

Post by brad » Sun Jan 16, 2011 6:34 am

There hasn't been a whole lot going on with the site in the past month - basically due to the server issues. I have received emails from all over the world with people telling me that they couldn't access it.

But we're back online!

VB has been fantastic - I have actually been spending the past week developing applications in VB for all the teachers to use in class to help explain concepts. It has been a great learning experience for me, I'm getting paid to learn how to use VB :D

applications I made this week were:

- Ohms' Law
OhmsLaw.png
OhmsLaw.png (594.48 KiB) Viewed 20518 times
- JK Flip Flop
JKFlipFlop.png
JKFlipFlop.png (508.76 KiB) Viewed 20518 times
- SR Flip Flop
SRFlipFlop.png
SRFlipFlop.png (525.45 KiB) Viewed 20518 times
- Diode Series Circuit
DiodeSeriesCircuit.png
DiodeSeriesCircuit.png (624.72 KiB) Viewed 20518 times
- Binary Cricket (a simple game that basically generates a random number between 0 and 6, if you get a 0 you are out and it's the next players turn, any other number you add up until you get out.) The students build this circuit for themselves.
BinaryCricket.png
BinaryCricket.png (24.89 KiB) Viewed 20518 times
- BJT Fixed Bias operation (Even plots the Qpoint for you!)
FixedBiasBJT.png
FixedBiasBJT.png (571.17 KiB) Viewed 20518 times
- Logic Gates
LogicGates.png
LogicGates.png (558.26 KiB) Viewed 20518 times

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: The Great Race VB Style!

Post by bitfogav » Mon Jan 17, 2011 1:26 am

These are some very cool app's :) they would make some great utils for the beginner, infact I would'nt mind them to use for my projects when it comes to logic gates, ohms law and working out the correct resistors. :)

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: The Great Race VB Style!

Post by bitfogav » Wed Jul 20, 2011 6:07 am

Brad, are able to share any of these Apps with poss source? :)

Post Reply
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Who is online

Users browsing this forum: No registered users and 3 guests