4
\$\begingroup\$

Currently, my game is a basic target shooting game.

  • The targets rise to the top, if the target reaches the top an end screen is shown.
  • If you shoot a target you get more score, and the speed in which the targets move increases.
  • If you shoot the fake target, the end screen is shown

If anyone could help me and clean up my code I'd appreciate it greatly.

namespace ProjectProtoypeUI
{
    public partial class gameForm : Form
    {

        bool gameOver;
        int targetSpeed;
        int score;
        Random randomValue = new Random();
       


        public gameForm()
        {
            InitializeComponent();
            GameRestart();
        }

        private void textScore_Click(object sender, EventArgs e)
        {

        }

        private void MainTimeEvent(object sender, EventArgs e)
        {
            textScore.Text = "Score: " + score;

            if (gameOver == true)
            {
                targetTimer.Stop();
                textScore.Text = "Score :" + score + "Game over, press enter to reset game";
            }

            foreach(Control test in this.Controls)
            {
                if (test is PictureBox)
                {
                    test.Top -= targetSpeed;
                    //test.Left -= targetSpeed;

                    if (test.Top < -100)
                    {
                        test.Top = randomValue.Next(700, 1000);
                        test.Left = randomValue.Next(5, 500);
                    }

                    if ((string)test.Tag == "target")
                    {
                        if (test.Top < -50)
                        {
                            gameOver = true;
                        }
                    }

                    if (gnomeImage.Bounds.IntersectsWith(pictureBox2.Bounds) || gnomeImage.Bounds.IntersectsWith(pictureBox1.Bounds))
                    {
                        test.Top = randomValue.Next(650, 800);
                        test.Left = randomValue.Next(50, 450);
                    }

                    if (pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds))
                    {
                        test.Top = randomValue.Next(700, 1000);
                        test.Left = randomValue.Next(5, 500);
                    }


                } 
            }    
        }

        private void shootTarget(object sender, EventArgs e)
        {
            if (gameOver == false)
            {
                var target = (PictureBox)sender;

                target.Top = randomValue.Next(750, 1000);
                target.Left = randomValue.Next(5, 500);
                
                if (target.Top > 800 && target.Left > 514)
                {
                    target.Top = randomValue.Next(100, 1000);
                    target.Left = randomValue.Next(5, 1000);
                }
                
                score += 1;
                targetSpeed += 1;

            }


        }

        private void gameLost(object sender, EventArgs e)
        {
            if (gameOver == false)
            {
                gameOver = true;
            }    
            
        }

        

        private void GameOver(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && gameOver == true)
            {
                GameRestart();
            }    
        }

        private void GameRestart()
        {
            targetSpeed = 10;
            score = 0;
            gameOver = false;

            

            foreach (Control pictures in this.Controls)
            {
                if (pictures is PictureBox)
                {
                    pictures.Top = randomValue.Next(750, 1000);
                    pictures.Left = randomValue.Next(5, 500);
                }    
            }

            targetTimer.Start();

        }


    }
}

```
\$\endgroup\$
4
  • \$\begingroup\$ Don't forget to add the necessary using directives \$\endgroup\$ – Aryan Parekh Nov 16 '20 at 5:56
  • 2
    \$\begingroup\$ Welcome to code review, please take your time to learn about the scope of this site. We can review your code, but we cannot refactor it (ie. to use more OOP, inheritance,...) or add new features (ie. targets coming in different angles). \$\endgroup\$ – slepic Nov 16 '20 at 6:04
  • \$\begingroup\$ thank you very much, is there anyway I could be pointed in the right direction to learn how to do these things? \$\endgroup\$ – Tytal124 Nov 16 '20 at 12:28
  • \$\begingroup\$ @Tytal124 Not on this site, here we review working code. \$\endgroup\$ – Aryan Parekh Nov 16 '20 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.