Now, we are going to add walls to the game. These are going to be the regular wall that you can't walk into in any kind of way. The player can walk on them, but cannot walk into them, or jump into them. These walls will just be rectangles, so they'll function as wall blocks, so that the player can actually jump on top of them. But, there are a few catches to doing this... . But first, we are going to add collectibles to the project so that the somewhat bizarre discussion of walls can be explained simpler. So, let's get a coin to go onto the form. The coin picture can be found here:. First of all, let's get one coin onto the form. We need to add a Rectangle and a Bitmap declaration for the coin.
Rectangle coin; // The coin location on the form. Bitmap coinbmp; // The picture of the coin. const int thecoinsize = 16; // The width and height of a coin rectangle as it should appear on the form. |
coin = new Rectangle(200, 550, thecoinsize, thecoinsize); coinbmp = new Bitmap(Application.StartupPath + @"\..\..\coin.bmp"); coinbmp.MakeTransparent(Color.Magenta); // Create the coin position on the display and get the coin picture from file. |
int anim; // The animation counter for the coins. |
anim++; // Increment the animation counter. |
updateartwork = false; // set it back to false. |
updateartwork = true; // Need to update everytime during a jump. |
updateartwork = true; |
// Make our drawing when the timer ticks. updateartwork = true; |
const int thecoinframecount = 4; // Number of frames in the coin animation. |
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. |
if (coin.IntersectsWith(playerloc)) { coin = Rectangle.Empty; // set this coin to be an empty rectangle } |
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. } |
Rectangle wall; // The location of the wall. |
wall = Rectangle.FromLTRB(225, 415, 275, 465); // Initialize the wall coordinates. |
gfx.FillRectangle(Brushes.Peru, wall); gfx.DrawRectangle(Pens.SaddleBrown, wall); // Draws the wall onto the form with peruvian interior and saddlebrown border. |
} else if (playerloc.IntersectsWith(wall)) { playerloc.Offset(wall.Right - playerloc.Left, 0); // Move the player back to the right edge of this wall. |
} else if (playerloc.IntersectsWith(wall)) { playerloc.Offset(wall.Left - playerloc.Right, 0); // Move player back to left edge of wall. |
} 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; } |