Advertisement
ArxkRBLX

Untitled

Dec 14th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. -- Arxk
  2. Made this little game a while ago, most of the things I created in Visual Studio were deleted transferring computers, so I don't have that many examples. It's just a little space invaders game.
  3. -- Arxk
  4.  
  5. Public Class Form3
  6. Dim ShooterSpeed As Integer
  7. Dim InvaderSpeed As Integer
  8. Dim ShotSpeed As Integer
  9. Dim InvaderDrop As Integer
  10.  
  11. Dim Level2 As Boolean
  12. Dim Level3 As Boolean
  13.  
  14. Dim box As PictureBox = New PictureBox
  15.  
  16. Dim Score As Integer
  17. Dim HighScore As Integer
  18. Dim abletomove As Boolean
  19.  
  20. Dim SRight As Boolean
  21. Dim SLeft As Boolean
  22.  
  23. Const NumOfInvaders As Integer = 25
  24. Dim IRight(NumOfInvaders) As Boolean
  25. Dim Invaders(NumOfInvaders) As PictureBox
  26. Dim x As Integer
  27.  
  28. Dim intCount As Integer
  29.  
  30. Dim shotDown As Integer
  31.  
  32. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  33. // This would load the form, setting all the values to default to make sure no previous values are shown.
  34. abletomove = True
  35. ShotSpeed = 9
  36. ShooterSpeed = 3
  37. InvaderSpeed = 5
  38. InvaderDrop = 42
  39.  
  40. // Start loading the invaders
  41. LoadInvaders()
  42. For Me.x = 1 To NumOfInvaders
  43. IRight(x) = True
  44. Next
  45. Score = 0
  46. HighScore = 0
  47. Label3.Visible = False
  48. Shot.Visible = False
  49. End Sub
  50.  
  51. Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
  52. // This would handle the pressing of keys, so pressing left sets the left value to true and moving you, then of course pressing up just shoots the invaders. Makes it visible etc.
  53.  
  54. If abletomove = True Then
  55. Select Case e.KeyCode
  56. Case Keys.Left
  57. SLeft = True
  58. SRight = False
  59. Case Keys.Right
  60. SRight = True
  61. SLeft = False
  62. Case Keys.Up
  63. If Shot.Visible = False Then
  64. Shot.Top = PictureBox1.Top
  65. Shot.Left = PictureBox1.Left + (PictureBox1.Width / 2) - (Shot.Width / 2)
  66. Shot.Visible = True
  67. End If
  68. End Select
  69. End If
  70. End Sub
  71.  
  72. Private Sub FormMain_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
  73. // When letting go of your keys, it would set those left and right values back to false, pretty self-explanatory.
  74.  
  75. If abletomove = True Then
  76. If e.KeyValue = Keys.Right Then
  77. SRight = False
  78. End If
  79. If e.KeyValue = Keys.Left Then
  80. SLeft = False
  81. End If
  82. End If
  83. End Sub
  84.  
  85. Private Sub FireShot()
  86. // This handles the firing of the bullet, makes sure that when it reaches the top, it is set to invisible.
  87.  
  88. If Shot.Visible = True Then
  89. Shot.Top -= ShotSpeed
  90. End If
  91.  
  92. If Shot.Top + Shot.Height < Me.ClientRectangle.Top Then
  93. Shot.Visible = False
  94. End If
  95. End Sub
  96.  
  97. Private Sub CheckHit()
  98. // This will check if one of the invaders are hit, and when they are hit, it will make the invader invisible.
  99.  
  100. For Me.x = 1 To NumOfInvaders
  101. If (Shot.Top + Shot.Height >= Invaders(x).Top) And (Shot.Top <= Invaders(x).Top + Invaders(x).Height) And (Shot.Left + Shot.Width >= Invaders(x).Left) And (Shot.Left <= Invaders(x).Left + Invaders(x).Width) And Shot.Visible = True And Invaders(x).Visible = True Then
  102. Invaders(x).Visible = False
  103. Shot.Visible = False
  104. shotDown += 1
  105. End If
  106. Next
  107. End Sub
  108.  
  109. Private Sub MoveInvader()
  110. // This would handle the moving of the invaders, obviously having an integer as InvaderSpeed, controlling it with a timer.
  111.  
  112. For Me.x = 1 To NumOfInvaders
  113. If IRight(x) = True Then
  114. Invaders(x).Left += InvaderSpeed
  115. Else
  116. Invaders(x).Left -= InvaderSpeed
  117. End If
  118.  
  119. If Invaders(x).Left + Invaders(x).Width > Me.ClientRectangle.Width And IRight(x) = True Then
  120. IRight(x) = False
  121. Invaders(x).Top += InvaderDrop
  122. End If
  123.  
  124. If Invaders(x).Left < Me.ClientRectangle.Left And IRight(x) = False Then
  125. IRight(x) = True
  126. Invaders(x).Top += InvaderDrop
  127. End If
  128. Next
  129. End Sub
  130.  
  131. Private Sub CheckGameOver()
  132. // This handles what happens after the game ends, it will check if all the invaders are gone, and if they are then it will do everything necessary to restart the game and prompt a messaging telling them the game is over.
  133.  
  134. For Me.x = 1 To NumOfInvaders
  135. If Invaders(x).Top + Invaders(x).Height >= PictureBox1.Top And Invaders(x).Visible = True Then
  136. My.Settings.MediumScore = Score
  137. TmrShoot.Enabled = False
  138. abletomove = False
  139. SRight = False
  140. SLeft = False
  141. coinsUp.Enabled = False
  142. Label3.Visible = True
  143. Me.BackColor = Color.Red
  144. MsgBox("Game OVER! CLICK OK TO CLOSE!")
  145. Me.Close()
  146. End If
  147. Next
  148. If shotDown = NumOfInvaders Then
  149. My.Settings.MediumScore = Score
  150. Level2 = True
  151. TmrShoot.Enabled = False
  152. Me.x = NumOfInvaders
  153. SRight = False
  154. SLeft = False
  155. Label4.Visible = True
  156. Me.BackColor = Color.White
  157. Score = 0
  158. Label1.Text = "SCORE: " & Score
  159. coinsUp.Enabled = False
  160. Label3.Visible = False
  161. abletomove = False
  162. MsgBox("YOU WON! CLICK OK TO CLOSE!")
  163. Me.Close()
  164. End If
  165. End Sub
  166.  
  167. Private Sub MoveShooter()
  168. // This will move the shooter, basically the same method as moving the invaders.
  169.  
  170. If SRight = True And PictureBox1.Left + PictureBox1.Width < Me.ClientRectangle.Width Then
  171. PictureBox1.Left += ShooterSpeed
  172. End If
  173. If SLeft = True And PictureBox1.Left > Me.ClientRectangle.Left Then
  174. PictureBox1.Left -= ShooterSpeed
  175. End If
  176. End Sub
  177.  
  178. Private Sub Button1_Click(sender As Object, e As EventArgs)
  179. // Just handles the click of the start button, it will load the invaders and start the game.
  180.  
  181. LoadInvaders()
  182. abletomove = True
  183. TmrShoot.Enabled = True
  184. coinsUp.Enabled = True
  185. End Sub
  186.  
  187. Private Sub coinsUp_Tick(sender As Object, e As EventArgs) Handles coinsUp.Tick
  188. Score = Score + 3
  189. Label1.Text = "SCORE: " & Score
  190. End Sub
  191.  
  192. Private Sub LoadInvaders()
  193. // This will load the invaders, it's called when the game originally starts and they are removed once the game has ended.
  194.  
  195. For Me.x = 1 To NumOfInvaders
  196. Invaders(x) = New PictureBox
  197. Invaders(x).SizeMode = PictureBoxSizeMode.StretchImage
  198. Invaders(x).Image = My.Resources.invader
  199. Invaders(x).Width = 40
  200. Invaders(x).Height = 32
  201. Invaders(x).BackColor = Color.Transparent
  202. Invaders(x).Left = (-50 * x) - (x * 5)
  203. Controls.Add(Invaders(x))
  204. Next
  205. End Sub
  206.  
  207. Private Sub TmrShoot_Tick(sender As Object, e As EventArgs) Handles TmrShoot.Tick
  208. // This timer handles everything, from firing shots to the movement of the shooter and the invaders moving.
  209.  
  210. FireShot()
  211. MoveShooter()
  212. CheckHit()
  213. CheckGameOver()
  214. MoveInvader()
  215. End Sub
  216. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement