Previously, I talked about adding a platform to the form. Now, we want to add more than one platform. Let's face it: you can only do so much with one platform (circus acts come to mind, but then, you need a flaming hoop). So, we need more platforms. Ideally, we will have an undetermined number of platforms in each level. Each level will be on the same form, and we'll just tweak the variables on it. Since the number of platforms is unknown, we can just store them into an ArrayList, the .NET version of the I-don't-know-how-big-my-storage-is-going-to-be storage. Let's declare our ArrayList.
ArrayList platforms; // Holds the list of all of the current platforms. |
platforms = new ArrayList(100); // Initialize the arraylist to hold up to 100 elements. platforms.Add(Rectangle.FromLTRB(50, 415, 200, thelandheight)); platforms.Add(Rectangle.FromLTRB(100, 465, 225, thelandheight)); platforms.Add(Rectangle.FromLTRB(150, 515, 250, PFMain.thelandheight)); // Instantiate the platforms for the level. |
platform = Rectangle.FromLTRB(150, 515, 250, PFMain.thelandheight); // Instantiate the platform. |
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. |
foreach (Rectangle platform in platforms) { 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. } |
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 { foreach (Rectangle platform in platforms) { 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. } } } } |