Advertisement
ssoni

asteroids.vb

Apr 21st, 2022
1,869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.95 KB | None | 0 0
  1. Public Class frmAsteriods
  2.  
  3.     Const numenemies As Integer = 5
  4.     Public AsteroidList As New List(Of Asteroid)
  5.     Public BulletList As New List(Of Asteroid)
  6.  
  7.     Public ship As New Asteroid
  8.     Public p1 As New Point
  9.     Public p2 As New Point
  10.  
  11.     Dim degrees As Integer
  12.     Dim radians As Single
  13.  
  14.     Const PI As Long = 3.141579
  15.     Const DEGREE_SKIP As Integer = 15
  16.     Const GUN_LENGTH As Integer = 80
  17.     Const THRUST_SKIP As Integer = 5
  18.     Const THRUST_MAX As Long = 100
  19.     Const MAX_BULLETS As Integer = 8
  20.  
  21.     Const asteriodRadius As Integer = 20
  22.     Const bulletRadius As Integer = 5
  23.     Const meRadius As Integer = 50
  24.     Const MAX_SPEED As Integer = 20
  25.     Const bulletSpeedMult As Integer = 30
  26.  
  27.     Dim foundbullet As Boolean
  28.     Dim numbullets As Integer
  29.  
  30.     Dim pressingkeyleft As Boolean
  31.     Dim pressingkeyright As Boolean
  32.     Dim pressingkeyup As Boolean
  33.     Dim pressingkeydown As Boolean
  34.  
  35.     Dim thrust As Integer
  36.     Dim score As Long
  37.  
  38.     Public Function distance(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer) As Single
  39.         Return Math.Sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2))
  40.     End Function
  41.  
  42.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  43.  
  44.         degrees = 0
  45.         score = 0
  46.         Dim r As Rectangle
  47.         Dim i As Integer
  48.         Dim a As Asteroid
  49.  
  50.         'Load up the images of the enemy
  51.         For i = 1 To numenemies
  52.             a = New Asteroid
  53.             r = New Rectangle
  54.             r.Y = Int(Rnd() * Me.Height)
  55.             r.X = Int(Rnd() * Me.Width)
  56.             r.Height = asteriodRadius * 2
  57.             r.Width = asteriodRadius * 2
  58.             a.rect = r
  59.             a.speedX = generateSpeed(MAX_SPEED)
  60.             a.speedY = generateSpeed(MAX_SPEED)
  61.             a.visible = True
  62.             AsteroidList.Add(a)
  63.         Next i
  64.  
  65.         'Load the player's ship
  66.         r = New Rectangle
  67.         r.Y = Me.Height / 2
  68.         r.X = Me.Width / 2
  69.         r.Height = meRadius * 2
  70.         r.Width = meRadius * 2
  71.         ship.rect = r
  72.         ship.visible = True
  73.  
  74.     End Sub
  75.  
  76.     Public Function generateSpeed(max As Integer) As Integer
  77.         Dim n As Integer
  78.  
  79.         n = (Rnd() * max) - (max / 2)
  80.         If Math.Abs(n) < max / 4 Then
  81.             If n < 0 Then
  82.                 n = n - (max / 4)
  83.             Else
  84.                 n = n + (max / 4)
  85.             End If
  86.         End If
  87.         Return n
  88.  
  89.     End Function
  90.  
  91.     Private Sub tmrDropThrust_Tick(sender As Object, e As EventArgs) Handles tmrDropThrust.Tick
  92.         thrust = thrust - 5
  93.         If thrust <= 0 Then
  94.             thrust = 0
  95.         End If
  96.  
  97.     End Sub
  98.  
  99.     Private Sub frmAsteriods_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
  100.         If e.KeyCode = 39 Then
  101.             pressingkeyright = False
  102.         ElseIf e.KeyCode = 37 Then
  103.             pressingkeyleft = False
  104.         ElseIf e.KeyCode = 32 Then
  105.             'fire = False
  106.             Call fireBullet()
  107.         ElseIf e.KeyCode = 38 Then
  108.             pressingkeyup = False
  109.         ElseIf e.KeyCode = 40 Then
  110.             pressingkeydown = False
  111.         End If
  112.  
  113.     End Sub
  114.  
  115.     Private Sub frmAsteriods_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
  116.         If e.KeyCode = 39 Then
  117.             pressingkeyright = True
  118.         ElseIf e.KeyCode = 37 Then
  119.             pressingkeyleft = True
  120.         ElseIf e.KeyCode = 32 Then
  121.             'fire = True
  122.         ElseIf e.KeyCode = 38 Then
  123.             pressingkeyup = True
  124.         ElseIf e.KeyCode = 40 Then
  125.             pressingkeydown = True
  126.         End If
  127.  
  128.     End Sub
  129.  
  130.     Private Sub frmAsteriods_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
  131.         Dim myPen As Pen
  132.         myPen = New Pen(Drawing.Color.White, 1)
  133.         Dim myGraphics As Graphics
  134.         myGraphics = Me.CreateGraphics
  135.  
  136.         'draw asteroids
  137.         For Each a In AsteroidList
  138.             If a.visible = True Then
  139.                 myGraphics.DrawEllipse(myPen, a.rect)
  140.             End If
  141.         Next
  142.  
  143.         'draw bullets
  144.         For Each b In BulletList
  145.             If b.visible = True Then
  146.                 myGraphics.DrawEllipse(myPen, b.rect)
  147.             End If
  148.         Next
  149.  
  150.  
  151.         'draw ship
  152.         myGraphics.DrawEllipse(myPen, ship.rect)
  153.         myGraphics.DrawLine(myPen, p1, p2)
  154.  
  155.     End Sub
  156.  
  157.     Private Sub tmrMoveEnemy_Tick(sender As Object, e As EventArgs) Handles tmrMoveEnemy.Tick
  158.  
  159.         Dim a As Asteroid
  160.  
  161.         'move each asteriod
  162.         For Each a In AsteroidList
  163.  
  164.             'move the asteriod
  165.             a.rect.X = a.rect.X + a.speedX
  166.             a.rect.Y = a.rect.Y + a.speedY
  167.  
  168.             'check if it went off the screen (4 ways)
  169.             'if it did, then reset it on the other side of the screen
  170.             'off the left side?
  171.             'off the right side?
  172.             'off the top?
  173.             'off the bottom?
  174.             If a.rect.X < 0 Then
  175.                 a.rect.X = Me.Width
  176.             ElseIf a.rect.X > Me.Width Then
  177.                 a.rect.X = 0
  178.             ElseIf a.rect.Y < 0 Then
  179.                 a.rect.Y = Me.Height
  180.             ElseIf a.rect.Y > Me.Height Then
  181.                 a.rect.Y = 0
  182.             End If
  183.  
  184.         Next
  185.  
  186.         Me.Refresh()
  187.         'Me.Invalidate()
  188.  
  189.     End Sub
  190.  
  191.     Private Sub tmrKeyPress_Tick(sender As Object, e As EventArgs) Handles tmrKeyPress.Tick
  192.  
  193.         If pressingkeyright = True Then
  194.             degrees = degrees + DEGREE_SKIP
  195.         ElseIf pressingkeyleft = True Then
  196.             degrees = degrees - DEGREE_SKIP
  197.         ElseIf pressingkeyup = True Then
  198.             thrust = thrust + THRUST_SKIP
  199.             If thrust > THRUST_MAX Then
  200.                 thrust = THRUST_MAX
  201.             End If
  202.         ElseIf pressingkeydown = True Then
  203.             thrust = thrust - THRUST_SKIP * 2
  204.             If thrust < 0 Then
  205.                 thrust = 0
  206.             End If
  207.         End If
  208.  
  209.         'If degrees >= 360 Then
  210.         '    degrees = degrees Mod 360
  211.         'ElseIf degrees < 0 Then
  212.         '    degrees = 360 + degrees
  213.         'End If
  214.  
  215.  
  216.         radians = degrees * (PI / 180)
  217.         'lblDegrees.Caption = degrees & " degrees"
  218.  
  219.         'NOTE:
  220.         'Point (X1,Y1) never moves.  It is fixed at (3000,3000)
  221.         'Point (X2,Y2) is the 2nd endpoint.
  222.         'This one will get moved, to make the line appear to rotate
  223.  
  224.         'To determine X2, start with the X1 value,
  225.         ' and ADD a certain amount based on the DEGREES  (using COS)
  226.         p2.X = p1.X + (Math.Cos(radians) * GUN_LENGTH)
  227.         'Line1.x2 = Line1.x1 + (Math.Cos(radians) * GUN_LENGTH)
  228.  
  229.  
  230.         'To determine Y2, start with the Y1 value,
  231.         'and add a certain amount based on the DEGREES  (using SIN)
  232.         '
  233.         'NOTE!! You need to subtract the sin adjustment b/c it's not a true x/y plane.
  234.         'B/c in VB6, the Y gets higher when you go DOWN the screen
  235.         p2.Y = p1.Y - (Math.Sin(radians) * GUN_LENGTH)
  236.         'Line1.y2 = Line1.y1 - (Math.Sin(radians) * GUN_LENGTH)
  237.  
  238.         'move the ship
  239.         ship.rect.Y = ship.rect.Y + thrust * Math.Cos(radians + (PI / 2))
  240.         ship.rect.X = ship.rect.X + thrust * Math.Sin(radians + (PI / 2))
  241.         p1.X = ship.rect.X + meRadius
  242.         p1.Y = ship.rect.Y + meRadius
  243.  
  244.         'check if it went off the screen (4 ways)
  245.         'if it did, then reset it on the other side of the screen
  246.         'off the left side?
  247.         If ship.rect.X < 0 Then
  248.             ship.rect.X = Me.Width
  249.             'off the right side?
  250.         ElseIf ship.rect.X > Me.Width Then
  251.             ship.rect.X = 0
  252.             'off the top?
  253.         ElseIf ship.rect.Y < 0 Then
  254.             ship.rect.Y = Me.Height
  255.             'off the bottom?
  256.         ElseIf ship.rect.Y > Me.Height Then
  257.             ship.rect.Y = 0
  258.         End If
  259.  
  260.  
  261.     End Sub
  262.     Public Sub fireBullet()
  263.  
  264.         Dim r As Rectangle
  265.         Dim b As Asteroid
  266.  
  267.         foundbullet = False
  268.         For Each b In BulletList
  269.             If b.visible = False Then
  270.                 b.visible = True
  271.                 b.rect.Y = p2.Y
  272.                 b.rect.X = p2.X
  273.                 b.speedX = bulletSpeedMult * Math.Sin(radians + (PI / 2))
  274.                 b.speedY = bulletSpeedMult * Math.Cos(radians + (PI / 2))
  275.                 foundbullet = True
  276.                 Exit For
  277.             End If
  278.         Next
  279.  
  280.         'if there are no bullets available, then load a new one (up to 3)
  281.         If numbullets < MAX_BULLETS And foundbullet = False Then
  282.             b = New Asteroid
  283.             r = New Rectangle
  284.             r.Y = p2.Y
  285.             r.X = p2.X
  286.             r.Height = bulletRadius * 2
  287.             r.Width = bulletRadius * 2
  288.             b.rect = r
  289.             b.speedX = bulletSpeedMult * Math.Sin(radians + (PI / 2))
  290.             b.speedY = bulletSpeedMult * Math.Cos(radians + (PI / 2))
  291.             b.visible = True
  292.             BulletList.Add(b)
  293.             numbullets = numbullets + 1
  294.         End If
  295.  
  296.  
  297.     End Sub
  298.  
  299.  
  300.     Private Sub tmrmovebullet_Tick(sender As Object, e As EventArgs) Handles tmrmovebullet.Tick
  301.         For Each b In BulletList
  302.             If b.visible = True Then
  303.                 b.rect.X = b.rect.X + b.speedX
  304.                 b.rect.Y = b.rect.Y + b.speedY
  305.             End If
  306.  
  307.             'if the bullet is off the screen, then hide it
  308.             If OffScreen(b.rect) Then
  309.                 b.visible = False
  310.             End If
  311.         Next
  312.  
  313.     End Sub
  314.  
  315.  
  316.     Public Function OffScreen(r As Rectangle) As Boolean
  317.  
  318.         If r.X < 0 Or r.X > Me.Width Or r.Y < 0 Or r.Y > Me.Height Then
  319.             Return True
  320.         Else
  321.             Return False
  322.         End If
  323.  
  324.     End Function
  325.  
  326.     Public Sub resetScreen()
  327.  
  328.         For Each a In AsteroidList
  329.             a.visible = True
  330.             a.rect.Y = Int(Rnd() * Me.Height)
  331.             a.rect.X = Int(Rnd() * Me.Width)
  332.         Next
  333.         ship.visible = True
  334.  
  335.     End Sub
  336.  
  337.     Private Sub tmrDetectHit_Tick(sender As Object, e As EventArgs) Handles tmrDetectHit.Tick
  338.         Dim acx As Integer
  339.         Dim acy As Integer
  340.         Dim bcx As Integer
  341.         Dim bcy As Integer
  342.         Dim d As Single
  343.  
  344.         For Each a In AsteroidList
  345.             acx = a.rect.X + asteriodRadius
  346.             acy = a.rect.Y + asteriodRadius
  347.  
  348.             'check EACH bullet (x), against hitting EACH rock (y)
  349.             For Each b In BulletList
  350.                 bcx = b.rect.X + bulletRadius
  351.                 bcy = b.rect.Y + bulletRadius
  352.                 d = distance(acx, acy, bcx, bcy)
  353.  
  354.                 If (d < asteriodRadius + bulletRadius) And a.visible = True And b.visible = True Then
  355.                     a.visible = False
  356.                     b.visible = False
  357.                     score = score + 1
  358.                     lblScore.Text = score
  359.                 End If
  360.             Next
  361.  
  362.             'is it hitting me?
  363.             bcx = ship.rect.X + meRadius
  364.             bcy = ship.rect.Y + meRadius
  365.             d = distance(acx, acy, bcx, bcy)
  366.  
  367.             If (d < asteriodRadius + meRadius) And a.visible = True And ship.visible = True Then
  368.                 ship.visible = False
  369.                 MsgBox("DEAD")
  370.                 score = 0
  371.                 lblScore.Text = score
  372.                 thrust = 0
  373.                 Call resetScreen()
  374.                 pressingkeyright = False
  375.                 pressingkeyleft = False
  376.                 pressingkeydown = False
  377.                 pressingkeyup = False
  378.             End If
  379.  
  380.         Next
  381.     End Sub
  382.  
  383.     Public Function levelOver() As Boolean
  384.  
  385.         'if there are ANY visible asteriods,
  386.         'then the level is NOT over
  387.  
  388.         For Each a In AsteroidList
  389.             If a.visible = True Then
  390.                 Return False
  391.             End If
  392.         Next
  393.         Return True
  394.  
  395.     End Function
  396.  
  397.     Private Sub tmrCheckNewLevel_Tick(sender As Object, e As EventArgs) Handles tmrCheckNewLevel.Tick
  398.         If levelOver() = True Then
  399.             Call resetScreen()
  400.         End If
  401.  
  402.     End Sub
  403.  
  404. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement