Now, we're going to add multiple coins and walls to the game, to give it some kind of completeness. First, we'll add support for multiple coins. Of course, we need some kind of list of coins. We can use another arraylist for the coins, and use a For loop or a For Each loop to check all of the coins.
ArrayList coins; // Holds the list of coins for this level. |
coin = new Rectangle(200, 550, thecoinsize, thecoinsize); |
coins = new ArrayList(100); coins.Add(new Rectangle(200, 530, thecoinsize, thecoinsize)); coins.Add(new Rectangle(100, 440, thecoinsize, thecoinsize)); coins.Add(new Rectangle(116, 440, thecoinsize, thecoinsize)); coins.Add(new Rectangle(132, 440, thecoinsize, thecoinsize)); coins.Add(new Rectangle(260, 355, thecoinsize, thecoinsize)); coins.Add(new Rectangle(230, 365, thecoinsize, thecoinsize)); coins.Add(new Rectangle(240, 510, thecoinsize, thecoinsize)); coins.Add(new Rectangle(20, 490, thecoinsize, thecoinsize)); coins.Add(new Rectangle(280, 400, thecoinsize, thecoinsize)); coins.Add(new Rectangle(200, 498, thecoinsize, thecoinsize)); // Ten coins going into the coins arraylist. |
if (coin.IntersectsWith(playerloc)) { coin = Rectangle.Empty; // set this coin to be an empty rectangle } |
for (lv = 0; lv < coins.Count; lv++) { coin = (Rectangle)coins[lv]; if (coin.IntersectsWith(playerloc)) { coin = Rectangle.Empty; // set this coin to be an empty rectangle coins[lv] = coin; } } |
if (!coin.IsEmpty) { gfx.DrawImage(coinbmp, coin.X, coin.Y, new Rectangle((anim % thecoinframecount) * thecoinsize, 0, thecoinsize, thecoinsize), GraphicsUnit.Pixel); // Draw the coin. Since anim increases, the coin will animation because of the mod and multiply for the left argument. }> |
foreach (Rectangle coin in coins) { if (!coin.IsEmpty) { gfx.DrawImage(coinbmp, coin.X, coin.Y, new Rectangle((anim % thecoinframecount) * thecoinsize, 0, thecoinsize, thecoinsize), GraphicsUnit.Pixel); // Draw the coin. Since anim increases, the coin will animation because of the mod and multiply for the left argument. } } // foreach coin in the arraylist. |
if (!isjumping) //Cannot jump if already jumping. { isjumping = true; // The jump is initialized. if (godown) { playerveloc = PFMain.thelowjumpvelocity; // Low jump made when down key is pressed. } else { playerveloc = PFMain.thesbarinitialvelocity; // Use space bar initial velocity. } } |
ArrayList walls; // The walls for this level. |
wall = Rectangle.FromLTRB(225, 415, 275, 465); // Initialize the wall coordinates. |
walls = new ArrayList(100); // The list of walls is set up. walls.Add(Rectangle.FromLTRB(225, 415, 275, 465)); walls.Add(Rectangle.FromLTRB(320, 480, 350, thelandheight)); walls.Add(Rectangle.FromLTRB(350, 520, 380, thelandheight)); // And three walls are added. |
} else if (playerloc.IntersectsWith(wall)) { if (playerveloc > 0 ) { // player is falling and should land on the wall. isjumping = false; leftend = wall.Left; rightend = wall.Right; // Set the end points for the wall. animcycler = 0; playerloc.Offset(0, wall.Top - playerloc.Bottom); } else { // The player is moving upwards and should hit head against wall. playerloc.Offset(0, wall.Bottom - playerloc.Top); // Move the player so that his head touches the top of the wall. playerveloc = 0; } } else { |
} else { foreach (Rectangle wall in walls) { if (playerloc.IntersectsWith(wall)) { if (playerveloc > 0 ) { // player is falling and should land on the wall. isjumping = false; leftend = wall.Left; rightend = wall.Right; // Set the end points for the wall. animcycler = 0; playerloc.Offset(0, wall.Top - playerloc.Bottom); } else { // The player is moving upwards and should hit head against wall. playerloc.Offset(0, wall.Bottom - playerloc.Top); // Move the player so that his head touches the top of the wall. playerveloc = 0; } } } |
else { foreach (Rectangle wall in walls) { if (playerloc.IntersectsWith(wall)) { playerloc.Offset(wall.Left - playerloc.Right, 0); // Move player back to left edge of wall. } } if (playerloc.Left > rightend && !isjumping ) { isjumping = true; playerveloc = 0; // Falling from edge of platform. } } |
} else { foreach (Rectangle wall in walls) { if (playerloc.IntersectsWith(wall)) { playerloc.Offset(wall.Right - playerloc.Left, 0); // Move the player back to the right edge of this wall. } } if (playerloc.Right < leftend && !isjumping ) { isjumping = true; // Falling off the edge of a platform. playerveloc = 0; // Free-fall. } } |
gfx.FillRectangle(Brushes.Peru, wall); gfx.DrawRectangle(Pens.SaddleBrown, wall); // Draws the wall onto the form with peruvian interior and saddlebrown border. |
foreach (Rectangle wall in walls) { gfx.FillRectangle(Brushes.Peru, wall); gfx.DrawRectangle(Pens.SaddleBrown, wall); // Draws the wall onto the form with peruvian interior and saddlebrown border. } |