Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Why does the game run for ever

Hello i am very new to programming and have just finished a few courses to c#. I saw this Code of the snake game and i was just wondering why the game runs endless. So i was thinking that the code is just run from top to bottum. So it should stop running after the Form1 method is completed. Right? So could you please tell me why it is running for ever? Here is the link from where you can acces the Code thank you for your help. https://www.dropbox.com/sh/djuft4103gx6szg/AADKQmKqn7Rgk8wW8ikZoXska?dl=0

1 Answer

Steven Parker
Steven Parker
231,248 Points

As the game starts, it establishes an interval timer that calls "UpdateScreen" at the animation rate. In that function you can find this code:

            //Check for Game Over
            if (Settings.GameOver)
            {
                //Check if Enter is pressed
                if (Input.KeyPressed(Keys.Enter))
                {
                    StartGame();
                }
            }

As you can see, when the game is over, all it does is check to see when a key is pressed and then it starts a new game.

To make the game exit, some code would need to be added to provide a different option.