Atari Combat: Tank Vb 2010
This is my first instructable so bear with me... for my vb final i decided to program atari combat tank everything worked out other than when i hit a barricade the tanks r unable to move...
Open VB2010
Open Visual Basic 2010 and select new project name the project anything you want i just kept mine as windowsapplication1 make sure Windows Forms Application is selected and click OK
Designing the Form
on form 1 you will need a button.... on my form i added 3 labels and 2 pictureboxes
Form 1 Code
double click the button to show the code and add the following:
Tank_VS_Tank.Show()
Adding Form 2
to add a new form go to the menu strip item "project" and select "Add Windows Form" and select "Windows Form" name it what you want and click "add"
Form 2 Design
add 16 timers 2 labels and 11 pictureboxes
place 9 pictureboxes at the bottom of the form and place one picturebox at the middle left and middle right of the form
place the labels at the top left and top right of the form
Form 2 Propertys
select form 2 and click the property's toolbar on the right side of the form.
select backcolor and change it to 0, 64, 0 or a dark green
change forecolor to transparent
change formborderstyle to fixedtoolwindow
change start position to center screen
and window state to maximized
Label Property's
select the label on the left side and change the following:
backcolor: transparent
borderstyle: none
font: IMPACT, 24pt
forecolor: red
text: 0
name: rs
location: 7, 14
autosize: false
size: 57,57
do the same for the label on the right but change the color to blue and name to bs
Picturebox Property's
change the property's for the the picturebox on the left:
backcolor: backcolor of form2
image: download the red tank in the picture above... and that will be the red tank
name: tank1
location: 16, 517
size: 30,30
change the picturebox property's on the right to the same as on the left except the following:
image download the blue tank in the picture above... that will be the blue tank
location: 1644, 517
name: tank2
for the first four pictureboxes at the bottom of the form change the following:
backcolor: download the red tank pictures above
name: name them as the notes in the pictures above
The Code: Dimensions
just under public class add this code ,,, these are the variables we will use later in the code
'we will use k to tell us the direction the tank2 is facing
Dim k As Integer = 4 'tank2 side counter
'we will use s to tell us the direction of tank1
Dim s As Integer = 3 'tank1 side counter
'b(17) is an object array there is not much online about object arrays so if i get enough attention i might make an 'instructable on it
Dim b(17) As PictureBox 'picturebox array
'bt/rt is used to detect if both tank hit the border
Dim bt As Boolean = False
Dim rt As Boolean = False
The Code:tank_vs_tank_keyup
key up is a handler that detects when a key is let up
double click form2 and select the declaration toolbar at the top of the code and select keyup
add the following code after the dimensions:
Select Case e.KeyCode
Case Is = Keys.W
Timer9.Enabled = False 'stops tank move when w key up
Case Is = Keys.S
Timer10.Enabled = False 'stops tank move when s key up
Case Is = Keys.D
Timer11.Enabled = False 'stops tank move when d key up
Case Is = Keys.A
Timer12.Enabled = False 'stops tank move when a key up
Case Is = Keys.ControlKey 'shoots
If s = 1 Then Timer1.Enabled = True 'detects if tank1 face right
If s = 2 Then Timer2.Enabled = True 'detects if tank1 face left
If s = 3 Then Timer3.Enabled = True 'detects if tank1 face up
If s = 4 Then Timer4.Enabled = True 'detects if tank1 face down
End Select
'left tank
Select Case e.KeyCode
Case Is = Keys.Up
Timer13.Enabled = False 'stops tank move when up key up
Case Is = Keys.Down
Timer14.Enabled = False 'stops tank move when down key up
Case Is = Keys.Left
Timer15.Enabled = False 'stops tank move when left key up
Case Is = Keys.Right
Timer16.Enabled = False 'stops tank move when right key up
Case Is = Keys.Enter 'shoots
If k = 1 Then Timer5.Enabled = True 'detects if tank2 face right
If k = 2 Then Timer6.Enabled = True 'detects if tank2 face left
If k = 3 Then Timer7.Enabled = True 'detects if tank2 face up
If k = 4 Then Timer8.Enabled = True 'detects if tank1 face down
End Select
The Code: Object Array
the following is code for 17 pictureboxes... these will act as our blocks the code will go after the private sub statment
"Private Sub Tank_VS_Tank_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load"
For i = 1 To 17
b(i) = New PictureBox 'adds new picbox
b(i).Visible = True 'makes block visible
b(i).BackColor = Color.Khaki 'makes color of blocker khaki
Me.Controls.Add(b(i)) 'adds tank blockers
Next
With b(1) 'size and place block 1
.Height = 22
.Width = 100
.Left = 81
.Top = 138
End With
With b(2) 'size and place block 2
.Height = 200
.Width = 22
.Left = 159
.Top = 159
End With
With b(3) 'size and place block 3
.Height = 200
.Width = 22
.Top = 350
.Left = 159
End With
With b(4) 'size and place block 4
.Height = 22
.Width = 100
.Left = 81
.Top = 550
End With
With b(5) 'size and place block 5
.Height = 22
.Width = 100
.Left = 404
.Top = 0
End With
With b(6) 'size and place block 6
.Height = 200
.Width = 22
.Left = 443
.Top = 22
End With
With b(7) 'size and place block 7
.Height = 200
.Width = 23
.Left = 443
.Top = 484
End With
With b(8) 'size and place block 8
.Height = 22
.Width = 100
.Top = 680
.Left = 404
End With
With b(9) 'size and place block 9
.Height = 200
.Width = 22
.Left = 631
.Top = 253
End With
With b(10) 'size and place block 10
.Height = 22
.Width = 100
.Left = 802
.Top = 0
End With
With b(11) 'size and place block 11
.Height = 200
.Width = 22
.Left = 841
.Top = 22
End With
With b(12) 'size and place block 12
.Height = 200
.Width = 22
.Left = 841
.Top = 484
End With
With b(13) 'size and place block 13
.Height = 22
.Width = 100
.Left = 802
.Top = 680
End With
With b(14) 'size and place block 14
.Height = 22
.Width = 100
.Left = 1125
.Top = 137
End With
With b(15) 'size and place block 15
.Height = 200
.Width = 22
.Left = 1125
.Top = 159
End With
With b(16) 'size and place block 16
.Height = 200
.Width = 22
.Left = 1125
.Top = 350
End With
With b(17) 'size and place block 17
.Height = 22
.Width = 100
.Left = 1125
.Top = 550
End With
End Sub
The Code: Keydown
key down detects if a key is down the code goes after private sub tank_vs_tank_keydown
select the declarations for the form 1 code and select keydown
Private Sub Tank_VS_Tank_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDow
'right tank
Select Case e.KeyCode
Case Is = Keys.W 'moves tank1 up and changes the counter to 3 and the image to tank face up
If tank1.Top = Me.Top Then Timer9.Enabled = False
s = 3
Timer9.Enabled = True
Timer10.Enabled = False
Timer11.Enabled = False
Timer12.Enabled = False
tank1.Image = rt1.Image
Case Is = Keys.S 'moves tank1 down and changes the counter to 4 and the image to tank face down
If tank1.Bottom = Me.Bottom Then Timer10.Enabled = False
s = 4
Timer10.Enabled = True
Timer9.Enabled = False
Timer11.Enabled = False
Timer12.Enabled = False
tank1.Image = rt3.Image
Case Is = Keys.D 'moves tank1 right and changes the counter to 1 and the image to tank face right
If tank1.Right = Me.Right Then Timer11.Enabled = False
s = 1
Timer11.Enabled = True
Timer9.Enabled = False
Timer10.Enabled = False
Timer12.Enabled = False
tank1.Image = rt4.Image
Case Is = Keys.A 'moves tank1 left and changes the counter to 2 and the image to tank face left
If tank1.Left = Me.Left Then Timer12.Enabled = False
s = 2
Timer12.Enabled = True
Timer9.Enabled = False
Timer10.Enabled = False
Timer11.Enabled = False
tank1.Image = rt2.Image
Case Is = Keys.P
MsgBox("Paused Press OK to Continue")
End Select
ramo.Left = tank1.Left + 15
ramo.Top = tank1.Top + 13
For re = 1 To 17
If tank1.Bounds.IntersectsWith(b(re).Bounds) Then Timer9.Enabled = False
If tank1.Bounds.IntersectsWith(b(re).Bounds) Then Timer10.Enabled = False
If tank1.Bounds.IntersectsWith(b(re).Bounds) Then Timer11.Enabled = False
If tank1.Bounds.IntersectsWith(b(re).Bounds) Then Timer12.Enabled = False
If tank1.Bounds.IntersectsWith(b(re).Bounds) Then rt = True
Next
If tank1.Top < Me.Top + 15 Then tank1.Top += 6
If tank1.Bottom > Me.Bottom - 35 Then tank1.Top -= 6
If tank1.Right > Me.Right - 15 Then tank1.Left -= 6
If tank1.Left < Me.Left + 10 Then tank1.Left += 6
'left tank
Select Case e.KeyCode
Case Is = Keys.Up 'moves tank2 up and changes the counter to 4 and the image to tank face up
k = 4
Timer13.Enabled = True
Timer14.Enabled = False
Timer15.Enabled = False
Timer16.Enabled = False
tank2.Image = bt1.Image
Case Is = Keys.Down 'moves tank2 down and changes the counter to 3 and the image to tank face down
k = 3
Timer14.Enabled = True
Timer15.Enabled = False
Timer16.Enabled = False
Timer13.Enabled = False
tank2.Image = bt3.Image
Case Is = Keys.Left 'moves tank2 right and changes the counter to 1 and the image to tank face right
k = 1
Timer15.Enabled = True
Timer16.Enabled = False
Timer13.Enabled = False
Timer14.Enabled = False
tank2.Image = bt2.Image
Case Is = Keys.Right 'moves tank2 left and changes the counter to 2 and the image to tank face left
k = 2
Timer16.Enabled = True
Timer13.Enabled = False
Timer14.Enabled = False
Timer15.Enabled = False
tank2.Image = bt4.Image
End Select
bamo.Left = tank2.Left + 15 'places blue ammo
bamo.Top = tank2.Top + 13
For ree = 1 To 17
If tank2.Bounds.IntersectsWith(b(ree).Bounds) Then Timer13.Enabled = False 'checks if tank2 hits blocks
If tank2.Bounds.IntersectsWith(b(ree).Bounds) Then Timer14.Enabled = False 'checks if tank2 hits blocks
If tank2.Bounds.IntersectsWith(b(ree).Bounds) Then Timer15.Enabled = False 'checks if tank2 hits blocks
If tank2.Bounds.IntersectsWith(b(ree).Bounds) Then Timer16.Enabled = False 'checks if tank2 hits blocks
If tank2.Bounds.IntersectsWith(b(ree).Bounds) Then bt = True
Next
If rt = True And bt = True Then reset()
If tank2.Top < Me.Top + 15 Then tank2.Top += 5
If tank2.Bottom > Me.Bottom + 35 Then tank2.Top -= 5
If tank2.Right > Me.Right - 15 Then tank2.Left -= 5
If tank2.Left < Me.Left + 5 Then tank2.Left += 5
End Sub
The Code: Red Bullet
go to the form2 design page and select timers 1 to 4 and press enter
add the following code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 'fires red ammo right
ramo.Left += 10
If ramo.Bounds.IntersectsWith(tank2.Bounds) Then Timer1.Enabled = False
If ramo.Bounds.IntersectsWith(tank2.Bounds) Then reset()
t()
If ramo.Right > Me.Right Then Timer1.Enabled = False
For rer = 1 To 17
If ramo.Bounds.IntersectsWith(b(rer).Bounds) Then Timer1.Enabled = False
Next
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick 'fires red ammo left
ramo.Left -= 10
If ramo.Bounds.IntersectsWith(tank2.Bounds) Then Timer2.Enabled = False
t()
If ramo.Left < Me.Left Then Timer2.Enabled = False
For rere = 1 To 17
If ramo.Bounds.IntersectsWith(b(rere).Bounds) Then Timer2.Enabled = False
Next
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick 'fires red ammo up
ramo.Top -= 10
If ramo.Bounds.IntersectsWith(tank2.Bounds) Then Timer3.Enabled = False
t()
If ramo.Top < Me.Top Then Timer3.Enabled = False
For rerer = 1 To 17
If ramo.Bounds.IntersectsWith(b(rerer).Bounds) Then Timer3.Enabled = False
Next
End Sub
Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick 'fires red ammo down
ramo.Top += 10
If ramo.Bounds.IntersectsWith(tank2.Bounds) Then Timer4.Enabled = False
t()
If ramo.Bottom > Me.Bottom Then Timer4.Enabled = False
For ri = 1 To 17
If ramo.Bounds.IntersectsWith(b(ri).Bounds) Then Timer4.Enabled = False
Next
End Sub
The Code: Blue Bullet
select timers 5 to 8 and press enter
Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick 'fires blue ammo left
bamo.Left -= 10
If bamo.Bounds.IntersectsWith(tank1.Bounds) Then Timer5.Enabled = False
t2()
If bamo.Left < Me.Left Then Timer5.Enabled = False
For rir = 1 To 17
If bamo.Bounds.IntersectsWith(b(rir).Bounds) Then Timer5.Enabled = False
Next
End Sub
Private Sub Timer6_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer6.Tick 'fires blue ammo right
bamo.Left += 10
If bamo.Bounds.IntersectsWith(tank1.Bounds) Then Timer6.Enabled = False
t2()
If bamo.Right > Me.Right Then Timer6.Enabled = False
For riri = 1 To 17
If bamo.Bounds.IntersectsWith(b(riri).Bounds) Then Timer6.Enabled = False
Next
End Sub
Private Sub Timer7_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer7.Tick 'fires blue ammo down
bamo.Top += 10
If bamo.Bounds.IntersectsWith(tank1.Bounds) Then Timer7.Enabled = False 'stops timer
t2()
If bamo.Bottom > Me.Bottom Then Timer7.Enabled = False 'if bamo is > form2 bottom ammo
For ririr = 1 To 17
If bamo.Bounds.IntersectsWith(b(ririr).Bounds) Then Timer7.Enabled = False 'detects if bamo hits blocks
Next
End Sub
Private Sub Timer8_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer8.Tick 'fires blue ammo up
bamo.Top -= 10
If bamo.Bounds.IntersectsWith(tank1.Bounds) Then Timer8.Enabled = False 'stops timer
t2()
If bamo.Top < Me.Top Then Timer8.Enabled = False 'if bamo is < form2 top ammo
For ririri = 1 To 17
If bamo.Bounds.IntersectsWith(b(ririri).Bounds) Then Timer8.Enabled = False 'detects if bamo hits blocks
Next
End Sub
The Code: Red Tank Movement
to move the red tank i use 4 timers each for either up down left or right in form2 design select timers 9-12 and press enter add the following code to each:
Private Sub Timer9_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer9.Tick 'up
tank1.Top -= 5
tank1.Image = rt1.Image
End Sub
Private Sub Timer10_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer10.Tick 'down
tank1.Top += 5
tank1.Image = rt3.Image
End Sub
Private Sub Timer11_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer11.Tick 'left
tank1.Left += 5
tank1.Image = rt4.Image
End Sub
Private Sub Timer12_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer12.Tick 'right
tank1.Left -= 5
tank1.Image = rt2.Image
End Sub
The Code: Blue Tank Movement
for the blue tank i did the same except in different timers so in form2 design select timers 13-16 and press enter add the following code to each:
Private Sub Timer13_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer13.Tick 'up
tank2.Top -= 5
tank2.Image = bt1.Image
End Sub
Private Sub Timer14_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer14.Tick
'down
tank2.Top += 5
tank2.Image = bt3.Image
End Sub
Private Sub Timer15_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer15.Tick 'left
tank2.Left -= 5
tank2.Image = bt2.Image
End Sub
Private Sub Timer16_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer16.Tick 'right
tank2.Left += 5
tank2.Image = bt4.Image
End Sub
The Code: Suberteens
in my code i had things like: t(), t2(), and reset() those are my subs t() detects if the red ammo hits the form borders or the blue tank; t2() detects if the blue ammo hits the red tank or the form; and reset() resets the game field
heres the code for t():
Private Sub t()
If rambo.Bounds.IntersectsWith(tank2.Bounds) Then tank2.Image = extank.Image
If rambo.Bounds.IntersectsWith(tank2.Bounds) Then tank1.Left = 12
If rambo.Bounds.IntersectsWith(tank2.Bounds) Then tank1.Top = 336
If rambo.Bounds.IntersectsWith(tank2.Bounds) Then tank2.Left = 1233
If rambo.Bounds.IntersectsWith(tank2.Bounds) Then tank2.Top = 336
End Sub
the code for t2():
Private Sub t2()
If bami.Bounds.IntersectsWith(tank1.Bounds) Then tank1.Image = extank.Image
If bami.Bounds.IntersectsWith(tank1.Bounds) Then tank2.Left = 1233
If bami.Bounds.IntersectsWith(tank1.Bounds) Then tank2.Top = 336
If bami.Bounds.IntersectsWith(tank1.Bounds) Then tank1.Left = 12
If bami.Bounds.IntersectsWith(tank1.Bounds) Then tank1.Top = 336
End Sub
the code for reset()
Private Sub reset()
tank1.Left = 12
tank1.Top = 336
tank2.Left = 1233
tank2.Top = 336
bt = False
rt = False
End Sub
The End
thanks for looking at my instructable please give feedback