示例#1
0
        public void timer1_Tick(object sender, EventArgs e)
        {
            //Memorizing the last increment, to avoid the situation when pressing opposite arrow keys at the same time kills the snake instantly
            if (incre.x == incRec.x && incre.y == -(incRec.y) && incre.y != 0)
            {
                incre.y = incRec.y;
            }
            else if (incre.x == -(incRec.x) && incre.y == incRec.y && incre.x != 0)
            {
                incre.x = incRec.x;
            }
            incRec.x = incre.x;
            incRec.y = incre.y;
            Point addPoint = new Point();

            addPoint.X = snake.getEnd().point.X + incre.x;
            addPoint.Y = snake.getEnd().point.Y + incre.y;
            if (checkBox1.Checked)
            {
                //if crossing the boundries is allowed, when the snake reaches the boundries, set it to appear from the other side
                turnAround(ref addPoint);
            }
            if (addPoint.X > snakeCoord.count - 1 || addPoint.Y > snakeCoord.count - 1 || addPoint.X < 0 || addPoint.Y < 0)
            {
                //if the snake moves out of the boundries
                gameOver();
                return;
            }
            if (snake.overlaps(addPoint) || obs.overlaps(addPoint))
            {
                //if the snake hits any obstacles
                gameOver();
                return;
            }
            if (sectList.collision(apple.point, addPoint))
            {
                //if the snake swallows an apple, its length will increase by 1, and we need to set a new apple
                snake.append(addPoint, pictureBox1, snakeColor);
                setApple();
            }
            else
            {
                //normal movement of the snake, by adding a new square to the head, as well as removing one from the tail
                snake.append(addPoint, pictureBox1, snakeColor);
                snake.remove_front();
            }
        }