Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. Public Class Form1
  2. Private init As Boolean = True
  3. Private ticks = 0
  4. 'frames per row
  5. Private fpr As Integer = 1
  6. Private WASD(3) As Integer
  7.  
  8. Const CELLSIZE As Integer = 32
  9. Const SCREENWIDTH As Integer = (1280 / CELLSIZE) - 1
  10. Const SCREENHEIGHT As Integer = (720 / CELLSIZE)
  11. Private SPEED As Integer
  12. Private gameOver As Integer = True
  13. Private colArr() As Brush = {New SolidBrush(Color.Black), New SolidBrush(Color.White), New SolidBrush(Color.Red), New SolidBrush(Color.Blue)}
  14. Private backgroundPos(SCREENHEIGHT, 1) As Integer
  15. Private rectArr(SCREENHEIGHT * 2) As Rectangle
  16.  
  17. Private Structure Player
  18. Dim loc As Point
  19. Dim size As Size
  20. Dim colour As Brush
  21. End Structure
  22.  
  23. Private car As Player
  24.  
  25. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  26. SPEED = ticker.Interval * 5
  27.  
  28. Me.DoubleBuffered = True
  29. SetStyle(ControlStyles.DoubleBuffer, True)
  30. SetStyle(ControlStyles.UserPaint, True)
  31. SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
  32. SetStyle(ControlStyles.AllPaintingInWmPaint, True)
  33.  
  34. End Sub
  35.  
  36. Private Sub startGame()
  37. With car
  38. .loc = New Point(1280 / 2, 720 / 2)
  39. .size = New Size(CELLSIZE, CELLSIZE * 2)
  40. .colour = colArr(2)
  41. End With
  42.  
  43. For i = 0 To SCREENHEIGHT
  44. backgroundPos(i, 0) = 14
  45. backgroundPos(i, 1) = SCREENWIDTH - 14
  46. Next
  47.  
  48. gameOver = False
  49. ticker.Enabled = True
  50. End Sub
  51.  
  52. Private Sub endGame()
  53. gameOver = True
  54. ticker.Enabled = False
  55. End Sub
  56.  
  57. Private Function getLocation(x, y)
  58. Dim loc As New Point
  59. If init Then
  60. loc = New Point(x * CELLSIZE, (y - 1) * CELLSIZE)
  61. init = False
  62. Else
  63. loc = New Point((x * CELLSIZE), ((y - 1) * CELLSIZE) + (ticks Mod (fpr)) * ((CELLSIZE) / fpr))
  64. End If
  65. Label1.Text = fpr
  66. Return loc
  67. End Function
  68.  
  69. Private Sub generateFrame()
  70. Dim newRow(SCREENWIDTH) As Integer
  71. For i = 0 To SCREENWIDTH
  72. newRow(i) = 0
  73. Next
  74.  
  75. For i = SCREENHEIGHT To 1 Step -1
  76. For j = 0 To 1
  77. backgroundPos(i, j) = backgroundPos(i - 1, j)
  78. Next
  79. Next
  80.  
  81. Dim random As Integer = CInt((3 * Rnd()) + 1)
  82. If (ticks / (fpr)) Mod 5 = 0 Then
  83. If random = 1 And backgroundPos(0, 1) < SCREENWIDTH Then
  84. backgroundPos(0, 0) += 1
  85. backgroundPos(0, 1) += 1
  86. ElseIf random = 2 And backgroundPos(0, 0) > 0 Then
  87. backgroundPos(0, 0) -= 1
  88. backgroundPos(0, 1) -= 1
  89. End If
  90. End If
  91. End Sub
  92.  
  93. Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
  94. Console.WriteLine("startGame")
  95. Dim down = e.KeyData
  96. If down = 87 Then
  97. WASD(0) = SPEED
  98. ElseIf down = 65 Then
  99. WASD(1) = SPEED
  100. ElseIf down = 83 Then
  101. WASD(2) = SPEED
  102. ElseIf down = 68 Then
  103. WASD(3) = SPEED
  104. End If
  105. End Sub
  106.  
  107. Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
  108. Dim up = e.KeyData
  109. If up = 87 Then
  110. WASD(0) = 0
  111. ElseIf up = 65 Then
  112. WASD(1) = 0
  113. ElseIf up = 83 Then
  114. WASD(2) = 0
  115. ElseIf up = 68 Then
  116. WASD(3) = 0
  117. End If
  118. End Sub
  119.  
  120. Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles ticker.Tick
  121. ticks += 1
  122. If (ticks Mod (fpr)) = 0 Then
  123. generateFrame()
  124. End If
  125.  
  126. Dim x As Integer = car.loc.X
  127. Dim y As Integer = car.loc.Y
  128. Dim newLoc = New Point(x - WASD(1) + WASD(3), y - WASD(0) + WASD(2))
  129. If newLoc.Y > 0 And newLoc.Y < (Me.ClientSize.Height - car.size.Height) Then
  130. car.loc = newLoc
  131. End If
  132. Me.Refresh()
  133. End Sub
  134.  
  135. Private Sub collision()
  136. 'MsgBox("oof")
  137. End Sub
  138.  
  139. Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
  140. Dim carRect = New Rectangle(car.loc, car.size)
  141. Dim edge = False
  142. Dim flag = False
  143. Dim colourValue As Brush = colArr(0)
  144. Dim sizeValue As New Size(CELLSIZE, CELLSIZE)
  145. If gameOver = False Then
  146. For i = 0 To SCREENHEIGHT
  147. For j = 0 To SCREENWIDTH
  148. If backgroundPos(i, 0) = j Then
  149. colourValue = colArr(3)
  150. flag = True
  151. edge = True
  152. ElseIf backgroundPos(i, 1) = j Then
  153. colourValue = colArr(3)
  154. edge = True
  155. flag = False
  156. End If
  157.  
  158. Dim rect As New Rectangle(getLocation(j, i), sizeValue)
  159. e.Graphics.FillRectangle(colourValue, rect)
  160.  
  161. If (edge) And (carRect.IntersectsWith(rect)) Then
  162. collision()
  163. End If
  164.  
  165. If flag = False Then
  166. colourValue = colArr(0)
  167. End If
  168. If flag = True Then
  169. colourValue = colArr(1)
  170. End If
  171. edge = False
  172. Next
  173. Next
  174.  
  175.  
  176. e.Graphics.FillRectangle(car.colour, New Rectangle(car.loc, car.size))
  177. Else
  178.  
  179. End If
  180. End Sub
  181.  
  182. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  183. startGame()
  184. End Sub
  185.  
  186. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  187. endGame()
  188. End Sub
  189. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement