Advertisement
Pepsomo

For Jack

Apr 27th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. Public Class Form1
  2. Dim Upc As Boolean
  3. Dim Downc As Boolean
  4. Dim Leftc As Boolean
  5. Dim rightc As Boolean
  6. Dim score As Integer = 0
  7. Dim lives As Integer = 3
  8. Public Property Movement As Object
  9. Public Property PitureBox2 As Object
  10.  
  11. Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
  12.  
  13.  
  14.  
  15.  
  16. If e.KeyCode = Keys.Up Then
  17. Upc = True
  18. End If
  19. If e.KeyCode.Down Then
  20. Downc = True
  21. End If
  22. If e.KeyCode.Left Then
  23. Leftc = True
  24. End If
  25. If e.KeyCode.Right Then
  26. rightc = True
  27. End If
  28. If e.KeyCode = Keys.Escape Then
  29. PictureBox3.Top = 300
  30. PictureBox3.Top = 300
  31. PictureBox1.Left = 0
  32. PictureBox1.Top = 0
  33. lives = 3
  34. Label1.Text = "Lives: " & lives
  35. score = 0
  36. Label2.Text = "Score: " & score
  37. rightc = False
  38. Upc = False
  39. Downc = False
  40. Leftc = False
  41. Movement.Start()
  42. End If
  43. End Sub
  44.  
  45.  
  46. Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
  47. If e.KeyCode = Keys.Up Then
  48. Upc = False
  49. End If
  50. If e.KeyCode.Down Then
  51. Downc = False
  52. End If
  53. If e.KeyCode.Left Then
  54. Leftc = False
  55. End If
  56.  
  57. If e.KeyCode.Right Then
  58. rightc = False
  59.  
  60. Label1.Text = "Left:" & PictureBox1.Left
  61. Label2.Text = "Left:" & PictureBox1.Top
  62. End If
  63. End Sub
  64.  
  65. Private Sub MovementT_Tick(sender As Object, e As EventArgs) Handles MovementT.Tick
  66. Chase(PictureBox3, PictureBox2)
  67. If Upc = True Then
  68. PictureBox1.Top -= 5
  69.  
  70. If PictureBox1.Top <= 0 Then
  71. PictureBox1.Top = 0
  72. End If
  73. End If
  74. If Downc = True Then
  75. PictureBox1.Top += 5
  76. If PictureBox1.Top + PictureBox1.Height >= Me.Height Then
  77. PictureBox1.Top = Me.Height - PictureBox1.Height
  78. End If
  79. End If
  80. If Leftc = True Then
  81. PictureBox1.Left -= 5
  82. End If
  83. If PictureBox1.Left <= 0 Then
  84. PictureBox1.Top = 0
  85. End If
  86. If rightc = True Then
  87. PictureBox1.Left += 5
  88. End If
  89. If PictureBox1.Left + PictureBox1.Width >= Me.Width Then
  90. PictureBox1.Left = Me.Width - PictureBox1.Width
  91. End If
  92. If Collision(PictureBox1, PictureBox2) = True Then
  93. PictureBox2.Top = Int(Rnd() * (480 - 2 * PictureBox2.Height) + PictureBox2.Height)
  94. PictureBox2.Left = Int(Rnd() * (640 - 2 * PictureBox2.Width) + PictureBox2.Width)
  95. PictureBox2.BringToFront()
  96. Debug.WriteLine("gem top" & PitureBox2.Top)
  97. Debug.WriteLine("Gem left" & PictureBox2.Left)
  98. score = score + 1
  99. Label1.Text = "Score " & score
  100. End If
  101. If Collision(PictureBox1, PictureBox3) Then
  102. If lives > 1 Then
  103. lives = lives - 1
  104. Label1.Text = "Lives : " & lives
  105. 'reset position of player'
  106. PictureBox1.Left = 0
  107. PictureBox1.Top = 0
  108. 'reset position of attack'
  109. PictureBox3.Top = 300
  110. PictureBox3.Left = 300
  111. Else
  112. lives = lives - 1
  113. Label1.Text = "Lives: " & lives
  114. MovementT.Stop()
  115. MsgBox("Game Over")
  116. End If
  117. End If
  118. End Sub
  119.  
  120. Private Function Collision(ByVal Object1 As Object, ByVal Object2 As Object) As Boolean
  121. Dim Collided As Boolean = False
  122. If Object1.Top + Object1.Height >= Object2.Top And
  123. Object2.Top + Object2.Height >= Object1.Top And
  124. Object1.Left + Object1.Width >= Object2.Left And
  125. Object2.Left + Object2.Width >= Object2.Left Then
  126. Collided = True
  127. End If
  128. Return Collided
  129. End Function
  130. Private Function Chase(ByRef Object1 As Object, ByRef Object2 As Object)
  131. 'If the enemy object is to right of player, then enemy object will move to left'
  132. If Object1.Left + Object1.Width > Object2.Left Then
  133. Object1.Left -= 1
  134. End If
  135. 'If the enemy object is to left of player, then object will move right'
  136. If Object1.Left + Object1.Width < Object2.Left Then
  137. Object1.Left += 1
  138. End If
  139. 'If the enemy object is lower of the player, then enemy object will move up'
  140. If Object1.Top + Object1.height > Object2.Top Then
  141. Object1.Top -= 1
  142. End If
  143. 'if the enenmy object is higher of player, then enemy object will move down'
  144. If Object1.Top + Object1.height < Object2.Top Then
  145. Object1.Top += 1
  146. End If
  147. End Function
  148.  
  149. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  150.  
  151. Me.KeyPreview = True
  152. Me.Size = New Size(800, 600)
  153. Me.CenterToScreen()
  154.  
  155. Label1.Text = "Left:" & PictureBox1.Left
  156. Label2.Text = "Top:" & PictureBox1.Top
  157. Label3.Text = "Screen:" & Me.Width & "*" & Me.Height
  158. End Sub
  159.  
  160. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  161. 'Save score to a csv database file on desktop'
  162. Dim FileName, SaveInfoStr As String
  163. FileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Score.csv"
  164.  
  165. Dim out As IO.StreamWriter = System.IO.File.AppendText(FileName)
  166.  
  167. SaveInfoStr = Label1.Text & "," & Label2.Text
  168.  
  169. out.WriteLine(SafeInfoStr)
  170. out.Close()
  171.  
  172. MsgBox("Your Score has been saved to file on Desktop")
  173. End Sub
  174.  
  175. Private Function SafeInfoStr() As Char
  176. Throw New NotImplementedException()
  177. End Function
  178. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement