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

namespace Run
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int num;    //紀錄圖片的索引值
        int a;
        int n = 0;
        int g = 0;
        int score;
        int y = 0;
        EventArgs u;
        EventArgs v;
        Boolean l = true;
        string pathName;
        System.Media.SoundPlayer sp = new System.Media.SoundPlayer(@"music.wav");
        System.Media.SoundPlayer sp1 = new System.Media.SoundPlayer(@"died.wav");
        private void Form1_Load(object sender, EventArgs e)
        {
            
            sp.PlayLooping();


            picshot.Image = new Bitmap("sword.png");
            num = 0;    //預設圖片的索引值為0
            picshot.SizeMode = PictureBoxSizeMode.StretchImage;
            picRun.SizeMode = PictureBoxSizeMode.StretchImage;//圖片調成和控制項同大小
            picX.SizeMode = PictureBoxSizeMode.StretchImage;//圖片調成和控制項同大小
            picY.SizeMode = PictureBoxSizeMode.StretchImage;
            picZ.SizeMode = PictureBoxSizeMode.StretchImage;
            Random rnd = new Random();
            a = rnd.Next(100,250);
            
          
            tmrRun.Interval = 20;  //tmrRun間隔為0.02秒
            tmrRun.Enabled = true;  //啟動tmrRun計時器
            tmrMove.Interval = 80; //tmrMove間隔為0.1秒
            tmrMove.Enabled = true; //啟動tmrMove計時器
            timerscore.Interval = 10; //tmrMove間隔為0.1秒
            timerscore.Enabled = true; //啟動tmrMove計時器
            
           // lblHP.Text = ("100" );
        }
        //在tmrRun_Tick事件中輪流播放四張圖片
       
        private void tmrRun_Tick(object sender, EventArgs e)
        {
            
            picRun.Image = imgRun.Images[num];
            if (num == 3)   //若圖片索引值 = 3
                num = 0;    //設圖片索引值 = 0
            else    //其餘即圖片索引值 < 3
                num++;  //圖片索引值加 1
            
        }


        int speed;
        //在tmrMove_Tick事件中將picRun左移10點
        private void tmrMove_Tick(object sender, EventArgs e)
        {
            speed += 1;

            n += 1;
            lblscore.Text = "score: " + score;
            picX.Image = new Bitmap("2.jpg");
            picX.Left += speed*4/10+20;  //picRun右移10點
            //若picX的Left >= ，就設picRun.Left等於表單工作區的寬度
            if (picX.Left >= 900) picX.Left = 0;
            picY.Image = new Bitmap("2.jpg");
            picY.Left += speed *  5 / 10 ;  //picRun右移10點
            if (picY.Left >= 900) picY.Left = 0;
            picZ.Image = new Bitmap("2.jpg");
            picZ.Left += speed * 3/ 10 + 10;  //picRun右移10點
            picshot.Left -= 10;
            //若picX的Left >= ，就設picRun.Left等於表單工作區的寬度
            if (picZ.Left >= 900) picZ.Left = 0;
            if (n > 10)
            {
                lblR.Text = "full use R";
                n = 11;              
            }
            if (n <= 10)
                lblR.Text = " ";
            if (y == 1)
                g += 1;
            if (g > 3)
            {
                picRun.Top += 500;
                y = 0;
                g = 0;
                l = true;
            }
            if (score > 300)
                lblreflect.Text = "反應指數 L.V 2";
            if (score > 600)
                lblreflect.Text = "反應指數 L.V 3";
            if (score > 900)
                lblreflect.Text = "反應指數 L.V 4";
            if (score > 1200)
                lblreflect.Text = "反應指數 L.V 5";
            if (score > 1800)
                lblreflect.Text = "反應指數 L.V 6";
            if (score > 2500)
                lblreflect.Text = "反應指數 L.V 7";
            if (score > 5000)
                lblreflect.Text = "你已成為傳說!!!";
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (l == true)
            {
                switch (keyData)
                {
                    case Keys.Down:
                        picRun.Top += 80;
                        break;
                    case Keys.Up:
                        picRun.Top -= 80;
                        break;
                    case Keys.R:

                        if (n > 10)
                        {
                            picRun.Top -= 500;
                            n = 0;
                            l = false;
                            y = 1;
                        }
                        break;

                    case Keys.F5:
                        toolStripButton3_Click(0, u);
                        break;
                    case Keys.F1:
                        toolStripButton1_Click(0, u);
                        break;
                    case Keys.Q:
                        picshot.Top = picRun.Top;
                        picshot.Left = picRun.Left - 70;
                        break;
                    
                }
            }
            else
            {
                switch (keyData)
                {
                    case Keys.F5:
                        toolStripButton3_Click(0, v);
                        break;
                    case Keys.F2:
                        toolStripButton2_Click(0, u);
                        break;
                }
            }    
            
                return true;
            }

       
               
        private void timer2_Tick(object sender, EventArgs e)
        {
            score++;
           
            
            double hp = 10000;
            

            while (picX.Bounds.IntersectsWith(picRun.Bounds) && hp != 0)
            {
                hp -= 1;

                
                continue;
            }

            while (picY.Bounds.IntersectsWith(picRun.Bounds) && hp != 0)
            {
                hp -= 1;
                
                continue;
            }

            while (picZ.Bounds.IntersectsWith(picRun.Bounds) && hp != 0)
            {
                hp -= 1;
                
                continue;
            }
            while (picstatus.Bounds.IntersectsWith(picRun.Bounds) && hp != 0)
            {
                hp -= 1;

                //lblHP.Text = ("" + hp.ToString());
                continue;
            }
            while (pictureBox2.Bounds.IntersectsWith(picRun.Bounds) && hp != 0)
            {
                hp -= 1;

                //lblHP.Text = ("" + hp.ToString());
                continue;
            }
            while (picX.Bounds.IntersectsWith(picshot.Bounds) && hp != 0)
            {
                picX.Left = 0;
                picshot.Top -= 500;
                break;
                
            }
            while (picY.Bounds.IntersectsWith(picshot.Bounds) && hp != 0)
            {
                picY.Left = 0;
                picshot.Top -= 500;
                break;

            } while (picZ.Bounds.IntersectsWith(picshot.Bounds) && hp != 0)
            {
                picZ.Left = 0;
                picshot.Top -= 500;
                break;

            }
            if (hp == 0)
            {
                tmrRun.Enabled = false;
                tmrMove.Enabled = false;
                timerscore.Enabled = false;
                l = false;
                sp.Stop();
                sp1.Play();
                score = score - 1;
                MessageBox.Show("U died!"+"  Your Score is: "+score.ToString());
            }
        }

        


        

        private void picZ_Click(object sender, EventArgs e)
        {

        }

        public void toolStripButton1_Click(object sender, EventArgs e)
        {
            sp.Stop();
            tmrRun.Enabled = false;
            tmrMove.Enabled = false;
            timerscore.Enabled = false;
            l = false;
            
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            sp.PlayLooping();
            tmrRun.Enabled = true;
            tmrMove.Enabled = true;
            timerscore.Enabled = true;
            l = true;
        }

        public void toolStripButton3_Click(object sender, EventArgs e)
        {
            sp.PlayLooping();
            score = 0;
            picX.Image = new Bitmap("2.jpg");
            picX.Left = 50;
            picY.Image = new Bitmap("2.jpg");
            picY.Left = 20;
            picZ.Image = new Bitmap("2.jpg");
            picZ.Left = 0;
            picshot.Top = 500;
            tmrRun.Enabled = true;  //啟動tmrRun計時器
            picRun.Top = 130;
            tmrMove.Enabled = true; //啟動tmrMove計時器
            
            timerscore.Enabled = true;
            l = true;
            speed = 0;
            n = 0;
            g = 0;
            y = 0;
            lblreflect.Text = "反應指數 L.V 1";
        }

        private void picRun_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {
          
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void picY_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void aboutbtn_Click(object sender, EventArgs e)
        {
            MessageBox.Show("   組員 :資延B 98156224 李咏翰" + Environment.NewLine + "             資延B 98156249 高傑偉" + Environment.NewLine + Environment.NewLine + "  操作指南: Q發射護盾" + Environment.NewLine + "                    R閃現 " + Environment.NewLine + "                   ↑向上移動 " + Environment.NewLine + "                   ↓向下移動 ");
        }

        

       


       


    }
}
