Visual Basic Gurus
Guest
Posts: n/a
I want the car to move automatically, left to right and I want to be able to control the blob using the keyboard.
See here for an example:
www.robster-lobster.co.uk/DodgeCar.exe
I have tried assigning it an X and Y axis like I did with the car but then I get some weird error message (see pic 2).
www.robster-lobster.co.uk/code.JPG
www.robster-lobster.co.uk/code2.JPG
See here for an example:
www.robster-lobster.co.uk/DodgeCar.exe
I have tried assigning it an X and Y axis like I did with the car but then I get some weird error message (see pic 2).
www.robster-lobster.co.uk/code.JPG
www.robster-lobster.co.uk/code2.JPG
Guest
Posts: n/a
Option Explicit On
Option Strict On
Public Class Car
Public Enum direction As Integer
Right
Left
End Enum
Private pDirection As direction = direction.Right
Private iX As Integer = 20
Private iY As Integer = 20
Private iSpeed As Integer = 40
Private iWidth As Integer = 100
Private iHeight As Integer = 100
Private cColour As Color = Color.Orange
Public Sub New()
End Sub
Public Sub drawcar(ByVal R As Graphics)
Dim bodyBrush As New SolidBrush(cColour)
Dim wheelBrush As New SolidBrush(Color.Black)
Dim windowBrush As New SolidBrush(Color.White)
Dim horizontal As Integer
Dim vertical As Integer
horizontal = iWidth \ 8
vertical = iHeight \ 5
R.FillRectangle(bodyBrush, iX, iY + 2 * vertical, horizontal * 8, vertical * 2)
R.FillEllipse(wheelBrush, iX + 1 * horizontal, iY + 3 * vertical, horizontal * 2, vertical * 2)
R.FillEllipse(wheelBrush, iX + 5 * horizontal, iY + 3 * vertical, horizontal * 2, vertical * 2)
Select Case pdirection
Case direction.Right
R.FillRectangle(bodyBrush, iX + 1 * horizontal, iY, horizontal * 5, vertical * 2)
R.FillRectangle(windowBrush, iX + 3 * horizontal, iY + 1 * vertical, horizontal * 2, vertical * 1)
Case direction.Left
R.FillRectangle(bodyBrush, iX + 2 * horizontal, iY, horizontal * 5, vertical * 2)
R.FillRectangle(windowBrush, iX + 3 * horizontal, iY + 1 * vertical, horizontal * 2, vertical * 1)
End Select
End Sub
Public Sub MoveCar(ByVal R As Graphics)
ClearCar(R)
If pDirection = direction.Right Then
iX += iSpeed
Else
iX -= iSpeed
End If
drawcar(R)
End Sub
#Region "Propertires"
Public Property X() As Integer
Get
Return iX
End Get
Set(ByVal value As Integer)
iX = value
End Set
End Property
Public Property Y() As Integer
Get
Return iY
End Get
Set(ByVal value As Integer)
iY = value
End Set
End Property
Public Property Width() As Integer
Get
Return iwidth
End Get
Set(ByVal value As Integer)
iwidth = value
End Set
End Property
Public Property Height() As Integer
Get
Return iheight
End Get
Set(ByVal value As Integer)
iheight = value
End Set
End Property
Public Property Colour() As Color
Get
Return cColour
End Get
Set(ByVal value As Color)
cColour = value
End Set
End Property
Public Property Speed() As Integer
Get
Return ispeed
End Get
Set(ByVal value As Integer)
ispeed = value
End Set
End Property
Public Property Directions() As direction
Get
Return pdirection
End Get
Set(ByVal value As direction)
pdirection = value
End Set
End Property
#End Region
Public Sub ClearCar(ByVal R As Graphics)
Dim clearBrush As New SolidBrush(Color.White)
Dim horizontal As Integer
Dim vertical As Integer
horizontal = iWidth \ 8
vertical = iHeight \ 5
R.FillRectangle(clearBrush, iX, iY + 2 * vertical, horizontal * 8, vertical * 2)
R.FillEllipse(clearBrush, iX + 1 * horizontal, iY + 3 * vertical, horizontal * 2, vertical * 2)
R.FillEllipse(clearBrush, iX + 5 * horizontal, iY + 3 * vertical, horizontal * 2, vertical * 2)
Select Case pDirection
Case direction.Right
R.FillRectangle(clearBrush, iX + 1 * horizontal, iY, horizontal * 5, vertical * 2)
Case direction.Left
R.FillRectangle(clearBrush, iX + 2 * horizontal, iY, horizontal * 5, vertical * 2)
End Select
End Sub
Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal colour As Color, ByVal speed As Integer)
iX = x
iY = y
iSpeed = speed
iHeight = height
iWidth = width
cColour = colour
End Sub
End Class
AND FOR SPIKE
Public Class Spike
'attributes
Private ix As Integer = 0
Private iy As Integer = 0
Private iSize As Integer = 20
Private cBody As Color = Color.Red
Private cFeatures As Color = Color.Black
#Region "Properties"
'properties
Public Property x() As Integer
Get
Return ix
End Get
Set(ByVal Value As Integer)
ix = Value
End Set
End Property
Public Property y() As Integer
Get
Return iy
End Get
Set(ByVal Value As Integer)
iy = Value
End Set
End Property
Public Property size() As Integer
Get
Return iSize
End Get
Set(ByVal Value As Integer)
iSize = Value
End Set
End Property
Public Property body() As Color
Get
Return cBody
End Get
Set(ByVal Value As Color)
cBody = Value
End Set
End Property
Public Property features() As Color
Get
Return cFeatures
End Get
Set(ByVal Value As Color)
cFeatures = Value
End Set
End Property
Public ReadOnly Property width() As Integer
Get
Return iSize * 1.5
End Get
End Property
#End Region
'constructor methods
Public Sub New()
End Sub
Public Sub New(ByVal x As Integer, _
ByVal y As Integer, _
ByVal size As Integer, _
ByVal body As Color, _
ByVal features As Color)
ix = x
iy = y
iSize = size
cBody = body
cFeatures = features
End Sub
'other methods
'draw spike
Public Sub Draw(ByVal spikeArea As Graphics)
Dim bodyPen As Pen
Dim bodyBrush As SolidBrush
Dim featuresBrush As SolidBrush
Dim iHalfsize As Integer = iSize \ 2
Dim iWidth As Integer = iSize * 1.5
Dim iArm As Integer = iSize \ 4
Dim iFeature As Integer = iSize \ 5
Dim iMouthX As Integer = iArm + iFeature * 1.5
Dim iMouthY As Integer = iFeature * 3.25
Dim iLeg As Integer = iSize \ 10
bodyPen = New Pen(cBody, iLeg)
bodyBrush = New SolidBrush(cBody)
featuresBrush = New SolidBrush(cFeatures)
spikeArea.FillEllipse(bodyBrush, ix + iArm, iy, iSize, iSize)
spikeArea.DrawLine(bodyPen, ix, iy, ix + iWidth, iy + iSize)
spikeArea.DrawLine(bodyPen, ix, iy + iHalfsize, ix + iWidth, iy + iHalfsize)
spikeArea.DrawLine(bodyPen, ix, iy + iSize, ix + iWidth, iy)
spikeArea.FillEllipse(featuresBrush, ix + iArm + iFeature, iy + iArm, iFeature, iFeature)
spikeArea.FillEllipse(featuresBrush, ix + iArm + iFeature * 3, iy + iArm, iFeature, iFeature)
spikeArea.FillRectangle(featuresBrush, ix + iMouthX, iy + iMouthY, iFeature * 2, iFeature \ 2)
End Sub
'clear spike
Public Sub Clear(ByVal spikeArea As Graphics)
Dim clearPen As Pen
Dim clearBrush As SolidBrush
Dim iHalfsize As Integer = iSize \ 2
Dim iWidth As Integer = iSize * 1.5
Dim iArm As Integer = iSize \ 4
Dim iLeg As Integer = iSize \ 10
clearPen = New Pen(Color.White, iLeg)
clearBrush = New SolidBrush(Color.White)
spikeArea.FillEllipse(clearBrush, ix + iArm, iy, iSize, iSize)
spikeArea.DrawLine(clearPen, ix, iy, ix + iWidth, iy + iSize)
spikeArea.DrawLine(clearPen, ix, iy + iHalfsize, ix + iWidth, iy + iHalfsize)
spikeArea.DrawLine(clearPen, ix, iy + iSize, ix + iWidth, iy)
End Sub
End Class
As far as I'm aware, all the code for car.vb and spike is correct.
Option Strict On
Public Class Car
Public Enum direction As Integer
Right
Left
End Enum
Private pDirection As direction = direction.Right
Private iX As Integer = 20
Private iY As Integer = 20
Private iSpeed As Integer = 40
Private iWidth As Integer = 100
Private iHeight As Integer = 100
Private cColour As Color = Color.Orange
Public Sub New()
End Sub
Public Sub drawcar(ByVal R As Graphics)
Dim bodyBrush As New SolidBrush(cColour)
Dim wheelBrush As New SolidBrush(Color.Black)
Dim windowBrush As New SolidBrush(Color.White)
Dim horizontal As Integer
Dim vertical As Integer
horizontal = iWidth \ 8
vertical = iHeight \ 5
R.FillRectangle(bodyBrush, iX, iY + 2 * vertical, horizontal * 8, vertical * 2)
R.FillEllipse(wheelBrush, iX + 1 * horizontal, iY + 3 * vertical, horizontal * 2, vertical * 2)
R.FillEllipse(wheelBrush, iX + 5 * horizontal, iY + 3 * vertical, horizontal * 2, vertical * 2)
Select Case pdirection
Case direction.Right
R.FillRectangle(bodyBrush, iX + 1 * horizontal, iY, horizontal * 5, vertical * 2)
R.FillRectangle(windowBrush, iX + 3 * horizontal, iY + 1 * vertical, horizontal * 2, vertical * 1)
Case direction.Left
R.FillRectangle(bodyBrush, iX + 2 * horizontal, iY, horizontal * 5, vertical * 2)
R.FillRectangle(windowBrush, iX + 3 * horizontal, iY + 1 * vertical, horizontal * 2, vertical * 1)
End Select
End Sub
Public Sub MoveCar(ByVal R As Graphics)
ClearCar(R)
If pDirection = direction.Right Then
iX += iSpeed
Else
iX -= iSpeed
End If
drawcar(R)
End Sub
#Region "Propertires"
Public Property X() As Integer
Get
Return iX
End Get
Set(ByVal value As Integer)
iX = value
End Set
End Property
Public Property Y() As Integer
Get
Return iY
End Get
Set(ByVal value As Integer)
iY = value
End Set
End Property
Public Property Width() As Integer
Get
Return iwidth
End Get
Set(ByVal value As Integer)
iwidth = value
End Set
End Property
Public Property Height() As Integer
Get
Return iheight
End Get
Set(ByVal value As Integer)
iheight = value
End Set
End Property
Public Property Colour() As Color
Get
Return cColour
End Get
Set(ByVal value As Color)
cColour = value
End Set
End Property
Public Property Speed() As Integer
Get
Return ispeed
End Get
Set(ByVal value As Integer)
ispeed = value
End Set
End Property
Public Property Directions() As direction
Get
Return pdirection
End Get
Set(ByVal value As direction)
pdirection = value
End Set
End Property
#End Region
Public Sub ClearCar(ByVal R As Graphics)
Dim clearBrush As New SolidBrush(Color.White)
Dim horizontal As Integer
Dim vertical As Integer
horizontal = iWidth \ 8
vertical = iHeight \ 5
R.FillRectangle(clearBrush, iX, iY + 2 * vertical, horizontal * 8, vertical * 2)
R.FillEllipse(clearBrush, iX + 1 * horizontal, iY + 3 * vertical, horizontal * 2, vertical * 2)
R.FillEllipse(clearBrush, iX + 5 * horizontal, iY + 3 * vertical, horizontal * 2, vertical * 2)
Select Case pDirection
Case direction.Right
R.FillRectangle(clearBrush, iX + 1 * horizontal, iY, horizontal * 5, vertical * 2)
Case direction.Left
R.FillRectangle(clearBrush, iX + 2 * horizontal, iY, horizontal * 5, vertical * 2)
End Select
End Sub
Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal colour As Color, ByVal speed As Integer)
iX = x
iY = y
iSpeed = speed
iHeight = height
iWidth = width
cColour = colour
End Sub
End Class
AND FOR SPIKE
Public Class Spike
'attributes
Private ix As Integer = 0
Private iy As Integer = 0
Private iSize As Integer = 20
Private cBody As Color = Color.Red
Private cFeatures As Color = Color.Black
#Region "Properties"
'properties
Public Property x() As Integer
Get
Return ix
End Get
Set(ByVal Value As Integer)
ix = Value
End Set
End Property
Public Property y() As Integer
Get
Return iy
End Get
Set(ByVal Value As Integer)
iy = Value
End Set
End Property
Public Property size() As Integer
Get
Return iSize
End Get
Set(ByVal Value As Integer)
iSize = Value
End Set
End Property
Public Property body() As Color
Get
Return cBody
End Get
Set(ByVal Value As Color)
cBody = Value
End Set
End Property
Public Property features() As Color
Get
Return cFeatures
End Get
Set(ByVal Value As Color)
cFeatures = Value
End Set
End Property
Public ReadOnly Property width() As Integer
Get
Return iSize * 1.5
End Get
End Property
#End Region
'constructor methods
Public Sub New()
End Sub
Public Sub New(ByVal x As Integer, _
ByVal y As Integer, _
ByVal size As Integer, _
ByVal body As Color, _
ByVal features As Color)
ix = x
iy = y
iSize = size
cBody = body
cFeatures = features
End Sub
'other methods
'draw spike
Public Sub Draw(ByVal spikeArea As Graphics)
Dim bodyPen As Pen
Dim bodyBrush As SolidBrush
Dim featuresBrush As SolidBrush
Dim iHalfsize As Integer = iSize \ 2
Dim iWidth As Integer = iSize * 1.5
Dim iArm As Integer = iSize \ 4
Dim iFeature As Integer = iSize \ 5
Dim iMouthX As Integer = iArm + iFeature * 1.5
Dim iMouthY As Integer = iFeature * 3.25
Dim iLeg As Integer = iSize \ 10
bodyPen = New Pen(cBody, iLeg)
bodyBrush = New SolidBrush(cBody)
featuresBrush = New SolidBrush(cFeatures)
spikeArea.FillEllipse(bodyBrush, ix + iArm, iy, iSize, iSize)
spikeArea.DrawLine(bodyPen, ix, iy, ix + iWidth, iy + iSize)
spikeArea.DrawLine(bodyPen, ix, iy + iHalfsize, ix + iWidth, iy + iHalfsize)
spikeArea.DrawLine(bodyPen, ix, iy + iSize, ix + iWidth, iy)
spikeArea.FillEllipse(featuresBrush, ix + iArm + iFeature, iy + iArm, iFeature, iFeature)
spikeArea.FillEllipse(featuresBrush, ix + iArm + iFeature * 3, iy + iArm, iFeature, iFeature)
spikeArea.FillRectangle(featuresBrush, ix + iMouthX, iy + iMouthY, iFeature * 2, iFeature \ 2)
End Sub
'clear spike
Public Sub Clear(ByVal spikeArea As Graphics)
Dim clearPen As Pen
Dim clearBrush As SolidBrush
Dim iHalfsize As Integer = iSize \ 2
Dim iWidth As Integer = iSize * 1.5
Dim iArm As Integer = iSize \ 4
Dim iLeg As Integer = iSize \ 10
clearPen = New Pen(Color.White, iLeg)
clearBrush = New SolidBrush(Color.White)
spikeArea.FillEllipse(clearBrush, ix + iArm, iy, iSize, iSize)
spikeArea.DrawLine(clearPen, ix, iy, ix + iWidth, iy + iSize)
spikeArea.DrawLine(clearPen, ix, iy + iHalfsize, ix + iWidth, iy + iHalfsize)
spikeArea.DrawLine(clearPen, ix, iy + iSize, ix + iWidth, iy)
End Sub
End Class
As far as I'm aware, all the code for car.vb and spike is correct.
Trending Topics
Guest
Posts: n/a
If you manage to do this I'll never say a bad thing about you again
www.robster-lobster.co.uk/DodgeCar.rar
Cheers, all help apprecaited
www.robster-lobster.co.uk/DodgeCar.rar
Cheers, all help apprecaited
Originally Posted by Gary_R
If you manage to do this I'll never say a bad thing about you again
www.robster-lobster.co.uk/DodgeCar.rar
Cheers, all help apprecaited

