Esempio n. 1
0
        void setObstacles()
        {
            obs = new sectList(snakeCoord.count * snakeCoord.count);
            Random rnd = new Random();
            //using a power function to modulate the logarithmic sense of human
            int difficulty = (int)(Math.Pow(1.4, trackBar4.Value) + 5) * trackBar4.Value / (trackBar2.Value + 1);

            //Number of obstacle groups is "difficulty"
            for (int i = 0; i < difficulty; i++)
            {
                int x = -1;
                int y = -1;
                x = rnd.Next(0, snakeCoord.count);
                y = rnd.Next(0, snakeCoord.count);
                obs.append(new Point(x, y), pictureBox1, Color.Black);
                //Each obstacle group has the size of a random number between o and "difficulty * 2"
                //but they may overlap with each other, or may round up an extra space, which allows them to occupy a more variable area
                int size = rnd.Next(0, difficulty * 2);
                for (int j = 0; j < size; j++)
                {
                    int x1, y1;
                    do
                    {
                        x1 = rnd.Next(-1, 2);
                        y1 = rnd.Next(-1, 2);
                    } while (x + x1 < 0 || y + y1 < 0 || x + x1 >= snakeCoord.count || y + y1 >= snakeCoord.count); //Only valid inside the drawing area
                    obs.append(new Point(x + x1, y + y1), pictureBox1, Color.Black);
                }
            }
        }
Esempio n. 2
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();
            }
        }
Esempio n. 3
0
        void initGame()
        {
            gameHandler = true;              //set game on
            label13.Hide();                  //"Game over" will be hidden
            pictureBox1.Show();              //Main drawing area
            int speed = trackBar1.Value + 1; //moving speed of the snake
            //using a power function to modulate the logarithmic sense for speed of human
            int interval = (int)(500 * Math.Pow(0.6, speed));

            timer1.Interval = interval;
            //using a static class to record how many grid lines to show
            snakeCoord.unit  = trackBar2.Value * 4 + 10;
            snakeCoord.count = maxSize / snakeCoord.unit;   //"maxsize" is the size of the drawing area
            Point startPoint = new Point();

            //start at about the centre
            startPoint.X = snakeCoord.count / 2;
            startPoint.Y = snakeCoord.count / 2;
            //Establish an instance of the queue recording the snake itself
            //Construction the list using the max possible length of the snake, which is the square of the grid size
            snake = new sectList(snakeCoord.count * snakeCoord.count);
            //The initial snake consists of 2 squares at nearly the centre of the drawing area
            snake.append(startPoint, pictureBox1, snakeColor);
            incre.x       = 1;
            incre.y       = 0;
            incRec.x      = incre.x;
            incRec.y      = incre.y;
            startPoint.X += incre.x;
            startPoint.Y += incre.y;
            //The second square of the initial snake
            snake.append(startPoint, pictureBox1, snakeColor);
            label7.Hide();  //Hise "Press any key to begin"
            setObstacles();
            setApple();
            this.Refresh();
            timer1.Start();
            //The obstacles the grids are generated in initGame() above and will be fixed throughout the game; therefore their controls must be locked
            trackBar2.Enabled = false;
            trackBar4.Enabled = false;
        }