Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Iam trying to develop at game like bal bat game,i want the picturebox to move to left,iam able to move it top to bottom and also right,i want it to move to left direction, the controls what i used are picturebox as ball,label as bat and 2 timer controls .

code :-
C#
private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            int i = (int)e.KeyCode;
            if (i == 37 && label1.Left > 0)
                label1.Left = label1.Left - 3;
            if (i == 39 && label1.Left < this.Width - label1.Width)
                label1.Left = label1.Left + 3;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Top = pictureBox1.Top - 5;
            if (pictureBox1.Top < 0)
            {
                timer1.Enabled = false;
                timer2.Enabled = true;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            pictureBox1.Top = pictureBox1.Top + 5;
            pictureBox1.Left = pictureBox1.Left + 2;
            if(label1.Top == pictureBox1.Top && pictureBox1.Left > label1.Left && pictureBox1.Left < label1.Right)
            {
                timer1.Enabled = true;
                timer2.Enabled = false;
                
            }
        }
Posted

1 solution

The quick answer is:

C#
pictureBox1.Left -= 2;


The real answer is that this is completely the wrong way to do animation. You want to draw your images using the Graphics class in a Paint event handler or OnPaint override.

Nick
 
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