'Class module
Sub Class_Globals
Public gv As GameView
Public pad As GamePad
Private mainTimer As Timer
Private fps As Float
fps = 30 'this value is not really important. It is just an approximate used when starting t
Private ldoggieTime As Long
Private lblFPS As Label
Public lblAngle As Label
Public Dogs As List
Private DogsBitmaps(,) As BitmapData
Public leroyBitmaps(,) As BitmapData
Private CarsBitmaps(,) As BitmapData
Public Cars As List
Public MyLeroy As Leroy
Public streetBackground As BitmapData
Private sounds As SoundPool
---> Public shotSoundId, breakSoundId, crashSoundId As Int
---> Public backgroundMusic, sirenSFX, whistleSFX As Int
Public backgroundScale As Int : backgroundScale = 1
End Sub
Public Sub Initialize (vGV As GameView, vPad As GamePad, vlblFPS As Label, vlblAngle As Label)
GameUtils.init
gv = vGV
pad = vPad
lblFPS = vlblFPS
lblAngle = vlblAngle
mainTimer.Initialize("MainTimer", 15)
streetBackground = CreateBackground
gv.BitmapsData.Add(streetBackground)
DogsBitmaps = GameUtils.LoadSpritesFromSheet(LoadBitmap(File.DirAssets, "dog_spritesheet.png"), 6, 2)
Dogs.Initialize
CarsBitmaps = GameUtils.LoadSpritesFromSheet(LoadBitmap(File.DirAssets, "car_spritesheet.png"), 6, 1)
Cars.Initialize
Dim bd As BitmapData
leroyBitmaps = GameUtils.LoadSpritesFromSheet(LoadBitmap(File.DirAssets, "leroy_spritesheet.png"), 6, 1)
AddLeroy (50%x, 50%y, _
50 * GameUtils.scale)
'sounds are loaded to a SoundPool
sounds.Initialize(4)
shotSoundId = sounds.Load(File.DirAssets, "shotgun.mp3")
breakSoundId = sounds.Load(File.DirAssets, "break.mp3")
---> crashSoundId = sounds.Load(File.DirAssets, "crash.mp3")
backgroundMusic = sounds.Load(File.DirAssets, "music.mp3")
---> sirenSFX = sounds.Load(File.DirAssets, "siren.mp3")
whistleSFX = sounds.Load(File.DirAssets, "whistle.mp3")
'add the game pad
pad.Initialize(gv)
Dim length, rightPadding, leftPadding, bottomPadding As Int
length = 100 * GameUtils.scale 'also the panel's height
rightPadding = 50 * GameUtils.scale
leftPadding = 20 * GameUtils.scale
bottomPadding = 20 * GameUtils.scale
pad.AddToGameView (gv, 50%x - (length/2), 100%y - bottomPadding - length, _
100%x - rightPadding - leftPadding, length)
Main.padX = 50%x
Main.padY = 100%y - bottomPadding - length + (length/2)
StartGame
End Sub
Private Sub StartGame
'Clear the previous remaining dogs.
For i = 0 To Dogs.Size - 1
Dim doggie As Dog
doggie = Dogs.Get(i)
doggie.RemoveFromGameView 'This method will remove the Dog from the GameView
Next
Dogs.Clear
'Create 4 new Dogs
For i = 1 To 4
AddDog (Rnd(1%x, 99%x), Rnd(1%y, 99%y), _
30 * GameUtils.scale)
Next
'Clear the previous remaining cars.
For i = 0 To Cars.Size - 1
Dim vehicle As Car
vehicle = Cars.Get(i)
vehicle.RemoveFromGameView 'This method will remove the Dog from the GameView
Next
Cars.Clear
'Create 4 new Cars
Dim dir As Int 'set direction of car
dir = 0
For i = 1 To 4
If i > 2 Then
dir = 1
End If
AddCar (5%x + (i * 15%x), Rnd(1%y, 99%y), _
40 * GameUtils.scale, dir)
Next
'start music
---> PlaySound(sirenSFX, 1)
End Sub
.
.
.
.
.
Public Sub StartTimer
mainTimer.Enabled = True
End Sub
Public Sub StopTimer
mainTimer.Enabled = False
End Sub
Private Sub mainTimer_Tick
'calculate the frames per second
fps = (1000 / Max(10, (DateTime.Now - ldoggieTime)) + 20 * fps) / 21
lblFPS.Text = NumberFormat(fps, 0, 0)
ldoggieTime = DateTime.Now
Dim doggie As Dog
Dim i As Int
.
.
.
.
.
'mark the GameView as "dirty". This will cause it to redraw itself.
gv.Invalidate
End Sub
Public Sub PlaySound(sound As Int, volume As Float)
sounds.Play(sound, volume, volume, 0, 0, 1)
End Sub
'Load street into background
Private Sub CreateBackground As BitmapData
'The background is created manually with a bitmap and canvas.
Dim bd As BitmapData
bd.Bitmap.InitializeMutable(100%x / backgroundScale, 100%y / backgroundScale) 'create a wide image
bd.Bitmap = LoadBitmap(File.DirAssets, "streetmap.png")
bd.srcRect.Initialize(0, 0, bd.Bitmap.Width, 62%y)
bd.destRect.Initialize(0, 8%y, 100%x, 70%y)
Return bd
End Sub