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 data.lvl) 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 orange and it isn't that easy. We'll do the brush alongside the lava rectangle setup which is covered next. The declarations come first.
Rectangle lava; // This is the box representing where the lava is. Brush lavabrsh; // The brush that we'll use to draw lava to the display. |
lava = Rectangle.FromLTRB(380, 520, 420, thelandheight); // Positions the lava on the display at these coordinates. lavabrsh = new SolidBrush(Color.FromArgb(180, 255, 155, 40)); // Make the lavabrush a hot and translucent color of orange. |
gfx.FillRectangle(lavabrsh, lava); gfx.DrawRectangle(Pens.DarkOrange, lava); // Draw the lava to the form. |
bool isgoner; // If the player is dead, we set this to true. |
if (playerloc.IntersectsWith(lava)) { // Touched the lava, now you must die. isgoner = true; playerveloc = PFMain.thesbarinitialvelocity; // This is for a falling animation when the player is beaten. } |
if (!isgoner) { |
playerveloc = PFMain.thesbarinitialvelocity; |
float rotateangle; // This is the angle that the player spins at when he is falling off the bottom of the form. |
const float thespinoutspeed = 50.0F; // Amount the player spins every frame during spinning animation. |
if (isgoner) { gfx.TranslateTransform(playerloc.X + (thecharwidth >> 1), playerloc.Y + (thecharheight >> 1)); // Move the zero point to the center of the character. gfx.RotateTransform(rotateangle); rotateangle += thespinoutspeed; // Apply the rotation. if (isjumping) { gfx.DrawImage(playerbmp, -thecharwidth >> 1, -thecharheight >> 1, new Rectangle((thecharwidth * thewalkingframecount << 1) + chardirec * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // Jumping draw only. } else { gfx.DrawImage(playerbmp, -thecharwidth >> 1, -thecharheight >> 1, new Rectangle(thecharwidth * chardirec * thewalkingframecount + animcycler * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // This draws the character to the display based on the current location. } playerloc.Offset(0, playerveloc + thegravity >> 1); playerveloc += thegravity; // Similar to the gravity calculations we made in charactermovement jumping routine. gfx.ResetTransform(); } else { if (isjumping) { gfx.DrawImage(playerbmp, playerloc.X, playerloc.Y, new Rectangle((thecharwidth * thewalkingframecount << 1) + chardirec * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // Jumping draw only. } else { gfx.DrawImage(playerbmp, playerloc.X, playerloc.Y , new Rectangle(thecharwidth * chardirec * thewalkingframecount + animcycler * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // This draws the character to the display based on the current location. } } |
int doompause; // This is for a short delay that happens when the player runs into something deadly. |
const int thedoomtimeout = 15; // Number of frames to wait before spinning our player off the form. |
if (isgoner) { if (doompause >= thedoomtimeout) { gfx.TranslateTransform(playerloc.X + (thecharwidth >> 1), playerloc.Y + (thecharheight >> 1)); // Move the zero point to the center of the character. gfx.RotateTransform(rotateangle); rotateangle += thespinoutspeed; // Apply the rotation. if (isjumping) { gfx.DrawImage(playerbmp, -thecharwidth >> 1, -thecharheight >> 1, new Rectangle((thecharwidth * thewalkingframecount << 1) + chardirec * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // Jumping draw only. } else { gfx.DrawImage(playerbmp, -thecharwidth >> 1, -thecharheight >> 1, new Rectangle(thecharwidth * chardirec * thewalkingframecount + animcycler * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // This draws the character to the display based on the current location. } playerloc.Offset(0, playerveloc + thegravity >> 1); playerveloc += thegravity; // Similar to the gravity calculations we made in charactermovement jumping routine. gfx.ResetTransform(); } else { rotateangle = 0F; //Reset the tumbling player. doompause += 1; // Doompause increments until it reaches thedoomtimeout. if (isjumping) { gfx.DrawImage(playerbmp, playerloc.X, playerloc.Y, new Rectangle((thecharwidth * thewalkingframecount << 1) + chardirec * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // Jumping draw only. } else { gfx.DrawImage(playerbmp, playerloc.X, playerloc.Y , new Rectangle(thecharwidth * chardirec * thewalkingframecount + animcycler * thecharwidth, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // This draws the character to the display based on the current location. } } } |
Rectangle floater; // This holds the location of the floater. Bitmap floatbmp; // The floater's picture. int floattick; // A number that determines what part of the circle the floater is at. |
const int thefloatersize = 16; // Width and height of the floater. |
floatbmp = new Bitmap(Application.StartupPath + @"\..\..\iorb.gif"); // Transparency already included in the GIF. floater = new Rectangle(480, 280, thefloatersize, thefloatersize); // Set up floater location. floattick = 0; // Initialize counter. |
gfx.DrawImage(floatbmp, floater); // Draw the floater. |
if (playerloc.IntersectsWith(floater)) { // Touched the floater. isgoner = true; playerveloc = PFMain.thesbarinitialvelocity; } |
floater.Offset(Convert.ToInt32(5 * Math.Cos(Convert.ToDouble(floattick) / 8)), Convert.ToInt32(5 * Math.Sin(Convert.ToDouble(floattick) / 8))); // Above, dr = [ 5 * Cos(T / 8), 5 * Sin(T / 8) ], which is a circle. floattick += 1; |