www.robster-lobster.co.uk/DodgeCar.rar
Cheers, all help apprecaited

Guest
Posts: n/a
Can't beleive your lecturer has not obfuscated the example program!!LOL
Reflector is a handy program for decompiling .NET exe's:
http://www.aisto.com/roeder/dotnet/
Its actually a very easy what they want you to do - all the difficult bits have been done in the classes! Took me a minute to get the car going scrolling in both directions across the screen!
I give you some pointers:
1. Drag a timer control onto form1 and then double click it in order for the event stub to appear
2. Delete existing code and Paste this into form1
Public Class Form1
Private draw As New Spike
Private newcar As New Car(20, 20, 100, 50, Color.Orange, 40)
Private drawarea As Graphics
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
drawarea = picArea.CreateGraphics
draw = New Spike(picArea.Width / 1 - 10, picArea.Height / 1 - 10, 20, Color.Red, Color.Black)
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
btnStart.Enabled = False
newcar.drawcar(drawarea)
draw.Draw(drawarea)
Timer1.Enabled = True
Timer1.Interval = 100 'change this for different speed
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If newcar.X = picArea.Size.Width Then 'not perfect but will do.....
newcar.Directions = Car.direction.Left
End If
If newcar.X <= 0 Then 'not perfect but will do.....
newcar.Directions = Car.direction.Right
End If
newcar.MoveCar(drawarea)
'you now need some code here to see if spike (draw) and Car (newcar) have hasCollided
'do this by looking at the x, y and width of spike and car.
End Sub
End Class
3. A clue to finish off the keyboard stuff....
keypress event on form1
Dan
Reflector is a handy program for decompiling .NET exe's:
http://www.aisto.com/roeder/dotnet/
Its actually a very easy what they want you to do - all the difficult bits have been done in the classes! Took me a minute to get the car going scrolling in both directions across the screen!
I give you some pointers:
1. Drag a timer control onto form1 and then double click it in order for the event stub to appear
2. Delete existing code and Paste this into form1
Public Class Form1
Private draw As New Spike
Private newcar As New Car(20, 20, 100, 50, Color.Orange, 40)
Private drawarea As Graphics
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
drawarea = picArea.CreateGraphics
draw = New Spike(picArea.Width / 1 - 10, picArea.Height / 1 - 10, 20, Color.Red, Color.Black)
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
btnStart.Enabled = False
newcar.drawcar(drawarea)
draw.Draw(drawarea)
Timer1.Enabled = True
Timer1.Interval = 100 'change this for different speed
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If newcar.X = picArea.Size.Width Then 'not perfect but will do.....
newcar.Directions = Car.direction.Left
End If
If newcar.X <= 0 Then 'not perfect but will do.....
newcar.Directions = Car.direction.Right
End If
newcar.MoveCar(drawarea)
'you now need some code here to see if spike (draw) and Car (newcar) have hasCollided
'do this by looking at the x, y and width of spike and car.
End Sub
End Class
3. A clue to finish off the keyboard stuff....
keypress event on form1
Dan
20K+ Super Poster.
Joined: May 2003
Posts: 20,599
Likes: 0
From: Ramsgate, Kent Drives: E39 530D Touring
the thing about the VB they teach you for school is this... how many times have you been at work and thought "damn, i know what i need to solve this problem, a VB app witha car that moves and a blob i can control"
Thread
Thread Starter
Forum
Replies
Last Post
massivewangers
General Car Related Discussion.
30
Oct 1, 2015 07:40 PM




