コード例 #1
0
        void Process(ConsoleKeyInfo cki)
        {
            if (cki.Modifiers == ConsoleModifiers.Control)
            {
                switch (cki.Key)
                {
                case ConsoleKey.P:
                    if (cki.Modifiers != ConsoleModifiers.Control)
                    {
                        break;
                    }
                    Console.BackgroundColor = ConsoleColor.DarkMagenta;
                    Console.SetCursorPosition(33, 27);
                    Console.Write("Game paused!");
                    Console.ReadKey();
                    Console.SetCursorPosition(33, 27);
                    Console.BackgroundColor = ConsoleColor.DarkBlue;
                    Console.Write("              ");
                    break;

                case ConsoleKey.S:
                    esc = true;
                    break;

                default:
                    break;
                }
            }
            else
            {
                snake.ChangeDirection(cki);
            }
        }
コード例 #2
0
        public void ReadUserInput()
        {
            switch (lastKey)
            {
            case ConsoleKey.UpArrow:
                snake.ChangeDirection(0, -1);
                SpFactor = Speed / 2;
                break;

            case ConsoleKey.DownArrow:
                snake.ChangeDirection(0, 1);
                SpFactor = Speed / 2;
                break;

            case ConsoleKey.LeftArrow:
                snake.ChangeDirection(-1, 0);
                SpFactor = 0;
                break;

            case ConsoleKey.RightArrow:
                snake.ChangeDirection(1, 0);
                SpFactor = 0;
                break;

            case ConsoleKey.Q:
                ResetConsole();
                Environment.Exit(0);
                break;
            }
        }
コード例 #3
0
ファイル: Game.cs プロジェクト: alexandrovva/Snake
        public void Start()
        {
            ConsoleKeyInfo keyInfo = Console.ReadKey();
            Thread         thread  = new Thread(SnakeMove);

            thread.Start();

            while (isAlive && keyInfo.Key != ConsoleKey.Escape)
            {
                keyInfo = Console.ReadKey();

                if (keyInfo.Key == ConsoleKey.S)
                {
                    Save();
                }

                if (keyInfo.Key == ConsoleKey.L)
                {
                    Load();
                }

                snake.ChangeDirection(keyInfo);
            }

            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(35, 10);
            Console.WriteLine("GAME OVER <3");
            Console.WriteLine("Your score is:" + score);
            Console.WriteLine("Your level:" + level);
        }
コード例 #4
0
        public void Start()
        {
            Thread th = new Thread(MoveSnake);

            th.Start();

            ConsoleKeyInfo keyInfo = Console.ReadKey();

            while (isAlive && keyInfo.Key != ConsoleKey.Escape)
            {
                keyInfo = Console.ReadKey();
                snake.ChangeDirection(keyInfo);
                if (keyInfo.Key == ConsoleKey.Backspace)
                {
                    GameResume();
                }
                if (keyInfo.Key == ConsoleKey.Spacebar)
                {
                    GameContinue();
                }
            }
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(35, 20);
            Console.WriteLine("GAME OVER!!");
        }
コード例 #5
0
ファイル: Player.cs プロジェクト: dainek1993/SnakeGame
 private void OnClick(object sender, ControlEventArg e)
 {
     if (e.Key == Keys.SPACE)
     {
         isPaused = !isPaused;
     }
     else
     {
         snake.ChangeDirection(e.Direction);
     }
 }
