In this episode, I am going to attempt to make my character move by the arrow keys. Just left and right. First, I need to set up the bitmap that will hold the character
Bitmap playerbmp; // This will hold the main character graphics. // Actually, it is a bunch of frames of the character. |
const int thecharwidth = 16; const int thecharheight = 32; // These define the width of the character in the file. |
playerbmp = new Bitmap(Application.StartupPath + @"\..\..\sumult.bmp"); // Loads the image into this bitmap. // Given this arrangement, the bitmap must be in the same directory as the solution. playerbmp.MakeTransparent(Color.Magenta); // Makes all of the magenta in the bitmap transparent. |
gfx.DrawImage(playerbmp, 100, thelandheight - thecharheight, new Rectangle(0, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // This draws the character to the display. Right now, the 100 is a fixed number, but When the character moves // it'll be changed. |
Rectangle playerloc; // This will hold the current location of the player. X and Y included. // Why didn't I use Point? Because I am probably going to use the height and width for collisions later. bool goleft, goright; // Indicates if the left or right buttons are pressed. const int themovespeed = 5; // How much I move every tick. |
playerloc = new Rectangle(100, thelandheight - thecharheight, thecharwidth, thecharheight); // Instantiate the player location. |
this.KeyDown += new KeyEventHandler(PFMain_KeyDown); this.KeyUp +=new KeyEventHandler(PFMain_KeyUp); private void PFMain_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Left) { goleft = true; } else if (e.KeyCode == Keys.Right) { goright = true; } } |
private void PFMain_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Left) { goleft = false; } else if (e.KeyCode == Keys.Right) { goright = false; } } |
if (goleft) { playerloc.Offset(-themovespeed, 0); // Move player left. updateartwork = true; } else if (goright) { playerloc.Offset(themovespeed, 0); // Move player right. updateartwork = true; } |
gfx.DrawImage(playerbmp, playerloc.X, playerloc.Y , new Rectangle(0, 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // This draws the character to the display based on the current location. |
int chardirec; // Which direction is the character facing? |
if (goleft) { playerloc.Offset(-themovespeed, 0); // Move player left. updateartwork = true; chardirec = 0; // Look left. } else if (goright) { playerloc.Offset(themovespeed, 0); // Move player right. updateartwork = true; chardirec = 1; // Look right. } |
gfx.DrawImage(playerbmp, playerloc.X, playerloc.Y , new Rectangle(thecharwidth * (chardirec << 2), 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // This draws the character to the display based on the current location. |
gfx.DrawImage(playerbmp, playerloc.X, playerloc.Y , new Rectangle(thecharwidth * (chardirec * 4), 0, thecharwidth, thecharheight), GraphicsUnit.Pixel); // This draws the character to the display based on the current location. |