Пример #1
0
        static void Main(string[] args)
        {
            Console.Clear();
            Console.SetCursorPosition(35, 7);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Welcome To Snake Game");
            Console.SetCursorPosition(20, 8);
            Console.WriteLine("Reach 110 score to complete level and complete 10 levels to finish game");
            Console.SetCursorPosition(25, 9);
            Console.WriteLine("Please, Enter Your Name");
            Console.SetCursorPosition(30, 10);
            string line = Console.ReadLine();//user's name

            Console.CursorVisible = false;
            Console.Clear();
            snake.Head();
            snake.Draw();
            food.Draw();
            wall.Draw();
            s.Draw(score, level);
            Thread thread = new Thread(F);

            thread.Start();
            while (true)
            {
                Console.ForegroundColor = ConsoleColor.Black;
                ConsoleKeyInfo key = Console.ReadKey();
                if (key.Key == ConsoleKey.DownArrow && last != 2 && last != 1)
                {
                    snake.Move(0, 1);
                    last    = 1;
                    success = 1;
                }
                if (key.Key == ConsoleKey.UpArrow && last != 1 && last != 2)
                {
                    snake.Move(0, -1);
                    last    = 2;
                    success = 1;
                }
                if (key.Key == ConsoleKey.LeftArrow && last != 4 && last != 3)
                {
                    snake.Move(-1, 0);
                    last    = 3;
                    success = 1;
                }
                if (key.Key == ConsoleKey.RightArrow && last != 3 && last != 4)
                {
                    snake.Move(1, 0);
                    last    = 4;
                    success = 1;
                }//head's move
                if (key.Key == ConsoleKey.F2)
                {
                    snake.Ser();
                    food.Ser();
                    s.Ser(score);
                    s.Ser1(level);
                    wall.Ser();
                }
                if (key.Key == ConsoleKey.F3)
                {
                    snake = snake.Des();
                    wall  = wall.Des();
                    score = int.Parse(s.Des());
                    level = int.Parse(s.Des1());
                    food  = food.Des();
                    Console.Clear();
                    food.Draw();
                    wall.Draw();
                    s.Draw(score, level);
                    success = 1;
                    Console.ReadKey();
                }
                if (success == 1)
                {
                    snake.Draw();
                    food.Draw();
                    if (snake.body.Count < 2)
                    {
                        wall.Draw();
                    }
                }
                success = 0;
                if (snake.CollisionWithFood(food))
                {
                    score = score + 10; // every food = 10 point
                    if (score >= 110)
                    {
                        level++;
                        if (level > 10)
                        {
                            Console.SetCursorPosition(30, 5);
                            Console.ForegroundColor = ConsoleColor.White;
                            Console.WriteLine("Congratulations!!!");
                            Console.SetCursorPosition(30, 6);
                            Console.WriteLine("You completed My Game");
                            Console.ReadKey();
                            return;
                        }
                        speed = 300;
                        score = 0;
                        last  = 0;
                        Console.Clear();
                        wall  = new Wall(level);
                        snake = new Snake();
                        snake.Head();
                        food = new Food(wall, snake);
                        snake.Draw();
                        s.Draw(score, level);// if level up
                    }
                    food = new Food(wall, snake);
                    food.Draw();
                    s.Draw(score, level);// score , level, and food point refresh
                }
                if (snake.CollisionWithBody() || snake.CollisionWithWall(wall))
                {
                    thread.Abort();
                    string ss = "";
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.Clear();
                    Console.SetCursorPosition(30, 7);
                    Console.WriteLine("GAME OVER");
                    Console.SetCursorPosition(28, 8);
                    Console.WriteLine("Your score is " + ((level - 1) * 110 + score));
                    Console.SetCursorPosition(27, 9);
                    Console.WriteLine("press R to restrart or Q to quit");
                    int maxscore = 0;
                    if (!File.Exists(line + ".txt"))
                    {
                        File.WriteAllText(line + ".txt", "0");
                    }
                    ss = File.ReadAllText(line + ".txt");
                    Console.SetCursorPosition(28, 10);
                    Console.WriteLine("Your Best score is " + ss);
                    maxscore = int.Parse(ss);
                    if (maxscore < score)
                    {
                        File.WriteAllText(line + ".txt", score.ToString());
                    }//Highscore
                    score = 0;
                    level = 1;//if Game Over
                    speed = 300;
                    while (true)
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        ConsoleKeyInfo kki = Console.ReadKey();
                        if (kki.Key == ConsoleKey.R)
                        {
                            thread = new Thread(F);
                            thread.Start();
                            break;
                        }
                        if (kki.Key == ConsoleKey.Q)
                        {
                            return;
                        }//Quit or Restart
                    }
                    wall  = new Wall(level);
                    snake = new Snake();
                    snake.Head();
                    food = new Food(wall, snake);
                    Console.Clear();
                    s.Draw(score, level);//start game from the beginning
                    snake.Draw();
                    wall.Draw();
                    food.Draw();
                }
                Console.ForegroundColor = ConsoleColor.Black;
            }
        }