コード例 #6
0
ファイル: Game.cs プロジェクト: Safrus/PP2
        public void Start()
        {
            ConsoleKeyInfo keyInfo = Console.ReadKey();
            Thread         thread  = new Thread(MoveSnake);

            thread.Start();
            while (isAlive && keyInfo.Key != ConsoleKey.Escape)
            {
                keyInfo = Console.ReadKey();
                snake.ChangeDirection(keyInfo);
            }
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.SetCursorPosition(30, 40);
            Console.WriteLine("GAME OVER!");
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: taerd/Snake-new-
        static void Main(string[] args)
        {
            Console.SetWindowSize(x + 40, y + 2);
            Console.SetBufferSize(x + 40, y + 2);
            Console.CursorVisible = false;
            wall = new Wall(x, y, '#');

            snake = new Snake(x / 2, y / 2);

            food = new Food(x, y, '@', snake);
            food.CreateFood();

            Console.SetCursorPosition(x + 2, 1);
            Console.Write("To start - press something");
            Console.SetCursorPosition(x + 2, 3);
            Console.Write("Moving- Arrows, End- Escape");

            pressed = Console.ReadKey();
            game    = true;

            difficulty = 0;

            Console.SetCursorPosition(x + 2, 5);
            Console.Write($"Lvl:{lvl[0]}");

            time = new Timer(Loop, null, 0, 200);

            while (game)
            {
                if (wall.IsHit(snake.GetHead()) || snake.IsHit(snake.GetHead()) || pressed.Key == ConsoleKey.Escape)
                {
                    game = false;
                    time.Change(0, Timeout.Infinite);
                }
                if (Console.KeyAvailable)
                {
                    pressed = Console.ReadKey();
                    snake.ChangeDirection(pressed.Key);
                }
            }
            time.Change(0, Timeout.Infinite);
            Console.SetCursorPosition(x + 2, 7);
            Console.Write("Game over");
            Console.SetCursorPosition(x + 2, 9);
            Console.Write($"Your score is : {snake.score}");
            Console.ReadKey();
        }
コード例 #8
0
ファイル: Game.cs プロジェクト: Jumaikhanova/PP2
        public void Start()
        {
            DrawGameView();
            DrawScore();
            DrawLevel();
            snake.Draw();
            food.Draw();
            wall.Draw();
            Thread thread = new Thread(MoveSnake);

            thread.Start();
            while (isAlive)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey(true);
                if (snake.SnakeMovement(keyInfo))
                {
                    snake.ChangeDirection(keyInfo);
                }

                if (keyInfo.Key == ConsoleKey.Escape)
                {
                    thread.Abort();
                    string fileName = Nickname + ".xml";
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }
                    FileStream    fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    XmlSerializer xs = new XmlSerializer(typeof(List <GameObject>));
                    xs.Serialize(fs, g_objects);
                    fs.Close();
                    MainMenu mainMenu = new MainMenu();
                    mainMenu.Options();
                    mainMenu.Begin();
                }
            }
        }
コード例 #9
0
 private void OnButtonKeyDown(object sender, KeyEventArgs e)
 {
     mySnake.ChangeDirection(e.Key);
 }
コード例 #10
0
ファイル: Game.cs プロジェクト: dragonkam/Snake_1
 public void Move(Directions direct) => snake.ChangeDirection(direct);
コード例 #11
0
 private void OnKeyDown(object sender, KeyDownEventArgs e)
 {
     //以下为按键映射
     if (this.Player1 != null)
     {
         if (e.KeyInfo.Key == ConsoleKey.W)//W
         {
             Player1.ChangeDirection(Direction.Up);
         }
         else if (e.KeyInfo.Key == ConsoleKey.D)//D
         {
             Player1.ChangeDirection(Direction.Right);
         }
         else if (e.KeyInfo.Key == ConsoleKey.S)//S
         {
             Player1.ChangeDirection(Direction.Down);
         }
         else if (e.KeyInfo.Key == ConsoleKey.A)//A
         {
             Player1.ChangeDirection(Direction.Left);
         }
         else if (e.KeyInfo.Key == ConsoleKey.UpArrow)//上
         {
             Player1.ChangeDirection(Direction.Up);
         }
         else if (e.KeyInfo.Key == ConsoleKey.RightArrow)//右
         {
             Player1.ChangeDirection(Direction.Right);
         }
         else if (e.KeyInfo.Key == ConsoleKey.DownArrow)//下
         {
             Player1.ChangeDirection(Direction.Down);
         }
         else if (e.KeyInfo.Key == ConsoleKey.LeftArrow)//左
         {
             Player1.ChangeDirection(Direction.Left);
         }
         else if (e.KeyInfo.Key == ConsoleKey.Enter)
         {
             if (this.Scene.Any(i => i is GameOver))//如果是GameOver
             {
                 this.isPause = true;
                 this.Reset();
             }
             else
             {
                 this.isPause = !this.isPause;
             }
         }
         else if (e.KeyInfo.Key == ConsoleKey.Escape)
         {
             if (this.Scene.Any(i => i is GameOver))
             {
                 this.isQuit = true;
             }
         }
     }
     else if (this.Scene.Any(i => i is GameStart))
     {
         this.Reset();
     }
 }