Проект 13: Moving Object

 

 

namespace moving_object
{
    public partial class Form1 : Form
    {
        enum Position
        {
            Left, Right, Up,Down
        }

        private int x;
        private int y;
      private  Position objP;

        public Form1()
        {
            InitializeComponent();
            x = 50; y= 50;
            objP=Position.Right;

        }






       
     

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.Blue, x,y,100,100);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if(objP==Position.Right)
            {
            x+=10;
            }
            else if(objP==Position.Left)
            {
            x-=10;
            }
             else if(objP==Position.Up)
            {
            y-=10;
            }
            else if(objP==Position.Down)
            {
            y+=10;
            }

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Left)
            {objP=Position.Left;}
            else if(e.KeyCode==Keys.Right)
            {objP=Position.Right;}
            else if(e.KeyCode==Keys.Up)
            {objP=Position.Up;}
            else if(e.KeyCode==Keys.Down)
            {objP=Position.Down;}
        }
    }
}

Търсене