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.
bool goleft, goright, gofast; // Indicates if the left or right buttons are pressed. |
if (e.KeyCode == Keys.Left) { goleft = true; } else if (e.KeyCode == Keys.Right) { goright = true; } else if (e.KeyCode == Keys.ShiftKey) { gofast = true; } |
if (e.KeyCode == Keys.Left) { goleft = false; } else if (e.KeyCode == Keys.Right) { goright = false; } else if (e.KeyCode == Keys.ShiftKey) { gofast = false; } |
playerloc.Offset(-themovespeed, 0); // Move player left. |
if (gofast) { playerloc.Offset(-themovespeed << 1, 0); // When shift is pressed, the player runs. } else { playerloc.Offset(-themovespeed, 0); // Move player left. } |
if (gofast) { playerloc.Offset(themovespeed << 1, 0); } else { playerloc.Offset(themovespeed, 0); // Move player right. } |
private void charactermovement() { if (goleft) { if (gofast) { playerloc.Offset(-themovespeed << 1, 0); // When shift is pressed, the player runs. } else { playerloc.Offset(-themovespeed, 0); // Move player left. } if (playerloc.Left < 0) { // If the player goes off the edge of the form, then we will move him back to the edge. playerloc.Offset(-playerloc.Left, 0); } updateartwork = true; chardirec = 0; // Look left. // Now update the animation cycler. animcycler = (animcycler + 1) % thewalkingframecount; } else if (goright) { if (gofast) { playerloc.Offset(themovespeed << 1, 0); } else { playerloc.Offset(themovespeed, 0); // Move player right. } if (playerloc.Right > themapwidth) { // If player goes off right edge... move him back. playerloc.Offset(themapwidth - playerloc.Right, 0); } updateartwork = true; chardirec = 1; // Look right. // Now update the animation cycler. animcycler = (animcycler + 1) % thewalkingframecount; } } |
private void artwork() { if (updateartwork) { /* 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, themapwidth, thelandheight)); // And the remainder of the area is theland, starting from thelandheight. gfx.FillRectangle(Brushes.ForestGreen, Rectangle.FromLTRB(0, thelandheight, themapwidth, themapheight)); // 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(thecharwidth * chardirec * thewalkingframecount + animcycler * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // This draws the character to the display based on the current location. updateartwork = false; // set it back to false. } this.drawonform(); } |
private void clock_Tick(object sender, System.EventArgs e) { this.charactermovement(); this.artwork(); } |
playerloc.Offset(-themovespeed - (themovespeed >> 1), 0); // When shift is pressed, the player runs. playerloc.Offset(themovespeed + (themovespeed >> 1), 0); // When Shift is pressed, the player runs. |
playerloc.Offset(-themovespeed - (themovespeed / 2), 0); // When shift is pressed, the player runs. playerloc.Offset(themovespeed + (themovespeed / 2), 0); // When Shift is pressed, the player runs. |
bool isjumping; // While this is true, the character will be jumping in the air. |
int playerveloc; /* 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 int thesbarinitialvelocity = -16; // The initial velocity for the player's jump using space bar. const int thegravity = 1; // Gravitational acceleration constant... how much the speed changes at each tick. |
if (e.KeyCode == Keys.Left) { goleft = true; } else if (e.KeyCode == Keys.Right) { goright = true; } else if (e.KeyCode == Keys.ShiftKey) { gofast = true; } else if (e.KeyCode == Keys.Space) { isjumping = true; // The jump is initialized. playerveloc = PFMain.thesbarinitialvelocity; // Use space bar initial velocity. } |
if (isjumping) { playerloc.Offset(0, playerveloc + thegravity >> 1); // dD (.Offset) = Vo (playerveloc) + 0.5 * A (thegravity >> 1) playerveloc += thegravity; // dV = (playerveloc +=) A (thegravity) // Moves the character according to its velocity, gravity, and location. updateartwork = true; // Need to update everytime during a jump. } |
if (isjumping) { playerloc.Offset(0, playerveloc + thegravity >> 1); // dD (.Offset) = Vo (playerveloc) + 0.5 * A (thegravity >> 1) playerveloc += thegravity; // dV = (playerveloc +=) A (thegravity) // Moves the character according to its velocity, gravity, and location. updateartwork = true; // Need to update everytime during a jump. if (playerloc.Bottom >= PFMain.thelandheight) { // check if we are touching ground (another rect advantage). isjumping = false; // Stop jumping... we've hit the ground. animcycler = 0; // Reset animation cycler that was counting during the jump. } } |
gfx.DrawImage(playerbmp, playerloc.X, playerloc.Y , new Rectangle(thecharwidth * chardirec * thewalkingframecount + animcycler * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // This draws the character to the display based on the current location. |
if (isjumping) { gfx.DrawImage(playerbmp, playerloc.X, playerloc.Y, new Rectangle((thecharwidth * thewalkingframecount << 1) + chardirec * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // Jumping draw only. } else { gfx.DrawImage(playerbmp, playerloc.X, playerloc.Y , new Rectangle(thecharwidth * chardirec * thewalkingframecount + animcycler * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // This draws the character to the display based on the current location. } |
new Rectangle((thecharwidth * thewalkingframecount << 1) + chardirec * thecharwidth, 0, thecharwidth, thecharheight) |