コード例 #1
0
 public void UpdateHead(BodyPart newHead)            // add the new head to the parts List
 {
     parts.Add(newHead);
 }
コード例 #2
0
        public void Run()
        {
            while (playing)
            {
                // Get and handle input
                Input _in = IH.Input;
                HandleInput(_in);

                // if we are paused stop here and redo while
                if (paused)
                {
                    Thread.Sleep(1);                     /* Since there are two threads working on the same
                                                         *                                                              bool make sure we are not overwhelming IHThread */
                    continue;
                }

                // Now that we got our input wich at this point should have been handled
                // lets calculate the next frame and print it

                // get the new head
                BodyPart nHead = snake.GetNewHead();

                /*
                 * ### Check for end of game events
                 */

                // Check if new head is outside window
                if (display.IsOutside(nHead))
                {
                    playing = false;
                    break;
                }
                foreach (BodyPart bp in snake.parts)
                {
                    if (nHead == bp)
                    {
                        // Death by accidental self-cannibalism
                        playing = false;
                    }
                }
                // now make sure that the player is not a bot, by checing if the snake is covering the entire window
                if (snake.parts.Count + 1 == display.Width * display.Height)
                {
                    // player is a bot make sure to break here as apple wont find another spot to place itself so we'll get a while (true) scenario
                    playing = false;
                    break;
                }

                /*
                 * ### Now check for if snake has eaten the apple and update accordingly
                 */

                if (nHead == apple)
                {
                    // the snake ate the apple ChangePos on apple and update display
                    apple.ChangePos(display.Width, display.Height, snake.BodyToVector2D());
                    display.PaintApple(apple);
                    display.UpdateSnake(nHead, snake.parts.Last());
                }
                else
                {
                    // snake didn't eat the apple update the snake
                    display.UpdateSnake(nHead, snake.parts.Last(), snake.parts.First());
                    snake.RemoveTail();
                }

                // now finally add the new head to the sake's List of BodyPart(s)
                snake.UpdateHead(nHead);

                Thread.Sleep(1000 / ticksPerSecond);
            }             // END While

            // Do ceanup before exit
            Cleanup();
        }