using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Car_Racing_Game
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool serit_ileri = false;
        bool RcarLeft = true;
        int road = 0;
        Random r = new Random();

        randomCar_c[] rndCar = new randomCar_c[2];

        class randomCar_c
        {
            public bool FakehaveCar = false;
            public PictureBox fakeCar;
            public bool vakit = false;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            road += 1;
            this.Text = "Car Racing Game " + road.ToString() + " m";
            if (serit_ileri) // ileri
            {
                for (int i = 0; i < 6; i++)
                {
                    this.Controls.Find("lblS" + i.ToString(), true)[0].Top -= 20;
                }               
                serit_ileri = false;
            }
            else // geri
            {
                for (int i = 0; i < 6; i++)
                {
                    this.Controls.Find("lblS" + i.ToString(), true)[0].Top += 20;
                }
                serit_ileri = true;              
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                if (!RcarLeft)
                {
                    pbRCar.Location = new Point(37, 346);
                    RcarLeft = true;
                }
            }
            else if (e.KeyCode == Keys.Right)
            {
                if (RcarLeft)
                {
                    pbRCar.Location = new Point(172, 346);
                    RcarLeft = false;
                }
            }
        }

        private void tmrRndCar_Tick(object sender, EventArgs e)
        {            
            for (int i = 0; i < rndCar.Length; i++)
            {
                if (!rndCar[i].FakehaveCar && rndCar[i].vakit)
                {
                    rndCar[i].fakeCar = new PictureBox();
                    randomCar(rndCar[i].fakeCar);
                    rndCar[i].fakeCar.Size = new Size(65, 106);
                    rndCar[i].fakeCar.Top = -rndCar[i].fakeCar.Height;

                    int sol_Yerles = r.Next(0, 2);
                    if (sol_Yerles == 0)
                    {
                        rndCar[i].fakeCar.Left = 37;
                    }
                    else
                    {
                        rndCar[i].fakeCar.Left = 172;
                    }

                    this.Controls.Add(rndCar[i].fakeCar);
                    rndCar[i].FakehaveCar = true;
                }
                else
                {
                    if (rndCar[i].vakit)
                    {
                        rndCar[i].fakeCar.Top += 20;                 
                        if (rndCar[i].fakeCar.Top >= 154)
                        {
                            for (int j = 0; j < rndCar.Length; j++)
                            {
                                if (!rndCar[j].vakit)
                                {
                                    rndCar[j].vakit = true;
                                    break;
                                }
                            }                               
                        }                        
                        if (rndCar[i].fakeCar.Top >= this.Height - 20)
                        {
                            rndCar[i].fakeCar.Dispose();
                            rndCar[i].FakehaveCar = false;
                            rndCar[i].vakit = false;
                        }
                    }                    
                }
                if (rndCar[i].vakit)
                {
                    float mutlakX = Math.Abs((pbRCar.Left + (pbRCar.Width / 2)) - (rndCar[i].fakeCar.Left + (rndCar[i].fakeCar.Width / 2)));
                    float mutlakY = Math.Abs((pbRCar.Top + (pbRCar.Height / 2)) - (rndCar[i].fakeCar.Top + (rndCar[i].fakeCar.Height / 2)));
                    float farkGenislik = (pbRCar.Width / 2) + (rndCar[i].fakeCar.Width / 2);
                    float farkYukselik = (pbRCar.Height / 2) + (rndCar[i].fakeCar.Height / 2);

                    if ((farkGenislik > mutlakX) && (farkYukselik > mutlakY))
                    {
                        tmrRndCar.Enabled = false;
                        tmrSerit.Enabled = false;                        
                        DialogResult sonuc = MessageBox.Show("Kaza yaptınız. Oyunu kaybettiniz. Yeniden oynamak ister misiniz ?","Dikkat",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                        if (sonuc == DialogResult.Yes)
                        {
                            pbRCar.Location =  new Point(37, 346);
                            for (int j = 0; j < rndCar.Length ; j++)
                            {
                                rndCar[j].fakeCar.Dispose();
                                rndCar[j].FakehaveCar = false;
                                rndCar[j].vakit = false;
                            }
                            road = 0;
                            rndCar[0].vakit = true;
                            tmrRndCar.Enabled = true;
                            tmrSerit.Enabled = true;
                        }
                        else
                        {
                            this.Close();
                        }
                        break;
                    } 
                }                
            } 
        }

        void randomCar(PictureBox pb)
        {
            int rnd = r.Next(0, 3);
            switch (rnd)
            {
                case 0:
                    pb.Image = Properties.Resources.car2;
                    break;
                case 1:
                    pb.Image = Properties.Resources.car3;
                    break;
                case 2:
                    pb.Image = Properties.Resources.car4;
                    break;
            }

            pb.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < rndCar.Length; i++)
            {
                rndCar[i] = new randomCar_c();                              
            }
            rndCar[0].vakit = true;
        }
    }
}
