示例#1
0
        private void InitForm()
        {
            BgArray      = new BaseBlock[iWindowSize, iWindowSize];
            snake        = new SnakeClass(6);
            snake.MaxPos = iWindowSize;
            List <Control> c = new List <Control>();

            for (int i = 0; i < iWindowSize; i++)
            {
                for (int j = 0; j < iWindowSize; j++)
                {
                    BaseBlock b = new BaseBlock();
                    b.Top         = iSize * i + 25;
                    b.Left        = iSize * j;
                    b.X           = j;
                    b.Y           = i;
                    b.Visible     = true;
                    BgArray[j, i] = b;
                    c.Add(b);
                }
            }
            this.Controls.AddRange(c.ToArray());
            RefreshSnake();
            this.Width            = 10 + iSize * iWindowSize;
            this.Height           = 45 + iSize * iWindowSize + iSize;
            t                     = new System.Timers.Timer(3000 / speed);
            t.AutoReset           = true;
            t.Elapsed            += TimerEvent;
            t.SynchronizingObject = this;
            t.Start();
            lbName.Visible  = true;
            lbSpeed.Visible = true;
            lbSpeed.Text    = speed.ToString();
            this.Refresh();
        }
示例#2
0
 private void Clean()
 {
     BgArray = null;
     snake   = null;
     path    = null;
     bLost   = false;
     t       = null;
     iCount  = 0;
 }
示例#3
0
        static void Main()
        {
            Console.CursorVisible = false;
            DrawFrame();
            bool       exit        = false;
            double     refreshRate = 1000 / 5.0;
            DateTime   lastTime    = DateTime.Now;
            Food       food        = new Food();
            SnakeClass snake       = new SnakeClass();

            while (!exit)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo input = Console.ReadKey();
                    switch (input.Key)
                    {
                    case ConsoleKey.Escape:
                        snake.gameOver = true;
                        break;

                    case ConsoleKey.UpArrow:
                        if (snake.Direction != Direction.down)
                        {
                            snake.Direction = Direction.up;
                        }
                        break;

                    case ConsoleKey.DownArrow:
                        if (snake.Direction != Direction.up)
                        {
                            snake.Direction = Direction.down;
                        }
                        break;

                    case ConsoleKey.LeftArrow:
                        if (snake.Direction != Direction.right)
                        {
                            snake.Direction = Direction.left;
                        }
                        break;

                    case ConsoleKey.RightArrow:
                        if (snake.Direction != Direction.left)
                        {
                            snake.Direction = Direction.right;
                        }
                        break;
                    }
                }

                if ((DateTime.Now - lastTime).TotalMilliseconds >= refreshRate)
                {
                    snake.Move();
                    if (snake.HeadPosition.X == food.foodCoordinates.X && snake.HeadPosition.Y == food.foodCoordinates.Y)
                    {
                        snake.Eat();
                        food         = new Food();
                        refreshRate /= 1.1;
                    }

                    if (snake.gameOver)
                    {
                        exit = true;
                        Console.Clear();
                        Console.WriteLine($"Game over! Your score: {snake.Length-1}");
                        Console.ReadLine();
                    }

                    lastTime = DateTime.Now;
                }
            }
        }
示例#4
0
文件: Form1.cs 项目: nightzsze/Small
 private void InitForm()
 {
     BgArray = new BaseBlock[iWindowSize, iWindowSize];
     snake = new SnakeClass(6);
     snake.MaxPos = iWindowSize;
     List<Control> c = new List<Control>();
     for (int i = 0; i < iWindowSize; i++)
     {
         for (int j = 0; j < iWindowSize; j++)
         {
             BaseBlock b = new BaseBlock();
             b.Top = iSize * i + 25;
             b.Left = iSize * j;
             b.X = j;
             b.Y = i;
             b.Visible = true;
             BgArray[j, i] = b;
             c.Add(b);
         }
     }
     this.Controls.AddRange(c.ToArray());
     RefreshSnake();
     this.Width = 10 + iSize * iWindowSize;
     this.Height = 45 + iSize * iWindowSize + iSize;
     t = new System.Timers.Timer(3000 / speed);
     t.AutoReset = true;
     t.Elapsed += TimerEvent;
     t.SynchronizingObject = this;
     t.Start();
     lbName.Visible = true;
     lbSpeed.Visible = true;
     lbSpeed.Text = speed.ToString();
     this.Refresh();
 }
示例#5
0
文件: Form1.cs 项目: nightzsze/Small
 private void Clean()
 {
     BgArray = null;
     snake = null;
     path = null;
     bLost = false;
     t = null;
     iCount = 0;
 }