Welcome back. After a long break, I've decided to continue the .NET Platform Game Tour. Here, I'm going to add some hazards. This is just going to be stuff that kills the main character and that you should avoid. I'm only going to make two types of hazards: a lava trap, which will just be a stationary rectangle of doom, and a floater, which will be a moving object of pain. First, we'll start off with the lava, since it will be easy. To get the most of where I place hazards, here's the map that I have created (don't forget to rename it to level.dat) and it's quite odd-looking. Since the lava will just be a rectangle, we can use the Graphics class Rectangle drawing methods. Since we've done this so many times, let's spice it up a little. Instead of using the regular old brushes provided by the Brushes class (yes, it is a class, despite its behavior), let's make our own brush. It will just be a translucent color of red and it isn't that easy. We'll do the brush alongside the lava rectangle setup which is covered next. The declarations come first.
Dim Lava As Rectangle 'A deadly rectangle of lava that will kill the character. Dim LavaBrsh As Brush 'This will be the brush that draws the lava onto the display. |
Lava = Rectangle.FromLTRB(210, 520, 270, LANDHEIGHT) 'Initialize the lava location. LavaBrsh = New SolidBrush(Color.FromArgb(192, 255, 32, 32)) 'Translucent hot looking red brush color. |
GFX.FillRectangle(LavaBrsh, Lava) GFX.DrawRectangle(Pens.Red, Lava) 'The translucent lava will be drawn with a red box around the border. |
Dim IsGoner As Boolean 'If this variable is true, then the character is dead. |
If Lava.IntersectsWith(PlayerLoc) Then IsGoner = True 'The player steps into lava. Lose a life. PlayerVeloc = SBARINITIALVELOCITY End If |
If Not IsGoner Then 'Do this only if the player has not been killed. |
PlayerVeloc = SBARINITIALVELOCITY |
Static RotateAngle As Single 'Holds the current rotation angle for our character. |
Const SPINOUTSPEED As Single = 50.0F 'How fast the character spins when he has been killed. |
If IsGoner Then GFX.TranslateTransform(Convert.ToSingle(PlayerLoc.X) + (Me.CHARWIDTH >> 1), Convert.ToSingle(PlayerLoc.Y) + (Me.CHARWIDTH >> 1)) 'Move the center point to the center of the character's midpoint. GFX.RotateTransform(RotateAngle) 'Spin the character clockwise. RotateAngle += SPINOUTSPEED If IsJumping Then GFX.DrawImage(PlayerBmp, -Me.CHARWIDTH >> 1, -Me.CHARHEIGHT >> 1, New Rectangle((CHARWIDTH * WALKINGFRAMECOUNT << 1) + CharDirec * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'Draw the character centered on the point 0, 0 Else GFX.DrawImage(PlayerBmp, -Me.CHARWIDTH >> 1, -Me.CHARHEIGHT >> 1, New Rectangle(CHARWIDTH * CharDirec * WALKINGFRAMECOUNT + AnimCycler * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'This draws the character to the display, based on the current character location, frame, and state. End If PlayerLoc.Offset(0, PlayerVeloc + GRAVITY >> 1) 'Gravity-based calculations that we have already done in CharacterMovement. PlayerVeloc += GRAVITY GFX.ResetTransform() Else If IsJumping Then GFX.DrawImage(PlayerBmp, PlayerLoc.X, PlayerLoc.Y, New Rectangle((CHARWIDTH * WALKINGFRAMECOUNT << 1) + CharDirec * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'Jumping draw only. Else GFX.DrawImage(PlayerBmp, PlayerLoc.X, PlayerLoc.Y, New Rectangle(CHARWIDTH * CharDirec * WALKINGFRAMECOUNT + AnimCycler * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'This draws the character to the display, based on the current character location, frame, and state. End If End If |
Static Doompause As Integer 'After this hits a certain value, the character will begin tumbling because he has been killed. |
Const DOOMTIMEOUT As Integer = 15 'Wait this many frames until the character begins tumbling to the bottom of the screen. |
If IsGoner Then If Doompause >= DOOMTIMEOUT Then GFX.TranslateTransform(Convert.ToSingle(PlayerLoc.X) + (Me.CHARWIDTH >> 1), Convert.ToSingle(PlayerLoc.Y) + (Me.CHARWIDTH >> 1)) 'Move the center point to the center of the character's midpoint. GFX.RotateTransform(RotateAngle) 'Spin the character clockwise. RotateAngle += SPINOUTSPEED If IsJumping Then GFX.DrawImage(PlayerBmp, -Me.CHARWIDTH >> 1, -Me.CHARHEIGHT >> 1, New Rectangle((CHARWIDTH * WALKINGFRAMECOUNT << 1) + CharDirec * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'Draw the character centered on the point 0, 0 Else GFX.DrawImage(PlayerBmp, -Me.CHARWIDTH >> 1, -Me.CHARHEIGHT >> 1, New Rectangle(CHARWIDTH * CharDirec * WALKINGFRAMECOUNT + AnimCycler * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'This draws the character to the display, based on the current character location, frame, and state. End If PlayerLoc.Offset(0, PlayerVeloc + GRAVITY >> 1) 'Gravity-based calculations that we have already done in CharacterMovement. PlayerVeloc += GRAVITY GFX.ResetTransform() Else RotateAngle = 0.0F 'Reset the rotateangle for our tumbling-spinning character. Doompause += 1 'Increment the delay counter. If IsJumping Then GFX.DrawImage(PlayerBmp, PlayerLoc.X, PlayerLoc.Y, New Rectangle((CHARWIDTH * WALKINGFRAMECOUNT << 1) + CharDirec * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'Jumping draw only. Else GFX.DrawImage(PlayerBmp, PlayerLoc.X, PlayerLoc.Y, New Rectangle(CHARWIDTH * CharDirec * WALKINGFRAMECOUNT + AnimCycler * CHARWIDTH, 0, CHARWIDTH, CHARHEIGHT), GraphicsUnit.Pixel) 'This draws the character to the display, based on the current character location, frame, and state. End If End If |
Dim FloatBmp As Bitmap 'THe bitmap that will hold the picture of the floater. Dim Floater As Rectangle 'This is the floater's location on the field. Dim FloatTick As Integer 'This is the time variable that indicates what part of the periodic floater function. |
Const FLOATERSIZE As Integer = 16 'The width and height of floater. |
FloatBmp = New Bitmap(Application.StartupPath & "\..\iorb.gif") 'Picture of the floater. Floater = New Rectangle(420, 400, FLOATERSIZE, FLOATERSIZE) 'Location of the floater. The width and height are all determined by the floatersize constant. FloatTick = 0 'Init the float counter. |
GFX.DrawImage(FloatBmp, Floater.X, Floater.Y) 'Draw the floater to the display. There's only one frame, so no selection involved. |
If Floater.IntersectsWith(PlayerLoc) Then IsGoner = True 'The player has touched the floater. Lose a life. PlayerVeloc = SBARINITIALVELOCITY End If |
Floater.Offset(Convert.ToInt32(5 * Math.Cos(Convert.ToDouble(FloatTick) / 8)), Convert.ToInt32(5 * Math.Sin(Convert.ToDouble(FloatTick) / 8))) 'The Floater moves in a circle. 'I am not using constants right now, because I am going to update the lava and floater eventually. FloatTick += 1 |