Exemplo n.º 1
0
        public void Start()
        {
            Console.Clear();
            Console.Write("Please, Enter Your Name : ");
            name = Console.ReadLine();
            ConsoleKeyInfo key = Console.ReadKey();

            Console.ForegroundColor = ConsoleColor.Green;
            Thread thread = new Thread(preDRAW);

            thread.Start();
            while (alive || key.Key != ConsoleKey.Q)
            {
                if (alive && key.Key == ConsoleKey.Escape)
                {
                    Save(food, snake, wall);
                }
                Console.Write("Score : " + score);
                key = Console.ReadKey();
                snake.ChangeDirection(key);
            }
            Dead();
            Game game = new Game();

            if (key.Key == ConsoleKey.R)
            {
                game.Start();
            }
            if (key.Key == ConsoleKey.Escape)
            {
                Environment.Exit(0);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Snake mySnake = new Snake(new Vector {
                X = 3, Y = 10
            }, new Vector {
                X = 1, Y = 0
            });
            Food food = new Food(new Vector {
                X = randGenerator.Next(1, WorldWidth), Y = randGenerator.Next(1, WorldHeight)
            });
            ConsoleRenderer renderer = new ConsoleRenderer(WorldWidth, WorldHeight);
            Keyboard        keyboard = new Keyboard();

            keyboard.OnLeftKeyPressed += (sender, eventInfo) =>
            {
                mySnake.ChangeDirection(new Vector {
                    X = -1, Y = 0
                });
            };
            keyboard.OnUpKeyPressed += (sender, eventInfo) =>
            {
                mySnake.ChangeDirection(new Vector {
                    X = 0, Y = -1
                });
            };
            keyboard.OnRightKeyPressed += (sender, eventInfo) =>
            {
                mySnake.ChangeDirection(new Vector {
                    X = 1, Y = 0
                });
            };
            keyboard.OnDownKeyPressed += (sender, eventInfo) =>
            {
                mySnake.ChangeDirection(new Vector {
                    X = 0, Y = 1
                });
            };

            GameEngine gameEngine = new GameEngine(mySnake, keyboard, food, renderer, WorldWidth, WorldHeight);

            gameEngine.Run();
        }
Exemplo n.º 3
0
 private void MainForm_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.Left)
     {
         snake.ChangeDirection(-1, 0);
     }
     else
     if (e.KeyCode == Keys.Right)
     {
         snake.ChangeDirection(1, 0);
     }
     else
     if (e.KeyCode == Keys.Up)
     {
         snake.ChangeDirection(0, -1);
     }
     else
     if (e.KeyCode == Keys.Down)
     {
         snake.ChangeDirection(0, 1);
     }
     else
     {
         if (e.KeyCode == Keys.Space)
         {
             if (timer.Enabled)
             {
                 timer.Stop();
             }
             else
             {
                 timer.Start();
             }
         }
     }
 }