SteK Creative Commons License 2011.01.03 0 0 729

 int x1, y1, x2, y2, radius = 10, speed_x1 = 1, speed_y1 = 1, speed_x2 = 1, speed_y2 = 1;
        int distance_x, distance_y;
                       
       public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeComponent();
            
            x1 = Width / 2 - radius;
            y1 = Height / 2 - radius;
            x2 = Width / 2 - radius;
            y2 = Height / 2 - radius;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ///--------------------------------------------------------
            
            x1 += speed_x1;
            y1 -= speed_y1;

            x2 -= speed_x2;
            y2 -= speed_y2;

            distance_x = x1 - x2;
            distance_y = y1 - y2;
           
            if (x1 < 0) { speed_x1 *= -1; }
            if (x1 > Width - radius*2) { speed_x1 *= -1; }
            if (y1 < 0) { speed_y1 *= -1; }
            if (y1 > Height - radius*4) { speed_y1 *= -1; }
            
            if (x2 < 0) { speed_x2 *= -1; }
            if (x2 > Width - radius * 2) { speed_x2 *= -1; }
            if (y2 < 0) { speed_y2 *= -1; }
            if (y2 > Height - radius * 4) { speed_y2 *= -1; }

            
                if (distance_x < radius && distance_y < radius)
                {
                    if (speed_x1 < 0)
                    {
                        if (x1 < x2)
                        {

                            speed_x2 *= -1;
                        }
                        
                        if (x1 > x2)
                        {
                            speed_x1 *= -1;
                            
                        }
                    }

                    if (speed_x2 < 0)
                    {
                        if (x1 > x2)
                        {
                            speed_x2 *= -1;
                            
                        }
                        
                        if (x1 < x2)
                        {
                            speed_x1 *= -1;
                        }
                    }

                    if (speed_y1 < 0)
                    {
                        if (y1 < y2)
                        {
                            speed_y2 *= -1;
                        }

                        else
                        {
                            speed_y1 *= -1;
                        }
                    }

                    if (speed_y2 < 0)
                    {
                        if (y1 > y2)
                        {
                            speed_y2 *= -1;

                        }

                        if (y1 < y2)
                        {
                            speed_y1 *= -1;
                        }
                    }
                }
            



            /*
            label1.Text = "Az x: "+x;
            label2.Text = "az x2: "+x2;
            label3.Text = "a távolság dx: " + dx;
            label4.Text = "az y: "+y;
            label5.Text = "az y2: "+y2;
            label6.Text = "a távolság dy" ;
            */

           
            ///--------------------------------------------------------
            
            Invalidate();

            this.Refresh();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.FillEllipse(Brushes.Red, x1, y1, radius, radius);
            e.Graphics.FillEllipse(Brushes.Green, x2, y2, radius, radius);
        }

Előzmény: digicat (728)