示例#1
0
        static void Main(string[] args)
        {
            /*
             * Utils.DrawChar('A', 10, 10);
             * Console.ReadKey();
             */

            //Controlar fps
            int      fps               = 0;
            int      desiredFPS        = 30;
            double   inverseDesiredFPS = 1.0 / desiredFPS;
            DateTime currentTime;
            TimeSpan deltaTime;

            int      frameCount  = 0;
            DateTime lastTime    = DateTime.Now;
            double   elapsedTime = 0;

            var game = new Game();

            game.Initialize();

            while (true)
            {
                game.Input();
                game.Update();
                game.Draw();

                //Console.SetCursorPosition(0, 21);
                //Console.WriteLine("");
                //Console.WriteLine($"FPS: {fps}/{inverseDesiredFPS}/{desiredFPS}");

                currentTime = DateTime.Now;
                deltaTime   = currentTime - lastTime;

                elapsedTime += deltaTime.TotalSeconds;

                while (deltaTime.TotalSeconds <= inverseDesiredFPS)
                {
                    deltaTime = DateTime.Now - lastTime;
                }

                if (elapsedTime >= 1)
                {
                    fps        = frameCount;
                    frameCount = 0;
                    elapsedTime--;
                }

                lastTime = currentTime;

                frameCount++;
            }
        }
示例#2
0
        public static void Move()
        {
            Game.Draw();
            bool drawed1 = false, drawed2 = false, drawed3 = false;

            while (!Game.Gameover)
            {
                if (Game.Gameover == true)
                {
                    break;
                }

                Game.snake.DrawSnake();
                DrawScore();
                SaveGame(new DataSave());

                if (Game.snake.dir == 4)
                {
                    Game.snake.Move(0, -1, Game.wall);
                }
                if (Game.snake.dir == 2)
                {
                    Game.snake.Move(0, 1, Game.wall);
                }
                if (Game.snake.dir == 3)
                {
                    Game.snake.Move(-1, 0, Game.wall);
                }
                if (Game.snake.dir == 1)
                {
                    Game.snake.Move(1, 0, Game.wall);
                }

                if (Game.score == 3 && !drawed1)
                {
                    Console.Clear();
                    Game.wall.DrawMap();
                    Game.snake.DrawSnake();
                    Game.food.DrawFood();
                    DrawScore();
                    drawed1 = true;
                }

                if (Game.score == 6 && !drawed2)
                {
                    Console.Clear();
                    Game.wall.DrawMap();
                    Game.snake.DrawSnake();
                    Game.food.DrawFood();
                    DrawScore();
                    drawed2 = true;
                }

                if (Game.score == 9 && !drawed3)
                {
                    Console.Clear();
                    Game.wall.DrawMap();
                    Game.snake.DrawSnake();
                    Game.food.DrawFood();
                    DrawScore();
                    drawed3 = true;
                }

                Thread.Sleep(100);
            }
            GameEnd();
        }
示例#3
0
文件: MainMenu.cs 项目: Almanova/PP2
        public void StartMenu()
        {
            ConsoleKeyInfo consoleKey = Console.ReadKey();

            while (true)
            {
                Show();
                consoleKey = Console.ReadKey();
                if (consoleKey.Key == ConsoleKey.UpArrow)
                {
                    Up();
                }
                if (consoleKey.Key == ConsoleKey.DownArrow)
                {
                    Down();
                }
                if (consoleKey.Key == ConsoleKey.Enter)
                {
                    if (cursor == 0)
                    {
                        Console.Clear();
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.SetCursorPosition(55, 7);
                        Console.WriteLine("SNAKE GAME");
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.SetCursorPosition(50, 11);
                        Console.WriteLine("Enter your Nickname:");
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Console.SetCursorPosition(45, 20);
                        Console.WriteLine("Press ENTER to start a new game");
                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                        Console.SetCursorPosition(55, 12);
                        string nickname = Console.ReadLine();
                        Game   game     = new Game(nickname);
                        game.Draw();
                        game.Start();
                    }

                    if (cursor == 1)
                    {
                        Console.Clear();
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.SetCursorPosition(55, 7);
                        Console.WriteLine("SNAKE GAME");
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.SetCursorPosition(50, 11);
                        Console.WriteLine("Enter your Nickname:");
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Console.SetCursorPosition(44, 20);
                        Console.WriteLine("Press ENTER to continue the game");
                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                        Console.SetCursorPosition(55, 12);
                        string nickname = Console.ReadLine();
                        string fileName = nickname + ".xml";
                        if (File.Exists(fileName))
                        {
                            FileStream        fs           = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                            XmlSerializer     xs           = new XmlSerializer(typeof(List <GameObject>));
                            List <GameObject> GameObjects2 = xs.Deserialize(fs) as List <GameObject>;
                            fs.Close();
                            Game game2 = new Game(GameObjects2, nickname);
                            game2.Start();
                        }
                        else
                        {
                            Console.Clear();
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.SetCursorPosition(55, 7);
                            Console.WriteLine("SNAKE GAME");
                            Console.ForegroundColor = ConsoleColor.Magenta;
                            Console.SetCursorPosition(53, 11);
                            Console.WriteLine("NO SAVED GAMES");
                            Console.ForegroundColor = ConsoleColor.DarkGray;
                            Console.SetCursorPosition(44, 20);
                            Console.WriteLine("Press any key to go back to Menu");
                            Console.ReadKey();
                        }
                    }

                    if (cursor == 2)
                    {
                        Environment.Exit(0);
                    }
                }
            }
        }
示例#4
0
 private void MainFrm_Paint(object sender, PaintEventArgs e)
 {
     Game.Draw(e.Graphics);
 }