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.
Dim Coin As Rectangle 'This holds the location of the coin. Dim CoinBmp As Bitmap 'This contains the frames of the coin as it will be drawn onto the map. Const COINSIZE As Integer = 16 'The width and height of the coin. |
Coin = New Rectangle(200, 550, COINSIZE, COINSIZE) CoinBmp = New Bitmap(Application.StartupPath & "\..\coin.bmp") 'Initialize the coin location and picture. CoinBmp.MakeTransparent(Color.Magenta) 'Clears the magenta colors in this bitmap. |
Static Anim As Integer 'Holds a number that coordinates animations. |
Anim += 1 'Increment the 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 COINFRAMECOUNT As Integer = 4 'The number of frames in the coin bitmap. |
GFX.DrawImage(CoinBmp, Coin.X, Coin.Y, New Rectangle((Anim Mod COINFRAMECOUNT) * COINSIZE, 0, COINSIZE, COINSIZE), GraphicsUnit.Pixel) 'Draws our spinning coin onto the form. |
If Coin.IntersectsWith(PlayerLoc) Then Coin = Coin.Empty 'Make the coin rectangle empty to indicate that we do not need to draw it. End If |
If Not Coin.IsEmpty Then GFX.DrawImage(CoinBmp, Coin.X, Coin.Y, New Rectangle((Anim Mod COINFRAMECOUNT) * COINSIZE, 0, COINSIZE, COINSIZE), GraphicsUnit.Pixel) 'Draws our spinning coin onto the form. End If |
Dim Wall As Rectangle 'Holds the location of the wall. |
Wall = Rectangle.FromLTRB(185, 415, 210, 465) 'Wall is initialized. |
GFX.FillRectangle(Brushes.Chocolate, Wall) GFX.DrawRectangle(Pens.BurlyWood, Wall) 'This will draw the wall onto the form. |
ElseIf PlayerLoc.IntersectsWith(Wall) Then PlayerLoc.Offset(Wall.Right - PlayerLoc.Left, 0) 'Move the player to the edge of this wall. |
ElseIf PlayerLoc.IntersectsWith(Wall) Then PlayerLoc.Offset(Wall.Left - PlayerLoc.Right, 0) 'Move the player to the edge of this wall. |
If PlayerLoc.IntersectsWith(Wall) Then If PlayerVeloc > 0 Then 'If player is falling, then the player will land on the wall. IsJumping = False 'Stop the jump. LeftEnd = Wall.Left : RightEnd = Wall.Right 'Set the endpoints. AnimCycler = 0 'Reset the cycler. PlayerLoc.Offset(0, Wall.Top - PlayerLoc.Bottom) 'Position the player to stand on this wall. Else 'The player has jumped up into the wall. PlayerLoc.Offset(0, Wall.Bottom - PlayerLoc.Top) 'Move the player to under the wall. PlayerVeloc = 0 'Make the player start falling down. End If End If |