Skip to content

Instantly share code, notes, and snippets.

@annibal
Created June 25, 2013 14:51
Show Gist options
  • Save annibal/5859077 to your computer and use it in GitHub Desktop.
Save annibal/5859077 to your computer and use it in GitHub Desktop.
Tank source code. might not work.
Public Class Form1
WithEvents t As New Timer With {.Interval = 30}
Dim p As New Panel
Dim g As Graphics
Dim kp(50) As Boolean
Class obj
Function sp(ByVal i As Object, ByVal x As Object, ByVal y As Object)
Me.pt(i).X = CSng(x)
Me.pt(i).Y = CSng(y)
End Function
Public x, y, speed, h, w, dir, turnSpeed, maxTurnSpeed
Public maxSpeed, accel, delay, grip, hp, agility
Public pt(15) As PointF
Function setMovementData(ByVal maximum_speed, ByVal maximum_turnSpeed, ByVal acceleration, ByVal agility, ByVal grip)
Me.maxSpeed = maximum_speed
Me.maxTurnSpeed = maximum_turnSpeed
Me.accel = acceleration
Me.agility = agility
Me.grip = grip
End Function
Sub New(ByVal x, ByVal y, ByVal s, ByVal w, ByVal h, ByVal dir, ByVal hp, ByVal shootdelay)
Me.x = x
Me.y = y
Me.speed = s
Me.h = h
Me.w = w
Me.delay = shootdelay
Me.hp = hp
End Sub
Sub New()
'lol
End Sub
Function posx(ByVal pos, ByVal angle, ByVal dis)
posx = CSng(pos + Math.Sin(angle * Math.PI / 180) * dis)
End Function
Function posy(ByVal pos, ByVal angle, ByVal dis)
posy = CSng(pos + Math.Cos(angle * Math.PI / 180) * dis)
End Function
Function posx(ByVal angle, ByVal dis)
posx = CSng(Me.x + Math.Sin(angle * Math.PI / 180) * dis)
End Function
Function posy(ByVal angle, ByVal dis)
posy = CSng(Me.y + Math.Cos(angle * Math.PI / 180) * dis)
End Function
End Class
Dim p1 As New obj(p.Width / 4, p.Height / 2, 0, 10, 200, 90, 5, 100)
Dim p2 As New obj(3 * p.Width / 4, p.Height / 2, 0, 50, 100, 0, 5, 100)
Dim h1 As New obj(p.Width / 4, p.Height / 2, 0, 20, 30, 0, 35, 0)
Dim h2 As New obj(p.Width / 4, p.Height / 2, 0, 20, 30, 0, 35, 0) '// h of Head stays top of p (Player), the tank
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.W Then kp(1) = True 'tank 1 move front
If e.KeyCode = Keys.S Then kp(2) = True 'tank 1 move back
If e.KeyCode = Keys.A Then kp(3) = True 'tank 1 turn left
If e.KeyCode = Keys.D Then kp(4) = True 'tank 1 turn right
If e.KeyCode = Keys.C Then kp(9) = True 'tank 1 head anti-horario
If e.KeyCode = Keys.V Then kp(10) = True 'tank 1 head horario
If e.KeyCode = Keys.Space Then kp(13) = True 'tank 1 shoot
If e.KeyCode = Keys.I Then kp(5) = True 'tank 2 move front
If e.KeyCode = Keys.K Then kp(6) = True 'tank 2 move behind
If e.KeyCode = Keys.J Then kp(7) = True 'tank 2 move left
If e.KeyCode = Keys.L Then kp(8) = True 'tank 2 move right
If e.KeyCode = Asc("-") Then kp(11) = True 'tank 2 head anti-horario
If e.KeyCode = Asc("=") Then kp(12) = True 'tank 2 head horario
If e.KeyCode = Keys.Enter Then kp(14) = True 'tank 2 shoot
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.W Then kp(1) = Not True 'tank 1 move front
If e.KeyCode = Keys.S Then kp(2) = Not True 'tank 1 move back
If e.KeyCode = Keys.A Then kp(3) = Not True 'tank 1 turn left
If e.KeyCode = Keys.D Then kp(4) = Not True 'tank 1 turn right
If e.KeyCode = Keys.C Then kp(9) = Not True 'tank 1 head anti-horario
If e.KeyCode = Keys.V Then kp(10) = Not True 'tank 1 head horario
If e.KeyCode = Keys.Space Then kp(13) = Not True 'tank 1 shoot
If e.KeyCode = Keys.I Then kp(5) = Not True 'tank 2 move front
If e.KeyCode = Keys.K Then kp(6) = Not True 'tank 2 move behind
If e.KeyCode = Keys.J Then kp(7) = Not True 'tank 2 move left
If e.KeyCode = Keys.L Then kp(8) = Not True 'tank 2 move right
If e.KeyCode = Asc("-") Then kp(11) = Not True 'tank 2 head anti-horario
If e.KeyCode = Asc("=") Then kp(12) = Not True 'tank 2 head horario
If e.KeyCode = Keys.Enter Then kp(14) = Not True 'tank 2 shoot
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
Call Form1_Resize(Me, Nothing)
p1.setMovementData(10, 5, 3, 2, 1)
p2.setMovementData(10, 5, 3, 2, 1)
Me.Controls.Add(p)
t.Start()
End Sub
Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles t.Tick
g.Clear(Color.Black)
If kp(1) And p1.speed < 10 Then p1.speed += 3
If kp(2) And p1.speed > -10 Then p1.speed -= 3
If kp(3) And p1.turnSpeed < p1.maxTurnSpeed Then p1.turnSpeed += p1.agility
If kp(4) And p1.turnSpeed > -p1.maxTurnSpeed Then p1.turnSpeed -= p1.agility
p1.dir += p1.turnSpeed
p1.x = p1.posx(p1.dir, p1.speed)
p1.y = p1.posy(p1.dir, p1.speed)
If p1.turnSpeed > 0 And Not (kp(3) Or kp(4)) Then p1.turnSpeed -= p1.agility
If p1.turnSpeed < 0 And Not (kp(3) Or kp(4)) Then p1.turnSpeed += p1.agility
If p1.speed > 0 And Not (kp(1) Or kp(2)) Then p1.speed -= p1.grip
If p1.speed < 0 And Not (kp(1) Or kp(2)) Then p1.speed += p1.grip
If kp(9) Then h1.dir += p1.agility
If kp(10) Then h1.dir -= p1.agility
Me.Text = "X:" & CInt(p1.x) & " Y:" & CInt(p1.y) & " Sp:" & p1.speed & " TSp:" & p1.turnSpeed & " D:" & p1.dir & " " & kp(1) & " " & kp(2) & " " & kp(3) & " " & kp(4)
ReDim p1.pt(3)
p1.sp(0, p1.posx(p1.dir + 45, p1.w / 2), p1.posy(p1.dir + 45, p1.h / 2))
p1.sp(1, p1.posx(p1.dir + 135, p1.w / 2), p1.posy(p1.dir + 135, p1.h / 2))
p1.sp(2, p1.posx(p1.dir + 225, p1.w / 2), p1.posy(p1.dir + 225, p1.h / 2))
p1.sp(3, p1.posx(p1.dir + 315, p1.w / 2), p1.posy(p1.dir + 315, p1.h / 2))
ReDim p2.pt(3)
p2.sp(0, p2.posx(p2.dir + 45, p2.w / 2), p2.posy(p2.dir + 45, p2.h / 2))
p2.sp(1, p2.posx(p2.dir + 135, p2.w / 2), p2.posy(p2.dir + 135, p2.h / 2))
p2.sp(2, p2.posx(p2.dir + 225, p2.w / 2), p2.posy(p2.dir + 225, p2.h / 2))
p2.sp(3, p2.posx(p2.dir + 315, p2.w / 2), p2.posy(p2.dir + 315, p2.h / 2))
ReDim h1.pt(3)
h1.sp(0, h1.posx(h1.posx(h1.dir, h1.w / 2), h1.dir + 45, h1.w / 4), h1.posy(h1.posy(h1.dir, h1.h / 2), h1.dir + 45, h1.h / 4))
h1.sp(1, h1.posx(h1.posx(h1.dir, h1.w / 2), h1.dir + 135, h1.w / 4), h1.posy(h1.posy(h1.dir, h1.h / 2), h1.dir + 135, h1.h / 4))
h1.sp(2, h1.posx(h1.posx(h1.dir, h1.w / 2), h1.dir + 225, h1.w / 4), h1.posy(h1.posy(h1.dir, h1.h / 2), h1.dir + 225, h1.h / 4))
h1.sp(3, h1.posx(h1.posx(h1.dir, h1.w / 2), h1.dir + 315, h1.w / 4), h1.posy(h1.posy(h1.dir, h1.h / 2), h1.dir + 315, h1.h / 4))
ReDim h1.pt(3)
h2.sp(0, h2.posx(h2.posx(h2.dir, h2.w / 2), h2.dir + 45, h2.w / 4), h2.posy(h2.posy(h2.dir, h2.h / 2), h2.dir + 45, h2.h / 4))
h2.sp(0, h2.posx(h2.posx(h2.dir, h2.w / 2), h2.dir + 135, h2.w / 4), h2.posy(h2.posy(h2.dir, h2.h / 2), h2.dir + 135, h2.h / 4))
h2.sp(0, h2.posx(h2.posx(h2.dir, h2.w / 2), h2.dir + 225, h2.w / 4), h2.posy(h2.posy(h2.dir, h2.h / 2), h2.dir + 225, h2.h / 4))
h2.sp(0, h2.posx(h2.posx(h2.dir, h2.w / 2), h2.dir + 315, h2.w / 4), h2.posy(h2.posy(h2.dir, h2.h / 2), h2.dir + 315, h2.h / 4))
g.FillPolygon(Brushes.BlueViolet, p1.pt)
'g.FillPolygon(Brushes.BlueViolet, h1.pt)
g.FillPolygon(Brushes.MediumVioletRed, p2.pt)
'g.FillPolygon(Brushes.MediumVioletRed, h2.pt)
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
p.Location = New Point(0, 0)
p.Size = Me.Size
g = p.CreateGraphics
End Sub
End Class
Public Class Form1
WithEvents t As New Timer With {.Interval = 30}
Dim p As New Panel
Dim g As Graphics
Dim kp(50) As Boolean
Class obj
Function sp(ByVal i As Object, ByVal x As Object, ByVal y As Object)
Me.pt(i).X = CSng(x)
Me.pt(i).Y = CSng(y)
End Function
Public x, y, speed, h, w, dir, turnSpeed, maxTurnSpeed
Public maxSpeed, accel, delay, grip, hp, agility
Public pt(15) As PointF
Function setMovementData(ByVal maximum_speed, ByVal maximum_turnSpeed, ByVal acceleration, ByVal agility, ByVal grip)
Me.maxSpeed = maximum_speed
Me.maxTurnSpeed = maximum_turnSpeed
Me.accel = acceleration
Me.agility = agility
Me.grip = grip
End Function
Sub New(ByVal x, ByVal y, ByVal s, ByVal w, ByVal h, ByVal dir, ByVal hp, ByVal shootdelay)
Me.x = x
Me.y = y
Me.speed = s
Me.h = h
Me.w = w
Me.delay = shootdelay
Me.hp = hp
End Sub
Sub New()
'lol
End Sub
Function posx(ByVal pos, ByVal angle, ByVal dis)
posx = CSng(pos + Math.Sin(angle * Math.PI / 180) * dis)
End Function
Function posy(ByVal pos, ByVal angle, ByVal dis)
posy = CSng(pos + Math.Cos(angle * Math.PI / 180) * dis)
End Function
Function posx(ByVal angle, ByVal dis)
posx = CSng(Me.x + Math.Sin(angle * Math.PI / 180) * dis)
End Function
Function posy(ByVal angle, ByVal dis)
posy = CSng(Me.y + Math.Cos(angle * Math.PI / 180) * dis)
End Function
End Class
Dim p1 As New obj(p.Width / 4, p.Height / 2, 0, 10, 200, 90, 5, 100)
Dim p2 As New obj(3 * p.Width / 4, p.Height / 2, 0, 50, 100, 0, 5, 100)
Dim h1 As New obj(p.Width / 4, p.Height / 2, 0, 20, 30, 0, 35, 0)
Dim h2 As New obj(p.Width / 4, p.Height / 2, 0, 20, 30, 0, 35, 0) '// h of Head stays top of p (Player), the tank
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.W Then kp(1) = True 'tank 1 move front
If e.KeyCode = Keys.S Then kp(2) = True 'tank 1 move back
If e.KeyCode = Keys.A Then kp(3) = True 'tank 1 turn left
If e.KeyCode = Keys.D Then kp(4) = True 'tank 1 turn right
If e.KeyCode = Keys.C Then kp(9) = True 'tank 1 head anti-horario
If e.KeyCode = Keys.V Then kp(10) = True 'tank 1 head horario
If e.KeyCode = Keys.Space Then kp(13) = True 'tank 1 shoot
If e.KeyCode = Keys.I Then kp(5) = True 'tank 2 move front
If e.KeyCode = Keys.K Then kp(6) = True 'tank 2 move behind
If e.KeyCode = Keys.J Then kp(7) = True 'tank 2 move left
If e.KeyCode = Keys.L Then kp(8) = True 'tank 2 move right
If e.KeyCode = Asc("-") Then kp(11) = True 'tank 2 head anti-horario
If e.KeyCode = Asc("=") Then kp(12) = True 'tank 2 head horario
If e.KeyCode = Keys.Enter Then kp(14) = True 'tank 2 shoot
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.W Then kp(1) = Not True 'tank 1 move front
If e.KeyCode = Keys.S Then kp(2) = Not True 'tank 1 move back
If e.KeyCode = Keys.A Then kp(3) = Not True 'tank 1 turn left
If e.KeyCode = Keys.D Then kp(4) = Not True 'tank 1 turn right
If e.KeyCode = Keys.C Then kp(9) = Not True 'tank 1 head anti-horario
If e.KeyCode = Keys.V Then kp(10) = Not True 'tank 1 head horario
If e.KeyCode = Keys.Space Then kp(13) = Not True 'tank 1 shoot
If e.KeyCode = Keys.I Then kp(5) = Not True 'tank 2 move front
If e.KeyCode = Keys.K Then kp(6) = Not True 'tank 2 move behind
If e.KeyCode = Keys.J Then kp(7) = Not True 'tank 2 move left
If e.KeyCode = Keys.L Then kp(8) = Not True 'tank 2 move right
If e.KeyCode = Asc("-") Then kp(11) = Not True 'tank 2 head anti-horario
If e.KeyCode = Asc("=") Then kp(12) = Not True 'tank 2 head horario
If e.KeyCode = Keys.Enter Then kp(14) = Not True 'tank 2 shoot
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
Call Form1_Resize(Me, Nothing)
p1.setMovementData(10, 5, 3, 2, 1)
p2.setMovementData(10, 5, 3, 2, 1)
Me.Controls.Add(p)
t.Start()
End Sub
Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles t.Tick
g.Clear(Color.Black)
If kp(1) And p1.speed < 10 Then p1.speed += 3
If kp(2) And p1.speed > -10 Then p1.speed -= 3
If kp(3) And p1.turnSpeed < p1.maxTurnSpeed Then p1.turnSpeed += p1.agility
If kp(4) And p1.turnSpeed > -p1.maxTurnSpeed Then p1.turnSpeed -= p1.agility
p1.dir += p1.turnSpeed
p1.x = p1.posx(p1.dir, p1.speed)
p1.y = p1.posy(p1.dir, p1.speed)
If p1.turnSpeed > 0 And Not (kp(3) Or kp(4)) Then p1.turnSpeed -= p1.agility
If p1.turnSpeed < 0 And Not (kp(3) Or kp(4)) Then p1.turnSpeed += p1.agility
If p1.speed > 0 And Not (kp(1) Or kp(2)) Then p1.speed -= p1.grip
If p1.speed < 0 And Not (kp(1) Or kp(2)) Then p1.speed += p1.grip
If kp(9) Then h1.dir += p1.agility
If kp(10) Then h1.dir -= p1.agility
Me.Text = "X:" & CInt(p1.x) & " Y:" & CInt(p1.y) & " Sp:" & p1.speed & " TSp:" & p1.turnSpeed & " D:" & p1.dir & " " & kp(1) & " " & kp(2) & " " & kp(3) & " " & kp(4)
ReDim p1.pt(3)
p1.sp(0, p1.posx(p1.dir + 45, p1.w / 2), p1.posy(p1.dir + 45, p1.h / 2))
p1.sp(1, p1.posx(p1.dir + 135, p1.w / 2), p1.posy(p1.dir + 135, p1.h / 2))
p1.sp(2, p1.posx(p1.dir + 225, p1.w / 2), p1.posy(p1.dir + 225, p1.h / 2))
p1.sp(3, p1.posx(p1.dir + 315, p1.w / 2), p1.posy(p1.dir + 315, p1.h / 2))
ReDim p2.pt(3)
p2.sp(0, p2.posx(p2.dir + 45, p2.w / 2), p2.posy(p2.dir + 45, p2.h / 2))
p2.sp(1, p2.posx(p2.dir + 135, p2.w / 2), p2.posy(p2.dir + 135, p2.h / 2))
p2.sp(2, p2.posx(p2.dir + 225, p2.w / 2), p2.posy(p2.dir + 225, p2.h / 2))
p2.sp(3, p2.posx(p2.dir + 315, p2.w / 2), p2.posy(p2.dir + 315, p2.h / 2))
ReDim h1.pt(3)
h1.sp(0, h1.posx(h1.posx(h1.dir, h1.w / 2), h1.dir + 45, h1.w / 4), h1.posy(h1.posy(h1.dir, h1.h / 2), h1.dir + 45, h1.h / 4))
h1.sp(1, h1.posx(h1.posx(h1.dir, h1.w / 2), h1.dir + 135, h1.w / 4), h1.posy(h1.posy(h1.dir, h1.h / 2), h1.dir + 135, h1.h / 4))
h1.sp(2, h1.posx(h1.posx(h1.dir, h1.w / 2), h1.dir + 225, h1.w / 4), h1.posy(h1.posy(h1.dir, h1.h / 2), h1.dir + 225, h1.h / 4))
h1.sp(3, h1.posx(h1.posx(h1.dir, h1.w / 2), h1.dir + 315, h1.w / 4), h1.posy(h1.posy(h1.dir, h1.h / 2), h1.dir + 315, h1.h / 4))
ReDim h1.pt(3)
h2.sp(0, h2.posx(h2.posx(h2.dir, h2.w / 2), h2.dir + 45, h2.w / 4), h2.posy(h2.posy(h2.dir, h2.h / 2), h2.dir + 45, h2.h / 4))
h2.sp(0, h2.posx(h2.posx(h2.dir, h2.w / 2), h2.dir + 135, h2.w / 4), h2.posy(h2.posy(h2.dir, h2.h / 2), h2.dir + 135, h2.h / 4))
h2.sp(0, h2.posx(h2.posx(h2.dir, h2.w / 2), h2.dir + 225, h2.w / 4), h2.posy(h2.posy(h2.dir, h2.h / 2), h2.dir + 225, h2.h / 4))
h2.sp(0, h2.posx(h2.posx(h2.dir, h2.w / 2), h2.dir + 315, h2.w / 4), h2.posy(h2.posy(h2.dir, h2.h / 2), h2.dir + 315, h2.h / 4))
g.FillPolygon(Brushes.BlueViolet, p1.pt)
'g.FillPolygon(Brushes.BlueViolet, h1.pt)
g.FillPolygon(Brushes.MediumVioletRed, p2.pt)
'g.FillPolygon(Brushes.MediumVioletRed, h2.pt)
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
p.Location = New Point(0, 0)
p.Size = Me.Size
g = p.CreateGraphics
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment