1

I'm currently working on a Snake game that can solve itself, but when I activate it, usually after 30~ successful hits, my application crashes with the aforementioned exception either in System.drawing.dll or in System.Windows.Forms.dll.

The problem usually occurs in the command "Application.DoEvents()", but it has occured in several other places too.

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Threading;
using System.IO;

namespace Snake
{
    enum Directions
    {
        Right = 1,
        Left = -1,
        Up = 2,
        Down = -2,
        NoDirection = 0
    }

    public partial class GameForm : Form
    {
        #region Data Members

        SnakeGame gmGame;
        Point pCurrFood;
        Directions dirFirstDirection;
        Directions dirSecondDirection;
        Directions dirHelpDirection;
        Color DEFAULT_COLOUR = Color.White;
        const int SEGMENT_HEIGHT = 10;
        const int SEGMENT_WIDTH = 10;

        #endregion

        #region Ctor

        public GameForm()
        {
            InitializeComponent();
            this.gmGame = new SnakeGame();
            this.dirFirstDirection = Directions.NoDirection;
            this.dirSecondDirection = Directions.NoDirection;
        }

        #endregion

        #region Other Methods

        private void PaintSegment(Graphics gGraphics, Point pnPoint, Color cColour)
        {
            Pen Pen = new Pen(cColour);
            Rectangle Rectangle = new Rectangle(pnPoint.X,
                                                pnPoint.Y,
                                                SEGMENT_HEIGHT,
                                                SEGMENT_WIDTH);
            gGraphics.DrawRectangle(Pen, Rectangle);
            Brush Brush = new SolidBrush(cColour);
            gGraphics.FillRectangle(Brush, Rectangle);
        }

        private void PlaceNewFood()
        {
            Random rRand = new Random();
            int nHeight = rRand.Next(this.panel1.Size.Height);
            int nWidth = rRand.Next(this.panel1.Size.Width);

            while ((nHeight % 10 != 0) || (nWidth % 10 != 0))
            {
                nHeight = rRand.Next(this.panel1.Size.Height - 10);
                nWidth = rRand.Next(this.panel1.Size.Width - 10);

                while (this.gmGame.SnakeQueue.Contains(new Point(nWidth, nHeight)))
                {
                    nHeight = rRand.Next(this.panel1.Size.Height);
                    nWidth = rRand.Next(this.panel1.Size.Width);
                }
            }

            this.pCurrFood = new Point(nWidth, nHeight);
            this.PaintSegment(this.panel1.CreateGraphics(), this.pCurrFood, Color.Red);
        }

        private void SelfSolve()
        {

            this.dirFirstDirection = (Directions)(Math.Sign(this.gmGame.SnakeHead.Y -
                                                            this.pCurrFood.Y) * 2);
            this.dirSecondDirection = (Directions)Math.Sign(this.pCurrFood.X -
                                                            this.gmGame.SnakeHead.X);

            this.ManageSnake(this.dirFirstDirection, this.dirSecondDirection);
        }

        private bool WillCollide(Point pnPointToCheck)
        {
            return ((pnPointToCheck.X > this.panel1.Size.Width) ||
                    (pnPointToCheck.Y > this.panel1.Size.Height) ||
                    (pnPointToCheck.X * pnPointToCheck.Y < 0) ||
                    (this.gmGame.SnakeQueue.Contains(pnPointToCheck)));
        }

