Thank you for tuning in again. I am currently thinking of how to get the character to run. Obviously, we are going to need another key that will be held down for running. How about the Shift key? Now, for the shift key, we need to edit the KeyDown and KeyUp subroutines to support the Shift key. But, before we do that, we need to make a variable that indicates if Shift is down.
Dim GoLeft, GoRight, GoFast As Boolean 'Indicates which buttons are pressed. |
If e.KeyCode = Keys.Left Then GoLeft = True ElseIf e.KeyCode = Keys.Right Then GoRight = True ElseIf e.KeyCode = Keys.ShiftKey Then GoFast = True End If |
If e.KeyCode = Keys.Left Then GoLeft = False ElseIf e.KeyCode = Keys.Right Then GoRight = False ElseIf e.KeyCode = Keys.ShiftKey Then GoFast = False End If |
PlayerLoc.Offset(-MOVESPEED, 0) 'Move player left. |
If GoFast Then PlayerLoc.Offset(-MOVESPEED << 1, 0) 'When Shift is pressed the player runs. Else PlayerLoc.Offset(-MOVESPEED, 0) 'Move player left. End If |
If GoFast Then PlayerLoc.Offset(MOVESPEED << 1, 0) 'Runs to the right. Else PlayerLoc.Offset(MOVESPEED, 0) 'Move player right. End If |
Private Sub CharacterMovement() If GoLeft Then If GoFast Then PlayerLoc.Offset(-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) 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 << 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) End If UpdateArtwork = True CharDirec = 1 'Look right. 'Now, update the animation cycler. AnimCycler = (AnimCycler + 1) Mod WALKINGFRAMECOUNT End If End Sub |
Private Sub Artwork() If UpdateArtwork Then 'I am going to draw a lightblue rectangle for the sky. 'It will go down until it reaches the land, so therefore the bottom of the rectangle has to be 'the LANDHEIGHT. GFX.FillRectangle(Brushes.LightSkyBlue, Rectangle.FromLTRB(0, 0, MAPWIDTH, LANDHEIGHT)) 'And the remainder of the area is LAND, starting from LANDHEIGHT. GFX.FillRectangle(Brushes.ForestGreen, Rectangle.FromLTRB(0, LANDHEIGHT, MAPWIDTH, MAPHEIGHT)) 'I use LTRB instead of New so that I don't have to calculate the height. 'These two will make the 'background' for the form. GFX.DrawImage(PlayerBmp, PlayerLoc.X, PlayerLoc.Y, New Rectangle(CHARWIDTH * CharDirec * WALKINGFRAMECOUNT + AnimCycler * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'This draws the character to the display, based on the current character location. UpdateArtwork = False 'Set it back to false. End If Me.DrawOnForm() 'Me.Invalidate(New Rectangle(0, 0, 1, 1)) End Sub |
Private Sub Clock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clock.Tick Me.CharacterMovement() 'Handles the character movement. Me.Artwork() 'Draws the information to the backbuffer, and then, to the form. End Sub |
PlayerLoc.Offset(-MOVESPEED - (MOVESPEED >> 1), 0) 'When Shift is pressed the player runs. 'Runs to the left. PlayerLoc.Offset(MOVESPEED + (MOVESPEED >> 1), 0) 'Runs to the right. |
PlayerLoc.Offset(-MOVESPEED - MOVESPEED \ 2, 0) 'When Shift is pressed the player runs. 'Runs to the left. PlayerLoc.Offset(MOVESPEED + MOVESPEED \ 2, 0) 'Runs to the right. |
Dim IsJumping As Boolean 'While this is true, the character will be jumping. |
Dim PlayerVeloc As Integer 'This is the velocity of the player in the Y direction. 'Positive values point down. 'To jump up, PlayerVeloc must be initialized to a negative value. Const SBARINITIALVELOCITY As Integer = -16 'The initial velocity for the player's jump using space bar. Const GRAVITY As Integer = 1 'Gravitational acceleration constant... how much the speed changes at each tick. |
If e.KeyCode = Keys.Left Then GoLeft = True ElseIf e.KeyCode = Keys.Right Then GoRight = True ElseIf e.KeyCode = Keys.ShiftKey Then GoFast = True ElseIf e.KeyCode = Keys.Space Then IsJumping = True 'The jump is initialized. PlayerVeloc = Me.SBARINITIALVELOCITY 'Use space bar initial velocity. 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. 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. AnimCycler = 0 'Reset the animation cycler: it has been counting during the jump. End If End If |
GFX.DrawImage(PlayerBmp, PlayerLoc.X, PlayerLoc.Y, New Rectangle(CHARWIDTH * CharDirec * WALKINGFRAMECOUNT + AnimCycler * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'This draws the character to the display, based on the current character location, frame, and state. |
If IsJumping Then GFX.DrawImage(PlayerBmp, PlayerLoc.X, PlayerLoc.Y, New Rectangle((CHARWIDTH * WALKINGFRAMECOUNT << 1) + CharDirec * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'Jumping draw only. Else GFX.DrawImage(PlayerBmp, PlayerLoc.X, PlayerLoc.Y, New Rectangle(CHARWIDTH * CharDirec * WALKINGFRAMECOUNT + AnimCycler * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'This draws the character to the display, based on the current character location, frame, and state. End If |
New Rectangle((CHARWIDTH * WALKINGFRAMECOUNT << 1) + CharDirec * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT) |