﻿targetElement = pictureBox2;
Control targetElement;
        public int pLeft
        {
            get { return targetElement.Left; }
            set
            {
                if (pLeft <= 0)
                {
                    gright = true;
                    gleft = false;
                    Bounced();
                }
                targetElement.Left = value;
            }
        }
        public int pRight
        {
            get { return targetElement.Right; }
            set
            {
                if (pRight >= Screen.PrimaryScreen.Bounds.Width)
                {
                    gright = false;
                    gleft = true;
                    Bounced();
                }
                targetElement.Left = value - targetElement.Width;
            }
        }
        public int pTop
        {
            get
            {
                return targetElement.Top;
            }
            set
            {
                if (pTop <= 0)
                {
                    gtop = false;
                    gbottom = true;
                    Bounced();
                }
                targetElement.Top = value;
            }
        }
        public int pBottom
        {
            get
            {
                return targetElement.Bottom;
            }
            set
            {
                if (pBottom >= Screen.PrimaryScreen.Bounds.Height)
                {
                    gbottom = false;
                    gtop = true;
                    Bounced();
                }
                targetElement.Top = value - targetElement.Height;
            }
        }
        bool started = false;
        Random rnd = new Random();
        public int speed { get => 10; }
        bool gleft = false, gright = false, gtop = false, gbottom = false;


        private void SecondWindow_VisibleChanged(object sender, EventArgs e)
        {
            if (targetElement.Visible)
            {
                timer1.Start();
            }
            else
            {
                timer1.Stop();
            }
        }
        int p_totalbounces = 0;
        public int TotalBounces
        {
            get => p_totalbounces; set
            {
                p_totalbounces = value;
                label3.Text = "Total bounces :" + p_totalbounces;
                label3.Location = new Point(this.Width / 2 - label3.Width / 2, label3.Height);
            }
        }
        void Bounced()
        {
            int rx = (255 * ((targetElement.Location.X * 100) / this.Width)) / 100;
            targetElement.BackColor = Color.FromArgb(Math.Abs(rnd.Next(0, 255) - rx), Math.Abs(rnd.Next(0, 255) - rx), Math.Abs(rnd.Next(0, 255) - rx));
            TotalBounces += 1;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!started)
            {
                gleft = true;
                gbottom = true;
                started = true;
            }

            if (gright)
            {
                pRight += speed;
            }
            if (gleft)
            {
                pLeft -= speed;
            }
            if (gtop)
            {
                pTop -= speed;
            }
            if (gbottom)
            {
                pBottom += speed;
            }
        }