Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I am doing this for a school assignment, but I got stuck at the end of the code. For one, I am unable to get the 'man' animation to be redrawn onto the panel. Though, in the picture box it will animate using the array. Secondly, using the key down, the man will not move at all. It works on my first version with the exact same code copy and pasted. I am unsure if there is another, or better way to do it, or if I somehow messed up. The code for the entire program is first, the second is for the key down, the third is for where I think I need to change the redrawing for it to be animated.

C#
public partial class Form1 : Form
            //Array for animated man
            for (int i = 1; i <= 7; i++)
            {
                images[i] = Image.FromFile(Application.StartupPath + @"\Man" + i.ToString() + ".png");
            }
            PicMan.Image = images[1];

            // Give form focus
            this.Focus();
        
        private void BtnStart_Click(object sender, EventArgs e)
        {
            //Animates Man
            TimeMan.Enabled = !TimeMan.Enabled;

            if (BtnStart.Text == "Start")
            {
                // New Game
                myGraphics.Clear(PnlGameContent.BackColor);
                BtnStart.Text = "Stop";
                BtnExit.Enabled = false;
                TxtScore.Text = "0";
                // set each coin off top of panel and give new speed
                for (int i = 0; i < 5; i++)
                {
                    coinY[i] = -coinSize;
                    coinSpeed[i] = myRandom.Next(4) + 3;
                }
                // Set man near the center
                manX = (int)(PnlGameContent.Width / 2);
                myGraphics.DrawImage(PicMan.Image, manX, PnlGameContent.Height - manSize, manSize, manSize);
                // Give form focus so it can accept KeyDown events
                this.Focus();
    
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // Erase man at old location
            myGraphics.FillRectangle(blankBrush, manX, PnlGameContent.Height - manSize, manSize, manSize);
            // Check for F key (left) and J key (right) and compute the mans position
            if (e.KeyCode == Keys.F)
            {
                manX = manX - 5;
            }
            else if (e.KeyCode == Keys.J)
            {
                manX = manX + 5;
            }
            // Position man
            myGraphics.DrawImage(PicMan.Image, manX, PnlGameContent.Height - manSize, manSize, manSize);
        }

        private void startToolStripMenuItem1_Click(object sender, EventArgs e)
        {
      
        private void TimeMan_Tick(object sender, EventArgs e)
        {
            //Counts through the images and if the last image is selected
            //then image gets reset back to image one
            PicMan.Image = images[count];
            count++;
            if (count > 7)
                count = 0;

        }
        }


C#
private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // Erase man at old location
            myGraphics.FillRectangle(blankBrush, manX, PnlGameContent.Height - manSize, manSize, manSize);
            // Check for F key (left) and J key (right) and compute the mans position
            if (e.KeyCode == Keys.F)
            {
                manX = manX - 5;
            }
            else if (e.KeyCode == Keys.J)
            {
                manX = manX + 5;
            }
            // Position man
            myGraphics.DrawImage(PicMan.Image, manX, PnlGameContent.Height - manSize, manSize, manSize);
        }


C#
// Set man near the center
               manX = (int)(PnlGameContent.Width / 2);
               myGraphics.DrawImage(PicMan.Image, manX, PnlGameContent.Height - manSize, manSize, manSize);


Any help is greatly appreciated, whether is be fixed coding, links, or small suggestions. Thanks in advance.

Sorry for the long code and question, but I am unsure of where I went wrong.
Posted
Updated 11-May-12 19:37pm
v3

1 solution

You should not be doing the drawing inside these methods. You should use them to capture information about how the picture, or any part of it, needs to be redrawn, and save that information. You should then override the System.Windows.Forms.Control.OnPaint()[^] method and do all rendering there, using the previously captured information to decide where to position the various objects.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900