Here we are at section 6, an introduction to Platforms. Now, previously, we've gotten our character to jump around a bit and walk to the edges of the screen, but we don't have anything interesting on the screen yet. That is about to change now. We are going to add platforms. This platform is only going to be the kind where you can walk in front of it and then jump onto it. First, we need to store the platforms into something. The platform itself only needs to be a Rectangle for now. If I decide on making bizarre-shaped platforms, then of course we'll have to modify the platform storage. So, ideally, the platforms will eventually come from a file that will have all of the information necessary for recreating a level. But, right now, we will start off with a simple one platform that will be hard-coded in the form class.
Dim Platform As Rectangle 'A platform. |
Platform = Rectangle.FromLTRB(150, 515, 210, LANDHEIGHT) 'Instantiate our platform |
GFX.FillRectangle(Brushes.Maroon, Platform) GFX.DrawRectangle(Pens.Chocolate, Platform) 'I'm not into interior decorating, so the only thing I can say about these colors is that 'they'd better be a light brown persuasion. This draws the platform. |
GFX.FillRectangle(Brushes.Tan, Platform) GFX.DrawRectangle(Pens.RosyBrown, Platform) 'I'm not into interior decorating, so the only thing I can say about these colors is that 'they'd better be a light brown persuasion. This draws the platform. |
Const SHOULDER As Integer = 17 'Looked in paint to determine the distance the shoulder is from the bottom of the foot. 'This will be used for determining if the player gets on a platform. |
ElseIf PlayerVeloc > 0 AndAlso PlayerLoc.Bottom >= Platform.Top AndAlso PlayerLoc.Bottom - SHOULDER <= Platform.Top Then 'The player has landed on this platform. He needs to be updated. 'See how many advantages there are to using a Rectangle. IsJumping = False 'Hit the ground, we are not jumping. AnimCycler = 0 'Reset cycler. PlayerLoc.Offset(0, Platform.Top - PlayerLoc.Bottom) 'Move the player onto this platform. |
ElseIf PlayerVeloc > 0 AndAlso PlayerLoc.Bottom >= Platform.Top AndAlso PlayerLoc.Bottom - SHOULDER <= Platform.Top Then If PlayerLoc.Left < Platform.Right AndAlso PlayerLoc.Right > Platform.Left Then 'The player has landed on this platform. He needs to be updated. 'See how many advantages there are to using a Rectangle. IsJumping = False 'Hit the ground, we are not jumping. AnimCycler = 0 'Reset cycler. PlayerLoc.Offset(0, Platform.Top - PlayerLoc.Bottom) 'Move the player onto this platform. End If |
Dim LeftEnd As Integer Dim RightEnd As Integer 'This stores the locations of the end of the cliff that the character is currently on. |
LeftEnd = 0 : RightEnd = MAPWIDTH 'Set the current end points. |
If IsJumping Then PlayerLoc.Offset(0, PlayerVeloc + GRAVITY >> 1) 'dD (.OffSet) = Vo (PlayerVeloc) + 0.5 * A (GRAVITY >> 1) PlayerVeloc += GRAVITY 'dV = (PlayerVeloc +=) A (GRAVITY) 'Moves the character according to its velocity, gravity, and location. UpdateArtwork = True 'Need to update everytime during a jump. If PlayerLoc.Bottom >= Me.LANDHEIGHT Then 'Check if we are touching the ground (another RECT advantage). IsJumping = False 'Stop the jump: we have hit the ground. LeftEnd = 0 : RightEnd = MAPWIDTH 'Set the endpoints for the ground. AnimCycler = 0 'Reset the animation cycler: it has been counting during the jump. ElseIf PlayerVeloc > 0 AndAlso PlayerLoc.Bottom >= Platform.Top AndAlso PlayerLoc.Bottom - SHOULDER <= Platform.Top Then If PlayerLoc.Left < Platform.Right AndAlso PlayerLoc.Right > Platform.Left Then 'The player has landed on this platform. He needs to be updated. 'See how many advantages there are to using a Rectangle. IsJumping = False 'Hit the ground, we are not jumping. LeftEnd = Platform.Left : RightEnd = Platform.Right 'Set the endpoints for the platform. AnimCycler = 0 'Reset cycler. PlayerLoc.Offset(0, Platform.Top - PlayerLoc.Bottom) 'Move the player onto this platform. End If End If End If |
If PlayerLoc.Left < 0 Then 'If the player goes off the edge of the form, then we will move him back to the edge. PlayerLoc.Offset(-PlayerLoc.Left, 0) ElseIf PlayerLoc.Right < LeftEnd AndAlso Not IsJumping Then IsJumping = True 'We are not really jumping, but we are falling. PlayerVeloc = 0 'Since we are falling from rest, the initial velocity will be zero. End If |
If PlayerLoc.Right > MAPWIDTH Then 'If player goes off right edge... move him back. PlayerLoc.Offset(MAPWIDTH - PlayerLoc.Right, 0) ElseIf PlayerLoc.Left > RightEnd AndAlso Not IsJumping Then IsJumping = True 'We are falling. PlayerVeloc = 0 'A fall from rest. End If |
PlayerLoc.Offset(0, LANDHEIGHT - PlayerLoc.Bottom) 'Move the player to stand on the land properly. |
Private Sub CharacterMovement() If GoLeft Then If GoFast Then PlayerLoc.Offset(-MOVESPEED - (MOVESPEED >> 1), 0) 'When Shift is pressed the player runs. 'Runs to the left. Else PlayerLoc.Offset(-MOVESPEED, 0) 'Move player left. End If If PlayerLoc.Left < 0 Then 'If the player goes off the edge of the form, then we will move him back to the edge. PlayerLoc.Offset(-PlayerLoc.Left, 0) ElseIf PlayerLoc.Right < LeftEnd AndAlso Not IsJumping Then IsJumping = True 'We are not really jumping, but we are falling. PlayerVeloc = 0 'Since we are falling from rest, the initial velocity will be zero. End If UpdateArtwork = True CharDirec = 0 'Look left. 'Now, update the animation cycler. AnimCycler = (AnimCycler + 1) Mod WALKINGFRAMECOUNT ElseIf GoRight Then If GoFast Then PlayerLoc.Offset(MOVESPEED + (MOVESPEED >> 1), 0) 'Runs to the right. Else PlayerLoc.Offset(MOVESPEED, 0) 'Move player right. End If If PlayerLoc.Right > MAPWIDTH Then 'If player goes off right edge... move him back. PlayerLoc.Offset(MAPWIDTH - PlayerLoc.Right, 0) ElseIf PlayerLoc.Left > RightEnd AndAlso Not IsJumping Then IsJumping = True 'We are falling. PlayerVeloc = 0 'A fall from rest. End If UpdateArtwork = True CharDirec = 1 'Look right. 'Now, update the animation cycler. AnimCycler = (AnimCycler + 1) Mod WALKINGFRAMECOUNT End If If IsJumping Then PlayerLoc.Offset(0, PlayerVeloc + GRAVITY >> 1) 'dD (.OffSet) = Vo (PlayerVeloc) + 0.5 * A (GRAVITY >> 1) PlayerVeloc += GRAVITY 'dV = (PlayerVeloc +=) A (GRAVITY) 'Moves the character according to its velocity, gravity, and location. UpdateArtwork = True 'Need to update everytime during a jump. If PlayerLoc.Bottom >= Me.LANDHEIGHT Then 'Check if we are touching the ground (another RECT advantage). IsJumping = False 'Stop the jump: we have hit the ground. LeftEnd = 0 : RightEnd = MAPWIDTH 'Set the endpoints for the ground. AnimCycler = 0 'Reset the animation cycler: it has been counting during the jump. PlayerLoc.Offset(0, LANDHEIGHT - PlayerLoc.Bottom) 'Move the player to stand on the land properly. ElseIf PlayerVeloc > 0 AndAlso PlayerLoc.Bottom >= Platform.Top AndAlso PlayerLoc.Bottom - SHOULDER <= Platform.Top Then If PlayerLoc.Left < Platform.Right AndAlso PlayerLoc.Right > Platform.Left Then 'The player has landed on this platform. He needs to be updated. 'See how many advantages there are to using a Rectangle. IsJumping = False 'Hit the ground, we are not jumping. LeftEnd = Platform.Left : RightEnd = Platform.Right 'Set the endpoints for the platform. AnimCycler = 0 'Reset cycler. PlayerLoc.Offset(0, Platform.Top - PlayerLoc.Bottom) 'Move the player onto this platform. End If End If End If End Sub |