Exemplo n.º 1
0
        public void SpawnFood()
        {
            int snake_x = Snake.Head.Position_X;
            int snake_y = Snake.Head.Position_Y;

            int bottom_x = 1;
            int top_x    = 1;

            int bottom_y = 1;
            int top_y    = 1;

            if (snake_x > Game_Matrix.GetLength(0) - snake_x)
            {
                top_x = snake_x;
            }
            else
            {
                bottom_x = snake_x;
                top_x    = Game_Matrix.GetLength(0);
            }

            if (snake_y > Game_Matrix.GetLength(1) - snake_y)
            {
                top_y = snake_y;
            }
            else
            {
                bottom_y = snake_y;
                top_y    = Game_Matrix.GetLength(1);
            }

            Random ran = new Random();

            // make sure that it generates on an empty field
            bool generated_truly = false;
            int  food_x          = 0;
            int  food_y          = 0;

            while (!generated_truly)
            {
                food_x = ran.Next(bottom_x, top_x);
                food_y = ran.Next(bottom_y, top_y);

                if (Game_Matrix[food_x, food_y] == 0)
                {
                    generated_truly = true;
                }
            }

            Current_Food = new Food(food_x, food_y);
            Game_Matrix[food_x, food_y] = 2;
        }
Exemplo n.º 2
0
        public void CheckTable(Graphics G)
        {
            int width  = 30;
            int height = 30;

            int rows = Game_Matrix.GetLength(0) - 1;
            int cols = Game_Matrix.GetLength(1) - 1;

            for (int i = 0; i <= rows; i++)
            {
                for (int y = 0; y <= cols; y++)
                {
                    Rectangle check       = new Rectangle(i * width, y * height, width, height);
                    Color     brush_color = Color.White;
                    if ((y % 2 == 0 && i % 2 == 0) || (y % 2 == 1 && i % 2 == 1))
                    {
                        brush_color = Color.LightGray;
                    }
                    SolidBrush brush = new SolidBrush(brush_color);
                    G.FillRectangle(brush, check);
                    brush.Dispose();
                }
            }
        }