コード例 #1
0
        public DartsGame()
        {
            this.KeyPreview = true;
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            InitializeComponent();
            AutoScaleMode = AutoScaleMode.None;
            StartPowerMeter();

            //target
            board.Controls.Add(target);
            target.BackColor = Color.Transparent;
            target.BringToFront();

            //first P1
            board.Controls.Add(Player1T1);
            Player1T1.BackColor = Color.Transparent;
            Player1T1.BringToFront();

            //second P1
            board.Controls.Add(Player1T2);
            Player1T2.BackColor = Color.Transparent;
            Player1T2.BringToFront();

            //third P1
            board.Controls.Add(Player1T3);
            Player1T3.BackColor = Color.Transparent;
            Player1T3.BringToFront();

            //first P2
            board.Controls.Add(Player2T1);
            Player2T1.BackColor = Color.Transparent;
            Player2T1.BringToFront();

            //second P2
            board.Controls.Add(Player2T2);
            Player2T2.BackColor = Color.Transparent;
            Player2T2.BringToFront();

            //third P2
            board.Controls.Add(Player2T3);
            Player2T3.BackColor = Color.Transparent;
            Player2T3.BringToFront();

            Score1ShowP1.Text = "301";
            Score1ShowP2.Text = "301";
            Player1T1.Parent  = board;

            if (File.Exists("../../save.xml"))
            {
                XMLPersistence reader = new XMLPersistence();
                var            state  = reader.GetState();

                InitializePlayers(state.player1, state.player2);

                this.Player1T1.Location = state.dart1P1Position;
                this.Player1T2.Location = state.dart2P1Position;
                this.Player1T3.Location = state.dart3P1Position;

                this.Player2T1.Location = state.dart1P2Position;
                this.Player2T2.Location = state.dart2P2Position;
                this.Player2T3.Location = state.dart3P2Position;

                first_score  = state.scoreP1;
                second_score = state.scoreP2;

                Score1ShowP1.Text = first_score.ToString();
                Score1ShowP2.Text = second_score.ToString();

                if (this.Player1T2.Location.X > 1100)
                {
                    counter = 2;
                }
                else if (this.Player1T3.Location.X > 1100)
                {
                    counter = 3;
                }
                else if (this.Player2T1.Location.X > 1100)
                {
                    counter = 4;
                }
                else if (this.Player2T2.Location.X > 1100)
                {
                    counter = 5;
                }
                else if (this.Player2T3.Location.X > 1100)
                {
                    counter = 6;
                }
            }
        }
コード例 #2
0
        //Constant numbers are the images offset
        //11 and 10 are offsets for dart Image
        // 24 and 22 are offsets for target image
        private void Power_Click(object sender, EventArgs e)
        {
            int section = 0;

            int power = 0;

            power = this.EndPowerMeter() - 50;

            if (CheckForPlayers())
            {
                if (counter == 1)
                {
                    if (namePlayer1 == "Human")
                    {
                        this.Player1T1.Location = new Point(this.target.Location.X + 11, this.target.Location.Y - power - 10);
                        section = this.points.CalculateThrow(this.target.Location.X + 24, this.target.Location.Y - power + 22);
                    }
                    else
                    {
                        section = GetSection(this.Player1T1, namePlayer1, first_score);
                    }
                }
                if (counter == 2)
                {
                    if (namePlayer1 == "Human")
                    {
                        this.Player1T2.Location = new Point(this.target.Location.X + 11, this.target.Location.Y - power - 10);
                        section = this.points.CalculateThrow(this.target.Location.X + 24, this.target.Location.Y - power + 22);
                    }
                    else
                    {
                        section = GetSection(this.Player1T2, namePlayer1, first_score);
                    }
                }
                if (counter == 3)
                {
                    if (namePlayer1 == "Human")
                    {
                        this.Player1T3.Location = new Point(this.target.Location.X + 11, this.target.Location.Y - power - 10);
                        section = this.points.CalculateThrow(this.target.Location.X + 24, this.target.Location.Y - power + 22);
                    }
                    else
                    {
                        section = GetSection(this.Player1T3, namePlayer1, first_score);
                    }
                }
                if (counter == 4)
                {
                    if (namePlayer2 == "Human")
                    {
                        this.Player2T1.Location = new Point(this.target.Location.X + 11, this.target.Location.Y - power - 10);
                        section = this.points.CalculateThrow(this.target.Location.X + 24, this.target.Location.Y - power + 22);
                    }
                    else
                    {
                        section = GetSection(this.Player2T1, namePlayer2, second_score);
                    }
                }
                if (counter == 5)
                {
                    if (namePlayer2 == "Human")
                    {
                        this.Player2T2.Location = new Point(this.target.Location.X + 11, this.target.Location.Y - power - 10);
                        section = this.points.CalculateThrow(this.target.Location.X + 24, this.target.Location.Y - power + 22);
                    }
                    else
                    {
                        section = GetSection(this.Player2T2, namePlayer2, second_score);
                    }
                }
                if (counter == 6)
                {
                    if (namePlayer2 == "Human")
                    {
                        this.Player2T3.Location = new Point(this.target.Location.X + 11, this.target.Location.Y - power - 10);
                        section = this.points.CalculateThrow(this.target.Location.X + 24, this.target.Location.Y - power + 22);
                    }
                    else
                    {
                        section = GetSection(this.Player2T3, namePlayer2, second_score);
                    }
                }

                if (0 < counter && counter < 4)
                {
                    first_score = first_score - section;
                    if (first_score == 0)
                    {
                        Score1ShowP1.Text = first_score.ToString();
                        MessageBox.Show("First player WON!");
                        ClearTheBoard();
                        ResetScore();
                        counter = 0;
                    }
                    else if (first_score < 0)
                    {
                        MessageBox.Show("Try again!");
                        counter           = 3;
                        first_score       = first_score + section;
                        Score1ShowP1.Text = first_score.ToString();
                        StartPowerMeter();
                        ClearTheBoard();
                    }
                    else
                    {
                        Score1ShowP1.Text = first_score.ToString();
                        StartPowerMeter();
                    }
                }
                else
                {
                    second_score = second_score - section;
                    if (second_score == 0)
                    {
                        Score1ShowP2.Text = second_score.ToString();
                        MessageBox.Show("Second player WON!");
                        ClearTheBoard();
                        ResetScore();
                        counter = 0;
                    }
                    else if (second_score < 0)
                    {
                        MessageBox.Show("Try again!");
                        counter           = 0;
                        second_score      = second_score + section;
                        Score1ShowP2.Text = second_score.ToString();
                        StartPowerMeter();
                        ClearTheBoard();
                    }
                    else
                    {
                        Score1ShowP2.Text = second_score.ToString();
                        StartPowerMeter();
                    }
                }

                XMLPersistence savefile = new XMLPersistence();
                savefile.SaveData(this.Player1T1.Location, Player1T2.Location, this.Player1T3.Location,
                                  this.Player2T1.Location, this.Player2T2.Location, this.Player2T3.Location,
                                  first_score, second_score, namePlayer1, namePlayer2);

                counter++;
                if (counter > 6)
                {
                    MessageBox.Show("Next turn!");
                    counter = 1;
                    ClearTheBoard();
                }
            }
        }