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 is declared as a rectangle. When it is collected, we will empty the rectangle. When we go to draw our coin, we first check if the coin is visible. So, let's instantiate the coin (in PFMain_Load):
			coin = new Rectangle(200, 550, thecoinsize, thecoinsize);
			coinbmp = new Bitmap(Application.StartupPath + @"\..\..\coin.bmp");
			// Create the coin position on the display and get the coin picture from file.
Don't forget to move the coin bitmap into your solution directory. And let's draw it. To make it animate, we need another variable to be declared for the animation. So, declare in the declarations section:
		int anim;
		// The animation counter for the coins.
anim is an integer used by the artwork sub that is going to count upwards forever. We can extract the data we need from it by just using % or an and operation. Since we are going to do an animation, we can get rid of the UpdateArtwork variable and checks/sets: So, let's add:
			// Increment the animation counter.
at the beginning of the artwork sub. Now, we should remove updateartwork since we want things to be updated all of the time: Remove the if block in the artwork subroutine, but leave the inside code intact, except for
				updateartwork = false;
				// set it back to false.
Note that you don't *have* to remove these, it just makes the code simpler. Remove this from the isjumping block of the CharacterMovement subroutine.
				updateartwork = true;
				// Need to update everytime during a jump.
Also, remove it from the goleft and goright blocks, which are right above the isjumping block.
				updateartwork = true;
Remove it from the KeyUp procedure, remove the entire Paint procedure, and remove...
			// Make our drawing when the timer ticks.
			updateartwork = true;
...from the Load event procedure. We can remove the declaration now. Now that we have done that bit of pruning, we can easily draw our coin: Since anim counts up forever, we can break it into four cycles by using the modulus: anim % 4 which is equal to anim & 3 but once again, let's pretend like we don't know how many frames are in the coin and use a constant instead. We can use another one for the
		const int thecoinframecount = 4;
		// Number of frames in the coin animation.
thecoinframecount indicates how many frames are in the coin, which can be easily changed Now, our drawing looks like this:
			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.
And that should make a coin appear on the form.
Now, to collect this coin, we will perform a collision detection with the player and the coin. This means that if the player and coin are touching, then the coin will be collected and turned into an empty rect. To do the collision detection, .NET has an easy way of detecting if two rectangles touch (intersect), which is in the Rectangle.IntersectsWith() function. So, in our charactermovement, after our character has moved to a new location, we perform the check:
			if (coin.IntersectsWith(playerloc)) 
				coin = Rectangle.Empty;
				// set this coin to be an empty rectangle
And then, in the artwork subroutine:
			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.
And now, the coin will disappear when you touch it.
Now... back to walls. Notice that we did a relatively simple check with the player and the coin. The wall check is much different because we are going to do more than one thing. The Rectangle.IntersectsWith() function can be expressed as: if (A.IntersectsWith(B)) => if (A.Right > B.Left && B.Right > A.Left && A.Bottom > B.Top && B.Bottom > A.Top) But, with the wall, we need to check if the player has hit it from below, from above, from the left, or from the right. So, what do we do? As with all collision detection, we need to perform a check whenever the player moves. Due to the nature of our movement, we can first check if the player has walked into the wall within the goleft or goright blocks and just move the character back to the side of the wall. This will also handle the character's movements while he is jumping. The only condition left is Jumping, and we can check if the player landed on the block or hit it from beneath by checking the player's velocity. A negative velocity means the player is jumping up, which means he has hit the wall from beneath. Likewise, a positive velocity will make the player land on the wall. Now, we just check if the player has touched the wall. First, we need to make the wall rectangle.
		Rectangle wall;
		// The location of the wall.
Then, we initialize the rectangle.
			wall = Rectangle.FromLTRB(225, 415, 275, 465);
			// Initialize the wall coordinates.
Now, to draw the wall (in artwork sub):
			gfx.FillRectangle(Brushes.Peru, wall);
			gfx.DrawRectangle(Pens.SaddleBrown, wall);
			// Draws the wall onto the form with peruvian interior and saddlebrown border.
And finally, to make the character interact with the wall. This will have to be checked in the goleft and goright if blocks in addition to the isjumping block of the charactermovement sub. Now, in the goleft block, we use .IntersectsWith to determine if the player has walked into the block.
				} else if (playerloc.IntersectsWith(wall)) {
					playerloc.Offset(wall.Right - playerloc.Left, 0);
					// Move the player back to the right edge of this wall.
In the GoRight block, we do the same, except the player moves back to the left.
				} else if (playerloc.IntersectsWith(wall)) {
					playerloc.Offset(wall.Left - playerloc.Right, 0);
					// Move player back to left edge of wall.
And finally, in the IsJumping block, we do a check like this.
				} 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);
						// 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;
And that should do it for the wall. Next we'll go ahead and add arrways of coins and walls. At this point, you can download the current state of the project.