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.
Dim Coins As ArrayList 'Holds the list of coins for this level. |
Coin = New Rectangle(200, 550, COINSIZE, COINSIZE) |
Coins = New ArrayList(100) Coins.Add(New Rectangle(200, 530, COINSIZE, COINSIZE)) Coins.Add(New Rectangle(100, 440, COINSIZE, COINSIZE)) Coins.Add(New Rectangle(116, 440, COINSIZE, COINSIZE)) Coins.Add(New Rectangle(132, 440, COINSIZE, COINSIZE)) Coins.Add(New Rectangle(260, 355, COINSIZE, COINSIZE)) Coins.Add(New Rectangle(230, 365, COINSIZE, COINSIZE)) Coins.Add(New Rectangle(240, 510, COINSIZE, COINSIZE)) Coins.Add(New Rectangle(20, 490, COINSIZE, COINSIZE)) Coins.Add(New Rectangle(280, 400, COINSIZE, COINSIZE)) Coins.Add(New Rectangle(200, 498, COINSIZE, COINSIZE)) 'Ten coins going into the coins arraylist. |
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 |
For LV = 0 To Coins.Count - 1 Coin = DirectCast(Coins(LV), Rectangle) If Coin.IntersectsWith(PlayerLoc) Then Coin = Rectangle.Empty 'Set this coin to be an empty rectangle, it is collected. Coins(LV) = Coin End If Next |
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 |
For Each Coin In Coins If Not Coin.IsEmpty Then GFX.DrawImage(CoinBmp, Coin.X, Coin.Y, <font color=#0000FF>New</font> Rectangle((Anim <font color=#0000FF>Mod</font> COINFRAMECOUNT) * COINSIZE, 0, COINSIZE, COINSIZE), GraphicsUnit.Pixel) 'Draw the coin. Since anim increases, the coin will animation because of the mod and multiply for the first left argument. End If Next |
If Not IsJumping Then 'Cannot jump if already jumping. IsJumping = True 'The jump is initialized. If GoDown Then PlayerVeloc = LOWJUMPVELOCITY 'Low jump made when down key is pressed. Else PlayerVeloc = SBARINITIALVELOCITY 'Use space bar initial velocity. End If End If |
Dim Walls As Arraylist 'The walls for this level. |
Wall = Rectangle.FromLTRB(185, 415, 210, 465) 'Wall is initialized. |
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, LANDHEIGHT)) Walls.Add(Rectangle.FromLTRB(350, 520, 380, LANDHEIGHT)) 'And three walls are added. |
If PlayerLoc.IntersectsWith(Wall) Then ElseIf 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 |
Else For Each Wall In Walls ElseIf 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 Next End If |
Else For Each Wall In Walls If Playerloc.IntersectsWith(Wall) Then PlayerLoc.Offset(Wall.Left - PlayerLoc.Right, 0) 'Move player back to left edge of wall. End If Next If PlayerLoc.Left > RightEnd AndAlso Not IsJumping Then IsJumping = True PlayerVeloc = 0 'Falling from the edge of platform. End If |
Else For Each 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. End If Next If PlayerLoc.Right < LeftEnd AndAlso Not IsJumping IsJumping = True 'Falling off the edge of a platform. PlayerVeloc = 0 'Free-fall. End If |
GFX.FillRectangle(Brushes.Chocolate, Wall) GFX.DrawRectangle(Pens.BurlyWood, Wall) 'This will draw the wall onto the form. |
For Each Wall In Walls GFX.FillRectangle(Brushes.Chocolate, Wall) GFX.DrawRectangle(Pens.BurlyWood, Wall) 'This will draw the wall onto the form. Next |