简体   繁体   中英

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

Is there a way and how can i make the program increment the obstacleSpeed by one every time the score increments by one? (The part with all the if statements). Im thinking it must be some sort of for-loop, but maybe im all wrong. hope someone can help.

I am new at coding so it would be great if you could explain it on like kids level

My Program:

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();

        }


    }
}

it tells me to add more details so the next part is just word spam. Sorry.

Here is a method of implementing this. It uses nothing more than creating a get-set property that references a method that I named OnScoreChanged() .

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

And your OnScoreChanged() method can support the change of speed.

private void OnScoreChanged()
{
    speed++;
}

Note that this event is called whenever you change the value of score . To avoid increasing the speed when the score decreases, you can do something like the following:

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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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