        private void ManageSnake(Directions dirFirstSnakeDirection,
                                 Directions dirSecondSnakeDirection)
        {
            Point pnNewHead = this.gmGame.SnakeHead;

            switch (dirFirstSnakeDirection)
            {
                case (Directions.Down):
                {
                    if (this.WillCollide(new Point(pnNewHead.X, pnNewHead.Y + SEGMENT_HEIGHT)))
                    {
                        this.ManageSnake(Directions.NoDirection, dirSecondSnakeDirection);
                    }
                    else
                    {
                        pnNewHead.Y += SEGMENT_HEIGHT;
                        dirHelpDirection = Directions.Down;
                    }

                    break;
                }
                case (Directions.Up):
                {
                    if (this.WillCollide(new Point(pnNewHead.X, pnNewHead.Y - SEGMENT_HEIGHT)))
                    {
                        this.ManageSnake(Directions.NoDirection, dirSecondSnakeDirection);
                    }
                    else
                    {
                        pnNewHead.Y -= SEGMENT_HEIGHT;
                        dirHelpDirection = Directions.Up;
                    }

                    break;
                }
                case (Directions.NoDirection):
                {
                    switch (dirSecondSnakeDirection)
                    {
                        case (Directions.Right):
                        {
                            if (this.WillCollide(new Point(pnNewHead.X + SEGMENT_WIDTH, pnNewHead.Y)))
                            {
                                this.ManageSnake(this.dirHelpDirection, dirSecondSnakeDirection);
                            }
                            else
                            {
                                pnNewHead.X += SEGMENT_WIDTH;
                            }

                            break;
                        }
                        case (Directions.Left):
                        {
                            if (this.WillCollide(new Point(pnNewHead.X - SEGMENT_WIDTH, pnNewHead.Y)))
                            {
                                this.ManageSnake(this.dirHelpDirection, dirSecondSnakeDirection);
                            }
                            else
                            {
                                pnNewHead.X -= SEGMENT_WIDTH;
                            }

                            break;
                        }
                    }

                    break;
                }
            }

            this.gmGame.AddSegment(pnNewHead);

            if (this.gmGame.SnakeHead.Equals(this.pCurrFood))
            {
                this.lblScoreNum.Text = (int.Parse(this.lblScoreNum.Text) + 1).ToString();
                this.PlaceNewFood();
            }
            else
            {
                this.PaintSegment(this.panel1.CreateGraphics(),
                                  (Point)this.gmGame.SnakeQueue.Peek(),
                                  DEFAULT_COLOUR);
                this.gmGame.RemoveSegment();
            }

            this.PaintSegment(this.panel1.CreateGraphics(),
                              this.gmGame.SnakeHead,
                              Color.Green);
            Thread.Sleep(5);
            Application.DoEvents();
            this.SelfSolve();
        }

        #endregion

        #region Events

        private void GameForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.PlaceNewFood();
                this.SelfSolve();
            }
            else if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
            else if (e.KeyCode == Keys.Space)
            {
                MessageBox.Show("Frozen, press OK to continue");
            }
        }

        private void GameForm_ClientSizeChanged(object sender, EventArgs e)
        {
            this.PaintSegment(this.panel1.CreateGraphics(), new Point(210, 250), Color.Green);
            this.PaintSegment(this.panel1.CreateGraphics(), new Point(220, 250), Color.Green);
            this.PaintSegment(this.panel1.CreateGraphics(), new Point(230, 250), Color.Green);
            this.PaintSegment(this.panel1.CreateGraphics(), new Point(240, 250), Color.Green);
            this.PaintSegment(this.panel1.CreateGraphics(), new Point(250, 250), Color.Green);
        }

        #endregion
    }
}

I know it's a lot of code, but since I'm not sure where's the root of the problem, I had to copy all of it.

I'd greatly appreciate it if you could point me in the right direction.

Thanks in advance :)

P.S. I know that the algorithm has flaws, I'll get to that later. Right now my main concern is solving the crash.

2
  • 5
    Simple... don't use DoEvents; use a timer instead, and just run your code on the tick. Commented Oct 20, 2011 at 11:54
  • I'm concerned about calling Application.DoEvents() like that. There's too much code to look at without having a strack trace. The trace tells you the line number. you haven't looked at it? Commented Oct 20, 2011 at 11:55

2 Answers 2

6

This is because you keep on going recursive in your method. e.g.:

      Thread.Sleep(5); 
      Application.DoEvents(); 
      this.SelfSolve(); 

Your method will basically never end.

You should use a timer and within that timer, call SelfSolve() This should solve your problem. When using a timer you dont have to call DoEvents yourself since a winforms timer anyways posts a call to your method as a message and the message loop will handle the invocation and other messages.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Bingo, just spotted the SelfSolve calling into ManageSnake, but you got there before me lol. Either way, there are lots of potential places the code can get stuck.
0

SelfSolve should not call ManageSnake directly, but rather schedule its run for some later moment. Otherwise you get infinite recursion SelfSolve -> ManageSnake -> SelfSolve etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.