|
-
Dec 22nd, 2010, 07:56 PM
#1
Thread Starter
New Member
Help with platform engine
Hello (first post). My name is DoctorN (but seemingly, that name was taken). I am working on a platform engine for my company. At this beginning moment, gameplay will be like an old case of super mairo. However, im at a stump, and can continue no farther unless I get some help. My character can not freely fall on his own, he has to jump in order to fall off the platform (if i jump off of the platform that is). He has to be perfectly alligned on top of the platform if he wishes to jump (so if i spawn him not on the platform, he doesnt fall, nor can he jump, just move left and right). And you'll see in the TimerY_Tick, theres a +113. that is because when he lands, thats the Y coordinate that he must land on (becuase 114 and below is the platform I guess). Can you look and adjust my code to those three things, or at least guide me in the right path? 1.wherever he spawns, hell fall if theres no ground underneath him. 2. there doesnt need to be a precise code where he has to land. thanks. heres the code
Code:
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|