Example #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);
                }
            }
        }
Example #2
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;
        }