Пример #2
0
        public void StartGame()
        {
            snake.SnakeTail = Snake.InitializeSnake(2, this.level);
            this.points     = 20;
            this.GameOver   = false;
            this.currentKey = ConsoleKey.Delete;
            this.speed      = 300;



            GenerateFoodCustomLevel();

            while (this.GameOver == false)
            {
                if (Console.KeyAvailable)
                {
                    this.currentKey = Console.ReadKey(true).Key;

                    if (this.lastKey == this.currentKey)
                    {
                        if (this.speed > 100)
                        {
                            this.speed -= 40;
                        }
                        else if (this.speed > 30)
                        {
                            this.speed -= 10;
                        }

                        continue;
                    }
                    this.speed = 300;
                }
                // Drawing and updating
                Console.Clear();


                Environment.DrawCustomArea(this.customLevel);
                DrawSpeed();
                DrawPoints();
                Environment.DrawFood(this.foodPositionX, this.foodPositionY);
                currentKey = snake.Update(currentKey, lastDirectionKey);
                snake.Draw();
                snake.DrawLength();

                if (snake.CollisionWithSnake())
                {
                    this.GameOver = true;
                    break;
                }
                if (snake.CollisionWithCustomWall(this.customLevel))
                {
                    this.GameOver = true;
                    break;
                }
                if (snake.CollisionWithFood(this.foodPositionX, this.foodPositionY))
                {
                    this.points += 10;
                    GenerateFoodCustomLevel();
                }



                Thread.Sleep(this.speed);
                this.lastKey     = this.currentKey;
                lastDirectionKey = currentKey;
            }

            this.bestScore = int.Parse(File.ReadLines(Directory.GetCurrentDirectory() + @"\scores\" + currentCustomLevelName + "Score" + ".txt").First());

            Console.Clear();

            if (this.points > this.bestScore)
            {
                File.WriteAllText(Directory.GetCurrentDirectory() + @"\scores\" + this.currentCustomLevelName + "Score" + ".txt", this.points.ToString());
                Console.WriteLine($"New record: {points}");
                Console.WriteLine($"Previus best score: {bestScore}");
            }
            else
            {
                Console.WriteLine($"Current score: {points}");
                Console.WriteLine($"Best score score: {bestScore}");
            }



            Thread.Sleep(2500);
        }
Пример #3
0
        static void game(object all1)
        {
           int a = 0, b = 40, c = 18, da = 1, lll = 3;
           int g = snake.Random1(a, b);
           int g1 = snake.Random2(a, c);
            while (true )
            {
                if (direction == 3)
                {
                    snake.Move(0, -1);
         
                }
                if (direction == 1)
                {
                    snake.Move(-1, 0);
                   
                    
                }
                if (direction == 2)
                {
                    snake.Move(1, 0);
                   
                }
                if (direction == 4)
                {
                    snake.Move(0, 1);
                    
                }
            
                if (snake.CollisionWithWall(wall) || snake.Collision())
                {
                    Console.Clear();
                    Console.SetCursorPosition(5, 5);
                    Console.WriteLine("GAME OVER!!!!");
                    Console.ReadKey();
                    snake = new Snake();
                    level = 1;
                    wall = new Wall(level);
                }
                 Console.Clear();
                if (da % lll == 0)
                {
                    lll *= 2;
                    level++;
                    wall = new Wall(level);
                }

                if (snake.CollisionWithFood(g, g1))
                {
                    da++;
                    g = snake.Random1(a, b);
                    g1 = snake.Random2(a, c);
                   
                }
                if (wall.FoodInWall(g, g1) == true && snake.FoodInMe(g, g1) == true )
                {
                    snake.DrawFood(g, g1);

                }
                else
                {




                    g = snake.Random1(a, b);
                    g1 = snake.Random2(a, c);
                    snake.DrawFood(g, g1);
                }
                
                snake.Draw(direction);
                wall.Draw();
                    
                Thread.Sleep(100);
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            int score   = 0;
            int last    = 0; //if right , then no left
            int level   = 1;
            int success = 0; //to do not refresh

            Console.SetCursorPosition(35, 7);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Welcome To Snake Game");
            Console.SetCursorPosition(20, 8);
            Console.WriteLine("Reach 110 score to complete level and complete 10 levels to finish game");
            Console.SetCursorPosition(25, 9);
            Console.WriteLine("Please, Enter Your Name");
            Console.SetCursorPosition(30, 10);
            string line = Console.ReadLine();//user's name

            Console.CursorVisible = false;
            Console.Clear();
            Wall  wall  = new Wall(level);
            Snake snake = new Snake();
            Score s     = new Score();
            Food  food  = new Food(wall, snake);

            Console.ForegroundColor = ConsoleColor.Green;
            while (true)
            {
                Console.ForegroundColor = ConsoleColor.Black;
                ConsoleKeyInfo key = Console.ReadKey();
                if (key.Key == ConsoleKey.DownArrow && last != 2)
                {
                    snake.Move(0, 1);
                    last    = 1;
                    success = 1;
                }
                if (key.Key == ConsoleKey.UpArrow && last != 1)
                {
                    snake.Move(0, -1);
                    last    = 2;
                    success = 1;
                }
                if (key.Key == ConsoleKey.LeftArrow && last != 4)
                {
                    snake.Move(-1, 0);
                    last    = 3;
                    success = 1;
                }
                if (key.Key == ConsoleKey.RightArrow && last != 3)
                {
                    snake.Move(1, 0);
                    last    = 4;
                    success = 1;
                }//head's move
                if (success == 1)
                {
                    snake.Draw();
                    wall.Draw();
                }
                success = 0;
                if (snake.CollisionWithFood(food))
                {
                    score = score + 10; // every food = 10 point
                    if (score >= 110)
                    {
                        level++;
                        if (level > 10)
                        {
                            Console.SetCursorPosition(30, 5);
                            Console.ForegroundColor = ConsoleColor.White;
                            Console.WriteLine("Congratulations!!!");
                            Console.SetCursorPosition(30, 6);
                            Console.WriteLine("You completed My Game");
                            Console.ReadKey();
                            return;
                        }
                        score = 0;
                        last  = 0;
                        Console.Clear();
                        wall  = new Wall(level);
                        snake = new Snake();
                        food  = new Food(wall, snake);
                        s.Draw(score, level);// if level up
                    }
                    food = new Food(wall, snake);
                    s.Draw(score, level);// score , level, and food point refresh
                }
                if (snake.CollisionWithBody() || snake.CollisionWithWall(wall))
                {
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.Clear();
                    Console.SetCursorPosition(30, 7);
                    Console.WriteLine("GAME OVER");
                    Console.SetCursorPosition(28, 8);
                    Console.WriteLine("Your score is " + ((level - 1) * 110 + score));
                    Console.SetCursorPosition(27, 9);
                    Console.WriteLine("press R to restrart or Q to quit");

                    /*...............................................................................................................
                     * string maxscore = "0";
                     * FileStream fs = new FileStream(line + ".xml", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                     * XmlSerializer xml = new XmlSerializer(typeof(string));
                     * maxscore = xml.Deserialize(fs) as string;
                     * if (score > int.Parse(maxscore))
                     * {
                     *  try
                     *  {
                     *      xml.Serialize(fs, score.ToString());
                     *  }
                     *  catch (Exception e)
                     *  {
                     *      Console.WriteLine(e.ToString());
                     *  }
                     *  finally
                     *  {
                     *      fs.Close();
                     *  }
                     * }
                     */

                    /*  int maxscore = 0;
                     * if (File.Exists(line + ".txt"))
                     * {
                     *    StreamReader sr = new StreamReader(line+".txt");
                     *    maxscore = int.Parse(sr.ReadToEnd());
                     *    sr.Close();
                     * }
                     * else
                     * {
                     *    File.Create(line + ".txt");
                     *    File.WriteAllText(line + ".txt", "0");
                     * }
                     * if (score > maxscore)
                     * {
                     *    File.WriteAllText(line + ".txt", score.ToString());
                     * }*/


                    //....................................................................................................
                    score = 0;
                    level = 1;//if Game Over
                    while (true)
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        ConsoleKeyInfo kki = Console.ReadKey();
                        if (kki.Key == ConsoleKey.R)
                        {
                            break;
                        }
                        if (kki.Key == ConsoleKey.Q)
                        {
                            return;
                        }//Quit or Restart
                    }
                    Console.Clear();
                    wall  = new Wall(level);
                    snake = new Snake();
                    food  = new Food(wall, snake);
                    s.Draw(score, level);//start game from the beginning
                }
            }
        }