Public Class Form1
Dim WithEvents TimerX As New Timer With {.Enabled = True, .Interval = 10}
Dim WithEvents TimerY As New Timer With {.Enabled = True, .Interval = 10}
Dim WithEvents TimerGravity As New Timer With {.Enabled = False, .Interval = 100}
Dim PlayerWidth As Integer = 20
Dim playerHeight As Integer = 20
'This is your Player
Dim playChar As PictureBox
'This rectangle represente the window,if the player does not intersec with this rectangle
'it means that it is about to move out of the window
Dim theBox As Rectangle
'This rectangle is your floor, if the player intersec with it, it means that it touch the floor
Dim TheFloor As Rectangle
'These are your movement variable (Higher the value faster your player goes
Dim SpeedX As Integer = 0
Dim SpeedY As Integer = 0
'This is your gravity Variable, Higher the value, stronger is the gravity
Dim Gravity As Integer = 1.5
'This is your jump variable, higher the value, higher is the jump
'We set the negative value of the speed toward the top of the form, this
'simplify the math
Dim JumpSpeed As Integer = -5.6
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.A ' Going toward the left
playChar.Image = Image.FromFile("C:\Users\Nick Lopez\Documents\Visual Studio 2010\Projects\n_platform\n_platform\Resources\player_walk_2_l.png")
SpeedX = -1.5
Case Keys.D ' Going toward the right
playChar.Image = Image.FromFile("C:\Users\Nick Lopez\Documents\Visual Studio 2010\Projects\n_platform\n_platform\Resources\player_walk_2_r.png")
SpeedX = 1.5
Case Keys.W ' Jump
'Create a test rectangle before jumping to see if the player touch the ground
Dim Test As Rectangle = New Rectangle(New Point(playChar.Location.X, playChar.Location.Y + 2), playChar.Size)
'If The test rectangle intersect with the floor, allow the jump
If Test.IntersectsWith(TheFloor) Then
'Perform the jump
SpeedY = JumpSpeed
'Start the gravite meter
TimerGravity.Enabled = True
End If
End Select
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyCode
Case Keys.A ' stop moving when the key is release
playChar.Image = Image.FromFile("C:\Users\Nick Lopez\Documents\Visual Studio 2010\Projects\n_platform\n_platform\Resources\player_stand_l.png")
SpeedX = 0
Case Keys.D ' stop moving when the key is release
playChar.Image = Image.FromFile("C:\Users\Nick Lopez\Documents\Visual Studio 2010\Projects\n_platform\n_platform\Resources\player_stand_r.png")
SpeedX = 0
End Select
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Set up the rectangle
Me.Size = New Size(320, 240)
theBox = New Rectangle(PlayerWidth, playerHeight, Me.ClientSize.Width - (PlayerWidth * 2), Me.ClientSize.Height - (playerHeight * 2))
playChar = PlayModel
TheFloor = New Rectangle(33, 140, 233, 156)
Me.DoubleBuffered = True
End Sub
Private Sub TimerX_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerX.Tick
'Create a test rectangle before moving the player to see if he is about to move out of the screen
Dim Test As Rectangle = New Rectangle(New Point(playChar.Location.X + SpeedX, playChar.Location.Y), playChar.Size)
' If not moving out of the screen then move
'NB If there is some obstacles, you will also test here if the Test rectangle intersect with the obstacle before moving
If Test.IntersectsWith(theBox) Then
playChar.Location = New Point(playChar.Location.X + SpeedX, playChar.Location.Y)
End If
'redraw the screen
Me.Refresh()
End Sub
Private Sub TimerY_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerY.Tick
playChar.Location = New Point(playChar.Location.X, playChar.Location.Y + SpeedY)
' if the player touches the floor
If playChar.Bounds.IntersectsWith(TheFloor) Then
' stop the downward movement
SpeedY = 0
'replace the player 1 pixel above the floor
playChar.Location = New Point(playChar.Location.X, levelGround.ClientSize.Height - playerHeight + 113)
'stop the gravity timer
TimerGravity.Enabled = False
End If
'redraw the screen
Me.Refresh()
End Sub
Private Sub TimerGravity_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerGravity.Tick
SpeedY += Gravity
End Sub
End Class