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.
Rectangle platform; // A platform. |
platform = Rectangle.FromLTRB(150, 515, 250, PFMain.thelandheight); // Instantiate the platform. |
gfx.FillRectangle(Brushes.Maroon, platform); gfx.DrawRectangle(Pens.Chocolate, platform); // This draws the platform with a maroon interior and a chocolate border. // Try the colors out to get a good one. |
gfx.FillRectangle(Brushes.Moccasin , platform); gfx.DrawRectangle(Pens.BurlyWood , platform); // This draws the platform with a maroon interior and a chocolate border. // Try the colors out to get a good one. |
const int theshoulder = 17; // Looked in paint to determine the shoulder distance from the bottom of the file. // If the platform top falls within this range above the player's feet, the player will get on the platform. |
} else if (playerveloc > 0 && playerloc.Bottom >= platform.Top && playerloc.Bottom - theshoulder <= platform.Top) { // The player has landed on the platform. Using a rectangle for the player made this check much easier than without. // We do need to set a few things to let the player know where he is. isjumping = false; // Not jumping animcycler = 0; // Restart the animation cycle to the first frame. playerloc.Offset(0, platform.Top - playerloc.Bottom); // Position the player so that he is standing on the platform. |
} else if (playerveloc > 0 && playerloc.Bottom >= platform.Top && playerloc.Bottom - theshoulder <= platform.Top) { // The player has landed on the platform. Using a rectangle for the player made this check much easier than without. // We do need to set a few things to let the player know where he is. if (playerloc.Right >= platform.Left && platform.Right >= playerloc.Left) { // Player is over the platform. isjumping = false; // Not jumping animcycler = 0; // Restart the animation cycle to the first frame. playerloc.Offset(0, platform.Top - playerloc.Bottom); // Position the player so that he is standing on the platform. } |
int leftend, rightend; // These are the left and right ends of the cliff that the character is currently on. |
leftend = 0; rightend = themapwidth; // Range of the ground set as the end points for the character's current platform. |
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. leftend = 0; rightend = PFMain.themapwidth; // Ground range. animcycler = 0; // Reset animation cycler that was counting during the jump. } else if (playerveloc > 0 && playerloc.Bottom >= platform.Top && playerloc.Bottom - theshoulder <= platform.Top) { // The player has landed on the platform. Using a rectangle for the player made this check much easier than without. // We do need to set a few things to let the player know where he is. if (playerloc.Right >= platform.Left && platform.Right >= playerloc.Left) { // Player is over the platform. isjumping = false; // Not jumping leftend = platform.Left; rightend = platform.Right; // Platform endpoints are now the falling endpoints. animcycler = 0; // Restart the animation cycle to the first frame. playerloc.Offset(0, platform.Top - playerloc.Bottom); // Position the player so that he is standing on the platform. } } } |
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); } else if (playerloc.Right < leftend && !isjumping ) { isjumping = true; // Falling off the edge of a platform. playerveloc = 0; // Free-fall. } |
if (playerloc.Right > themapwidth) { // If player goes off right edge... move him back. playerloc.Offset(themapwidth - playerloc.Right, 0); } else if (playerloc.Left > rightend && !isjumping ) { isjumping = true; playerveloc = 0; // Falling from edge of platform. } |
playerloc.Offset(0, thelandheight - playerloc.Bottom); // Move the player so he stands properly on the bottom. |
private void charactermovement() { if (goleft) { if (gofast) { playerloc.Offset(-themovespeed - (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); } else if (playerloc.Right < leftend && !isjumping ) { isjumping = true; // Falling off the edge of a platform. playerveloc = 0; // Free-fall. } updateartwork = true; chardirec = 0; // Look left. // Now update the animation cycler. animcycler = (animcycler + 1) % thewalkingframecount; } else if (goright) { if (gofast) { playerloc.Offset(themovespeed + (themovespeed >> 1), 0); // When Shift is pressed, the player runs. } 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); } else if (playerloc.Left > rightend && !isjumping ) { isjumping = true; playerveloc = 0; // Falling from edge of platform. } updateartwork = true; chardirec = 1; // Look right. // Now update the animation cycler. animcycler = (animcycler + 1) % thewalkingframecount; } 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. leftend = 0; rightend = PFMain.themapwidth; // Ground range. animcycler = 0; // Reset animation cycler that was counting during the jump. playerloc.Offset(0, thelandheight - playerloc.Bottom); // Move the player so he stands properly on the bottom. } else if (playerveloc > 0 && playerloc.Bottom >= platform.Top && playerloc.Bottom - theshoulder <= platform.Top) { // The player has landed on the platform. Using a rectangle for the player made this check much easier than without. // We do need to set a few things to let the player know where he is. if (playerloc.Right >= platform.Left && platform.Right >= playerloc.Left) { // Player is over the platform. isjumping = false; // Not jumping leftend = platform.Left; rightend = platform.Right; // Platform endpoints are now the falling endpoints. animcycler = 0; // Restart the animation cycle to the first frame. playerloc.Offset(0, platform.Top - playerloc.Bottom); // Position the player so that he is standing on the platform. } } } } |