Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- Private init As Boolean = True
- Private ticks = 0
- 'frames per row
- Private fpr As Integer = 1
- Private WASD(3) As Integer
- Const CELLSIZE As Integer = 32
- Const SCREENWIDTH As Integer = (1280 / CELLSIZE) - 1
- Const SCREENHEIGHT As Integer = (720 / CELLSIZE)
- Private SPEED As Integer
- Private gameOver As Integer = True
- Private colArr() As Brush = {New SolidBrush(Color.Black), New SolidBrush(Color.White), New SolidBrush(Color.Red), New SolidBrush(Color.Blue)}
- Private backgroundPos(SCREENHEIGHT, 1) As Integer
- Private rectArr(SCREENHEIGHT * 2) As Rectangle
- Private Structure Player
- Dim loc As Point
- Dim size As Size
- Dim colour As Brush
- End Structure
- Private car As Player
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- SPEED = ticker.Interval * 5
- Me.DoubleBuffered = True
- SetStyle(ControlStyles.DoubleBuffer, True)
- SetStyle(ControlStyles.UserPaint, True)
- SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
- SetStyle(ControlStyles.AllPaintingInWmPaint, True)
- End Sub
- Private Sub startGame()
- With car
- .loc = New Point(1280 / 2, 720 / 2)
- .size = New Size(CELLSIZE, CELLSIZE * 2)
- .colour = colArr(2)
- End With
- For i = 0 To SCREENHEIGHT
- backgroundPos(i, 0) = 14
- backgroundPos(i, 1) = SCREENWIDTH - 14
- Next
- gameOver = False
- ticker.Enabled = True
- End Sub
- Private Sub endGame()
- gameOver = True
- ticker.Enabled = False
- End Sub
- Private Function getLocation(x, y)
- Dim loc As New Point
- If init Then
- loc = New Point(x * CELLSIZE, (y - 1) * CELLSIZE)
- init = False
- Else
- loc = New Point((x * CELLSIZE), ((y - 1) * CELLSIZE) + (ticks Mod (fpr)) * ((CELLSIZE) / fpr))
- End If
- Label1.Text = fpr
- Return loc
- End Function
- Private Sub generateFrame()
- Dim newRow(SCREENWIDTH) As Integer
- For i = 0 To SCREENWIDTH
- newRow(i) = 0
- Next
- For i = SCREENHEIGHT To 1 Step -1
- For j = 0 To 1
- backgroundPos(i, j) = backgroundPos(i - 1, j)
- Next
- Next
- Dim random As Integer = CInt((3 * Rnd()) + 1)
- If (ticks / (fpr)) Mod 5 = 0 Then
- If random = 1 And backgroundPos(0, 1) < SCREENWIDTH Then
- backgroundPos(0, 0) += 1
- backgroundPos(0, 1) += 1
- ElseIf random = 2 And backgroundPos(0, 0) > 0 Then
- backgroundPos(0, 0) -= 1
- backgroundPos(0, 1) -= 1
- End If
- End If
- End Sub
- Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
- Console.WriteLine("startGame")
- Dim down = e.KeyData
- If down = 87 Then
- WASD(0) = SPEED
- ElseIf down = 65 Then
- WASD(1) = SPEED
- ElseIf down = 83 Then
- WASD(2) = SPEED
- ElseIf down = 68 Then
- WASD(3) = SPEED
- End If
- End Sub
- Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
- Dim up = e.KeyData
- If up = 87 Then
- WASD(0) = 0
- ElseIf up = 65 Then
- WASD(1) = 0
- ElseIf up = 83 Then
- WASD(2) = 0
- ElseIf up = 68 Then
- WASD(3) = 0
- End If
- End Sub
- Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles ticker.Tick
- ticks += 1
- If (ticks Mod (fpr)) = 0 Then
- generateFrame()
- End If
- Dim x As Integer = car.loc.X
- Dim y As Integer = car.loc.Y
- Dim newLoc = New Point(x - WASD(1) + WASD(3), y - WASD(0) + WASD(2))
- If newLoc.Y > 0 And newLoc.Y < (Me.ClientSize.Height - car.size.Height) Then
- car.loc = newLoc
- End If
- Me.Refresh()
- End Sub
- Private Sub collision()
- 'MsgBox("oof")
- End Sub
- Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
- Dim carRect = New Rectangle(car.loc, car.size)
- Dim edge = False
- Dim flag = False
- Dim colourValue As Brush = colArr(0)
- Dim sizeValue As New Size(CELLSIZE, CELLSIZE)
- If gameOver = False Then
- For i = 0 To SCREENHEIGHT
- For j = 0 To SCREENWIDTH
- If backgroundPos(i, 0) = j Then
- colourValue = colArr(3)
- flag = True
- edge = True
- ElseIf backgroundPos(i, 1) = j Then
- colourValue = colArr(3)
- edge = True
- flag = False
- End If
- Dim rect As New Rectangle(getLocation(j, i), sizeValue)
- e.Graphics.FillRectangle(colourValue, rect)
- If (edge) And (carRect.IntersectsWith(rect)) Then
- collision()
- End If
- If flag = False Then
- colourValue = colArr(0)
- End If
- If flag = True Then
- colourValue = colArr(1)
- End If
- edge = False
- Next
- Next
- e.Graphics.FillRectangle(car.colour, New Rectangle(car.loc, car.size))
- Else
- End If
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- startGame()
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- endGame()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement