繁体   English   中英

有没有办法,我怎样才能让程序在每次分数增加 1 时将障碍速度增加 1?

[英]Is there a way and how can i make the program increment the obstacleSpeed by one every time the score increments by one?

有没有办法,我怎样才能让程序在每次分数增加 1 时将障碍速度增加 1? (包含所有 if 语句的部分)。 我认为它一定是某种 for 循环,但也许我都错了。 希望有人可以提供帮助。

我是编码新手,所以如果你能像孩子一样解释它会很棒

我的程序:

namespace Arnold
{
    public partial class Form1 : Form
    {
        bool jumping = false;
        int jumpSpeed;
        int force = 15;
        int score = 0;
        int obstacleSpeed = 15;
        Random rand = new Random();
        int posisition;
        bool isGameOver = false;

 public Form1()
        {
            InitializeComponent();
            GameReset();
        }

        private void mainGameTimerEvent(object sender, EventArgs e)
        {
            player.Top += jumpSpeed;

            txtScore.Text = "score: " + score;

            if (jumping == true && force < 0)
            {
                jumping = false;
            }

            if (jumping == true)
            {
                jumpSpeed = -25;
                force -= 1;
            }
            else
            {
                jumpSpeed = 25;
            }

            if (player.Top > 364 && jumping == false)
            {
                force = 15;
                player.Top = 365;
                jumpSpeed = 0;

            }
            foreach (Control x in this.Controls)
            {
                if (x is PictureBox && (string)x.Tag == "Heli")
                {
                    x.Left -= obstacleSpeed;


                    if (x.Left < -100)
                    {
                        x.Left = this.ClientSize.Width + rand.Next(200, 500) + (x.Width * 15);
                    }

                    if (player.Bounds.IntersectsWith(x.Bounds))
                    {
                        gameTimer.Stop();
                        player.Image = Properties.Resources.Explosion_2_0;
                        txtScore.Text += " Press R to restart the game! Or press Q to exit!";
                        isGameOver = true;
                    }
                }

                if (x is PictureBox && (string)x.Tag == "obstacle")
                {
                     x.Left -= obstacleSpeed;
                    
                   
                    if (x.Left < -100)
                    {
                        x.Left = this.ClientSize.Width + rand.Next(200, 500) + (x.Width * 15);
                        score++;
                    }

                    if (player.Bounds.IntersectsWith(x.Bounds))
                    {
                        gameTimer.Stop();
                        player.Image = Properties.Resources.Explosion_2_0;
                        txtScore.Text += " Press R to restart the game! Or press Q to exit!";
                        isGameOver = true;
                    }
                }
            }

            if (score > 5)
            {
                obstacleSpeed = 20;
            }

            if(score > 10)
            {
                obstacleSpeed = 25;
            }

            if (score > 15)
            {
                obstacleSpeed = 30;
            }
            if (score > 20)
            {
                obstacleSpeed = 35;
            }
            if (score > 25)
            {
                obstacleSpeed = 40;
            }
            if (score > 30)
            {
                obstacleSpeed = 45;
            }
            if (score > 35)
            {
                obstacleSpeed = 50;
            }
            if (score > 40)
            {
                obstacleSpeed = 55;
            }
            if (score > 45)
            {
                obstacleSpeed = 60;
            }
            if (score > 50)
            {
                obstacleSpeed = 65;
            }
            if (score > 55)
            {
                obstacleSpeed = 70;
            }
            if (score > 60)
            {
                obstacleSpeed = 75;
            }
            if (score > 65)
            {
                obstacleSpeed = 80;
            }
            if (score > 70)
            {
                obstacleSpeed = 85;
            }
            if (score > 75)
            {
                obstacleSpeed = 90;
            }
        }

        private void keyisdown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space && jumping == false)
            {
                jumping = true;
            }
        }

        private void keyisup(object sender, KeyEventArgs e)
        {
            if (jumping == true)
            {
                jumping = false;    
            }

            if (e.KeyCode == Keys.R && isGameOver == true)
            {
                GameReset();
            }
            if (e.KeyCode == Keys.Q && isGameOver == true)
            {
                txtScore.Text = "Are you sure you want to exit the program? (Y/N)";
            }
            if (e.KeyCode == Keys.Y)
            {
                Environment.Exit(0);
            }
            if (e.KeyCode == Keys.N)
            {
                GameReset();
            }
        }

        private void GameReset()
        {
            force = 15;
            jumpSpeed = 0;
            jumping = false;
            score = 0;
            obstacleSpeed = 15;
            txtScore.Text = "Score" + score;
            player.Image = Properties.Resources.figur_færdig;
            isGameOver = false;
            player.Top = 365;

            foreach (Control x in this.Controls)
            {
                
                if (x is PictureBox && (string)x.Tag == "obstacle")
                {
                    posisition = this.ClientSize.Width + rand.Next(500, 800) + (x.Width * 10);

                    x.Left = posisition;

                }
            }

            gameTimer.Start();

        }


    }
}

它告诉我要添加更多详细信息,因此下一部分只是垃圾邮件。 对不起。

这是实现这一点的方法。 它只使用创建一个引用我命名为OnScoreChanged()的方法的get-set property

int _score;
int speed;
private int score
{
    get
    {
        return _score;
    }
    set
    {
        _score = value;
        OnScoreChanged();
    }
}

并且您的 OnScoreChanged() 方法可以支持速度的变化。

private void OnScoreChanged()
{
    speed++;
}

请注意,只要您更改score的值,就会调用此事件。 为了避免在score下降时提高speed ,您可以执行以下操作:

private int score
{
    get
    {
        return _score;
    }
    set
    {
        if(_score < value)  // When the value of score increases
        {
            OnScoreIncreased();
        }
        else if(_score > value) // When the value of score decreases
        {
            OnScoreDecreased();
        }
        else
        {
            _score = value;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM