Introduction: Making a Simple Game in MS VisualBasic 6
hi everyone,
today i was thinking about posting an instructable and had no idea what to post.
finally i decided to write a simple game like City Bloxx on mobile phones.(but very basic)
//This instructable assumes that you know the basics of writing a program in VB//
//BTW I've uploaded the final project//
THEORY:
Dropping the house blocks on top of each other to build an apartment.
at first look at the game, you see simply hitting button 5 and the block drops and lands on top of the last. But if you look correctly you'll find out how the game exactly works.
Isn't simple? so why not to make your own?
over to the next step...
today i was thinking about posting an instructable and had no idea what to post.
finally i decided to write a simple game like City Bloxx on mobile phones.(but very basic)
//This instructable assumes that you know the basics of writing a program in VB//
//BTW I've uploaded the final project//
THEORY:
Dropping the house blocks on top of each other to build an apartment.
at first look at the game, you see simply hitting button 5 and the block drops and lands on top of the last. But if you look correctly you'll find out how the game exactly works.
Isn't simple? so why not to make your own?
over to the next step...
Step 1: Graphics
Alright, now that you've made your own rules, open MSPaint and start designing a house block.( it must be small.). and make sure there are no empty fields around it. the shape must fit the paper... now draw the final block that stands on top of the building(usually shorter than other ones but width is the same as the width of house block).
Step 2: Objects and Animations
So you see that there is a block swinging on top and when you hit 5 a COPY of it will fall down. now how to reach that? how to create objects in run-time? the answer is using Object Arrays.
you can create control arrays with most of the controls in VB6.
first add these controls.
A Label named lbTotal
A Label named lbCorrect
A Label named lbWrong
A Label named lbRemain
A Command Button named "cmdU" - Visible : False
A Command Button named "cmdD"- Visible : False
//labels are screaming their function and buttons are to scroll the building in the End.
Creating the Array (For the Blocks)
1. If you're using GIF images that have Transparency use Image control. if not, use PictureBox. (I've used PictureBox here)
2. Rename it to "Block"
3. Set AutoSize property to True
4. Set BorderStyle property to None
5. Set Index property to '0' ***
6. click on Picture property then click on the button on the far right. Find your House block image and hit open.
7. Move it to the Top of the Form
*** that is the property that tells vb this is a control array. now if you copy and paste it you won't see the message saying "Do you want to create a control array..."
Making the Block Swinging Animation:
to do this, add a Timer Control. with the name of : "CCMove" (Crane Cable Move!) and the interval value of '25'. it must be Enabled by default.
but first create a Shape as the Ground level and put it down on the form. Name it "BaseBlock" (The swinging Block works with the dimensions of this object)
now double-click on the timer you created and write this piece of code in Timer event.
If rev Then
'go right
Block(0).Left = Block(0).Left + 100
If Block(0).Left > BaseBlock.Left + BaseBlock.Width - (Block(0).Width \ 2) Then rev = False
Else
'go left
Block(0).Left = Block(0).Left - 100
If Block(0).Left < BaseBlock.Left - (Block(0).Width \ 2) Then rev = True
End If
now go to the General - Declarations section and type this:
Dim rev as Boolean
this will declare a variable named rev in type of Boolean. this variable is used in CCMove Timer to switch the movement direction.
Now hit 'Play' to run the project and test the swinging animation.
>>What does the Timer do?
It moves the block 0 in a direction until it reaches the limitation you set for then switches the direction. the limitation here is based on "BaseBlock" object with the offset of 1/2 block width from left and right.
you can create control arrays with most of the controls in VB6.
first add these controls.
A Label named lbTotal
A Label named lbCorrect
A Label named lbWrong
A Label named lbRemain
A Command Button named "cmdU" - Visible : False
A Command Button named "cmdD"- Visible : False
//labels are screaming their function and buttons are to scroll the building in the End.
Creating the Array (For the Blocks)
1. If you're using GIF images that have Transparency use Image control. if not, use PictureBox. (I've used PictureBox here)
2. Rename it to "Block"
3. Set AutoSize property to True
4. Set BorderStyle property to None
5. Set Index property to '0' ***
6. click on Picture property then click on the button on the far right. Find your House block image and hit open.
7. Move it to the Top of the Form
*** that is the property that tells vb this is a control array. now if you copy and paste it you won't see the message saying "Do you want to create a control array..."
Making the Block Swinging Animation:
to do this, add a Timer Control. with the name of : "CCMove" (Crane Cable Move!) and the interval value of '25'. it must be Enabled by default.
but first create a Shape as the Ground level and put it down on the form. Name it "BaseBlock" (The swinging Block works with the dimensions of this object)
now double-click on the timer you created and write this piece of code in Timer event.
If rev Then
'go right
Block(0).Left = Block(0).Left + 100
If Block(0).Left > BaseBlock.Left + BaseBlock.Width - (Block(0).Width \ 2) Then rev = False
Else
'go left
Block(0).Left = Block(0).Left - 100
If Block(0).Left < BaseBlock.Left - (Block(0).Width \ 2) Then rev = True
End If
now go to the General - Declarations section and type this:
Dim rev as Boolean
this will declare a variable named rev in type of Boolean. this variable is used in CCMove Timer to switch the movement direction.
Now hit 'Play' to run the project and test the swinging animation.
>>What does the Timer do?
It moves the block 0 in a direction until it reaches the limitation you set for then switches the direction. the limitation here is based on "BaseBlock" object with the offset of 1/2 block width from left and right.
Step 3: Main Coding
as you guessed it, dropping animation needs a timer too. but first you need to declare the variables needed.
-Open your Code Editor and go to General - Declarations Section and type following code:
Dim CBLOCK As Byte, BAlive(255) As Boolean, LastLand As Byte, Lastone as Boolean
Dim TWrong As Byte, TCorrect As Byte, TBlocks As Byte, TRemain As Byte, Goal As Byte
'//Note that a Byte type variable can only hold 0 to 255.
CBLOCK : Holds the current block index number.
BAlive(255) : The array for dead(missed) blocks. because maximum value of CBLOCK is 255 the range of the array is set 255.
LastLand : Holds the index on the last block landed correctly.(Alive)
Lastone : wont let you drop more blocks if true.
T(Wrong/Correct/Blocks/Remain) : Clearly visible what are they used for. the 'T' stands for "Total".
Goal : how many blocks to drop.
-now we have to create a Sub to ease the dropping process and avoid bugs.
-Again go to General - Declarations section and type this:
Sub DropBlock()
'if a block is still dropping, stop the procedure
If BlockLever.Enabled = True Or Lastone = True Then Exit Sub
If TRemain - 1 = 0 Then
'stop crane
CCMove.Enabled = False
'hide mother block
Block(0).Visible = False
Lastone = True
Call Annoy(3)
End If
'load a new one
CBLOCK = CBLOCK + 1
Load Block(CBLOCK) 'load block
Block(CBLOCK).ZOrder
Block(CBLOCK).Visible = True
BlockLever.Enabled = True
'every 2 correct blocks, screen goes up
'If CBLOCK Mod 2 = 0 Then ScreenAnim.Enabled = True
'set total
TBlocks = CBLOCK
lbTotal.Caption = TBlocks
'show remaining
TRemain = Goal - TBlocks
lbRemain.Caption = TRemain
End Sub
i've commented the actions of the lines following it.
//Note on 'Block(CBLOCK).ZOrder' : in Control Arrays, using ZOrder will bring the control to front (On top of the others).
-Still one thing is remaining. The Annoy system!. this is so simple cuz it just shows some message.
-Create a Label and name it "PLand_T" - no idea for the name!! - use a Large font for it and set the alignment on Center for better interface.
-Create a Timer and name it "PLand" - Enabled : False - Interval : 1200
Double-Click on it and type the following cod for it:
PLand_T.Visible = False
PLand.Enabled = False
** Yeah! it just dissapears the message after 1.2 seconds
okay. to control this we'll add a Sub called Annoy with one argument that specifies the message ID. this will ease the process and takes smaller place to call. because you wrote the main code before and call it whenever you want.
- in Code Editor, the General - Declarations section add this code:
Select Case mID
Case 0
PLand_T.ForeColor = vbBlack
PLand_T.Caption = "Get Ready!"
Case 1
PLand_T.ForeColor = vbBlue
PLand_T.Caption = "Perfect Landing!"
Case 2
PLand_T.ForeColor = vbRed
PLand_T.Caption = "MISSED!"
Case 3
PLand_T.ForeColor = vbBlack
PLand_T.Caption = "That was the Last one!"
cmdU.Visible = True
cmdD.Visible = True
End Select
PLand_T.Visible = True
PLand.Enabled = True
//so the Message system is good to go!
-Now get back on your Form and insert a Timer. Name it "BlockLever" - Enabled : False - Interval : 25
This timer won't only move the block, it mainly runs the rules of the game. so it's the critical part.
Double-Click on it and type the following code: (Actions Commented) you must type the correct addresses in the indicated places
Block(CBLOCK).Top = Block(CBLOCK).Top + 150
If CBLOCK = 1 Then 'first block exception
If Block(CBLOCK).Top + Block(CBLOCK).Height >= BaseBlock.Top Then
BlockLever.Enabled = False
BAlive(CBLOCK) = True
LastLand = 1 'well, even a fool can land this one
'annoy the correct
TCorrect = TCorrect + 1
lbCorrect.Caption = TCorrect
End If
Else
If Block(CBLOCK).Top + Block(CBLOCK).Height >= Block(LastLand).Top - 150 And Block(CBLOCK).Left > Block(LastLand).Left - (Block(CBLOCK).Width \ 2) And Block(CBLOCK).Left < Block(LastLand).Left + Block(LastLand).Width - (Block(CBLOCK).Width \ 2) Then
BlockLever.Enabled = False
BAlive(CBLOCK) = True 'set this index as Alive(Correctly landed)
'dock to correct y position
Block(CBLOCK).Top = Block(LastLand).Top - Block(CBLOCK).Height
'dock to correct x position on low differece
If Block(CBLOCK).Left > Block(LastLand).Left - 200 And Block(CBLOCK).Left < Block(LastLand).Left + 200 Then
Block(CBLOCK).Left = Block(LastLand).Left
'Perfect Land!
Call Annoy(1)
End If
LastLand = CBLOCK
'add the correct
TCorrect = TCorrect + 1
lbCorrect.Caption = TCorrect
'every 2 correct blocks, screen goes up
If TCorrect Mod 2 = 0 Then ScreenAnim.Enabled = True
'last block
If TRemain = 1 Then
Block(0).Picture = LoadPicture("<The TOP Block address here>") 'set the picture
End If
End If
End If
'if went out of the form
If Block(CBLOCK).Top > Me.Height Then
BlockLever.Enabled = False 'stop the lever
BAlive(CBLOCK) = False 'this block is dead
TWrong = TWrong + 1 'increment the wrongs
lbWrong.Caption = TWrong 'display it
'last block
If TRemain = 1 Then
Block(0).Picture = LoadPicture("<The TOP Block address here>") 'set the picture
End If
Call Annoy(2) 'display the MISSED message
End If
-Open your Code Editor and go to General - Declarations Section and type following code:
Dim CBLOCK As Byte, BAlive(255) As Boolean, LastLand As Byte, Lastone as Boolean
Dim TWrong As Byte, TCorrect As Byte, TBlocks As Byte, TRemain As Byte, Goal As Byte
'//Note that a Byte type variable can only hold 0 to 255.
CBLOCK : Holds the current block index number.
BAlive(255) : The array for dead(missed) blocks. because maximum value of CBLOCK is 255 the range of the array is set 255.
LastLand : Holds the index on the last block landed correctly.(Alive)
Lastone : wont let you drop more blocks if true.
T(Wrong/Correct/Blocks/Remain) : Clearly visible what are they used for. the 'T' stands for "Total".
Goal : how many blocks to drop.
-now we have to create a Sub to ease the dropping process and avoid bugs.
-Again go to General - Declarations section and type this:
Sub DropBlock()
'if a block is still dropping, stop the procedure
If BlockLever.Enabled = True Or Lastone = True Then Exit Sub
If TRemain - 1 = 0 Then
'stop crane
CCMove.Enabled = False
'hide mother block
Block(0).Visible = False
Lastone = True
Call Annoy(3)
End If
'load a new one
CBLOCK = CBLOCK + 1
Load Block(CBLOCK) 'load block
Block(CBLOCK).ZOrder
Block(CBLOCK).Visible = True
BlockLever.Enabled = True
'every 2 correct blocks, screen goes up
'If CBLOCK Mod 2 = 0 Then ScreenAnim.Enabled = True
'set total
TBlocks = CBLOCK
lbTotal.Caption = TBlocks
'show remaining
TRemain = Goal - TBlocks
lbRemain.Caption = TRemain
End Sub
i've commented the actions of the lines following it.
//Note on 'Block(CBLOCK).ZOrder' : in Control Arrays, using ZOrder will bring the control to front (On top of the others).
-Still one thing is remaining. The Annoy system!. this is so simple cuz it just shows some message.
-Create a Label and name it "PLand_T" - no idea for the name!! - use a Large font for it and set the alignment on Center for better interface.
-Create a Timer and name it "PLand" - Enabled : False - Interval : 1200
Double-Click on it and type the following cod for it:
PLand_T.Visible = False
PLand.Enabled = False
** Yeah! it just dissapears the message after 1.2 seconds
okay. to control this we'll add a Sub called Annoy with one argument that specifies the message ID. this will ease the process and takes smaller place to call. because you wrote the main code before and call it whenever you want.
- in Code Editor, the General - Declarations section add this code:
Select Case mID
Case 0
PLand_T.ForeColor = vbBlack
PLand_T.Caption = "Get Ready!"
Case 1
PLand_T.ForeColor = vbBlue
PLand_T.Caption = "Perfect Landing!"
Case 2
PLand_T.ForeColor = vbRed
PLand_T.Caption = "MISSED!"
Case 3
PLand_T.ForeColor = vbBlack
PLand_T.Caption = "That was the Last one!"
cmdU.Visible = True
cmdD.Visible = True
End Select
PLand_T.Visible = True
PLand.Enabled = True
//so the Message system is good to go!
-Now get back on your Form and insert a Timer. Name it "BlockLever" - Enabled : False - Interval : 25
This timer won't only move the block, it mainly runs the rules of the game. so it's the critical part.
Double-Click on it and type the following code: (Actions Commented) you must type the correct addresses in the indicated places
Block(CBLOCK).Top = Block(CBLOCK).Top + 150
If CBLOCK = 1 Then 'first block exception
If Block(CBLOCK).Top + Block(CBLOCK).Height >= BaseBlock.Top Then
BlockLever.Enabled = False
BAlive(CBLOCK) = True
LastLand = 1 'well, even a fool can land this one
'annoy the correct
TCorrect = TCorrect + 1
lbCorrect.Caption = TCorrect
End If
Else
If Block(CBLOCK).Top + Block(CBLOCK).Height >= Block(LastLand).Top - 150 And Block(CBLOCK).Left > Block(LastLand).Left - (Block(CBLOCK).Width \ 2) And Block(CBLOCK).Left < Block(LastLand).Left + Block(LastLand).Width - (Block(CBLOCK).Width \ 2) Then
BlockLever.Enabled = False
BAlive(CBLOCK) = True 'set this index as Alive(Correctly landed)
'dock to correct y position
Block(CBLOCK).Top = Block(LastLand).Top - Block(CBLOCK).Height
'dock to correct x position on low differece
If Block(CBLOCK).Left > Block(LastLand).Left - 200 And Block(CBLOCK).Left < Block(LastLand).Left + 200 Then
Block(CBLOCK).Left = Block(LastLand).Left
'Perfect Land!
Call Annoy(1)
End If
LastLand = CBLOCK
'add the correct
TCorrect = TCorrect + 1
lbCorrect.Caption = TCorrect
'every 2 correct blocks, screen goes up
If TCorrect Mod 2 = 0 Then ScreenAnim.Enabled = True
'last block
If TRemain = 1 Then
Block(0).Picture = LoadPicture("<The TOP Block address here>") 'set the picture
End If
End If
End If
'if went out of the form
If Block(CBLOCK).Top > Me.Height Then
BlockLever.Enabled = False 'stop the lever
BAlive(CBLOCK) = False 'this block is dead
TWrong = TWrong + 1 'increment the wrongs
lbWrong.Caption = TWrong 'display it
'last block
If TRemain = 1 Then
Block(0).Picture = LoadPicture("<The TOP Block address here>") 'set the picture
End If
Call Annoy(2) 'display the MISSED message
End If
Step 4: Screen Animation
So here are the last three things remaining.
Screen animation happens when player puts 2 blocks correctly on top of eachother. (refer to BlockLever timer code). To do this we need to move all the "Live" blocks down. Live block refers to the BAlive Array we declared before. which holds the blocks success or failure as a boolean value. (changed in BlockLever timer).
Screen Animation that i have made works with a Counter Variable and a Boolean Variable(discussed later). so first we will declare it. in General - Declarations.
Dim SYC As Integer, noanim as Boolean
alright. now add a Timer, Name : ScreenAnim - Enabled : False - Interval : 25
Type the following code:
Private Sub ScreenAnim_Timer()
SYC = SYC + 1
jump = 225
If noanim Then jump = -225
For i = 1 To CBLOCK
'if block is alive
If BAlive(i) Then Block(i).Top = Block(i).Top + jump
Next i
BaseBlock.Top = BaseBlock.Top + jump
If SYC = 10 Then
noanim = False
SYC = 0
ScreenAnim.Enabled = False
End If
End Sub
- Close the Code Editor. double-click on form and type the following codes. this part sets the startup values. like Goal.
CBLOCK = 0
rev = False
Goal = 15 'max blocks
cmdU.Visible = False
cmdD.Visible = False
TRemain = Goal - TBlocks
Call Annoy(0)
Next page to the Final step...
Screen animation happens when player puts 2 blocks correctly on top of eachother. (refer to BlockLever timer code). To do this we need to move all the "Live" blocks down. Live block refers to the BAlive Array we declared before. which holds the blocks success or failure as a boolean value. (changed in BlockLever timer).
Screen Animation that i have made works with a Counter Variable and a Boolean Variable(discussed later). so first we will declare it. in General - Declarations.
Dim SYC As Integer, noanim as Boolean
alright. now add a Timer, Name : ScreenAnim - Enabled : False - Interval : 25
Type the following code:
Private Sub ScreenAnim_Timer()
SYC = SYC + 1
jump = 225
If noanim Then jump = -225
For i = 1 To CBLOCK
'if block is alive
If BAlive(i) Then Block(i).Top = Block(i).Top + jump
Next i
BaseBlock.Top = BaseBlock.Top + jump
If SYC = 10 Then
noanim = False
SYC = 0
ScreenAnim.Enabled = False
End If
End Sub
- Close the Code Editor. double-click on form and type the following codes. this part sets the startup values. like Goal.
CBLOCK = 0
rev = False
Goal = 15 'max blocks
cmdU.Visible = False
cmdD.Visible = False
TRemain = Goal - TBlocks
Call Annoy(0)
Next page to the Final step...
Step 5: Final Step
So. We added all the things and codings but didn't assigned a button to call DropBlock! to solve this go to Form - KeyDown Section and type the code:
If KeyCode = vbKeySpace Then Call DropBlock ' if Space is hit then drop a block.
Now it's the time to play! Click 'Play' button to run project. Have fun!
If KeyCode = vbKeySpace Then Call DropBlock ' if Space is hit then drop a block.
Now it's the time to play! Click 'Play' button to run project. Have fun!