Vlad_Savitskiy

C# game

Apr 16th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4.  
  5. namespace CSLightFirst
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             Console.CursorVisible = false;
  12.  
  13.             Random rand = new Random();
  14.             Point playerPosition = new Point(3, 3);
  15.             int playerDX = 0, playerDY = 0;
  16.             Point bonePosition = new Point(1, 1);
  17.             int collectedBones = 0, allBones = 10;
  18.             bool isPlaying = true;
  19.             bool isBoneEat = true;
  20.             List<Point> emptyCellsPositions = new List<Point>();
  21.             char[,] map =
  22.             {
  23.                 {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
  24.                 {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', '#'},
  25.                 {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', '#'},
  26.                 {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', '#', ' ', '#'},
  27.                 {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  28.                 {'#', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  29.                 {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#'},
  30.                 {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#'},
  31.                 {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#'},
  32.                 {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#'},
  33.                 {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#'},
  34.                 {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}
  35.             };
  36.  
  37.             PrintMap(map, emptyCellsPositions);
  38.            
  39.             while (isPlaying)
  40.             {
  41.                 Console.SetCursorPosition(playerPosition.Y, playerPosition.X);
  42.                 Console.Write('@');
  43.                 System.Threading.Thread.Sleep(250);
  44.              
  45.                 if (Console.KeyAvailable)
  46.                 {
  47.                     ConsoleKeyInfo key = Console.ReadKey(true);
  48.                     ChangeDirection(key, ref playerDX, ref playerDY);
  49.                 }
  50.                
  51.                 Move(ref playerPosition, playerDX, playerDY);
  52.                
  53.                 if (isBoneEat)
  54.                 {
  55.                     isBoneEat = false;
  56.                     Point newBonePosition = emptyCellsPositions[rand.Next(0, emptyCellsPositions.Count)];
  57.                     map[newBonePosition.X, newBonePosition.Y] = '!';
  58.                     map[bonePosition.X, bonePosition.Y] = ' ';
  59.                     DrawBone(newBonePosition, bonePosition);
  60.                     bonePosition = newBonePosition;
  61.                 }
  62.                
  63.                 if (map[playerPosition.X, playerPosition.Y] == '!')
  64.                 {
  65.                     collectedBones++;
  66.                     isBoneEat = true;
  67.                 }
  68.                 ShowActualScore(0, map.GetLength(0) + 2, collectedBones, allBones);
  69.  
  70.                 if (map[playerPosition.X, playerPosition.Y] == '#')
  71.                     isPlaying = false;
  72.                 if (collectedBones == allBones)
  73.                     break;
  74.             }
  75.  
  76.             if (!isPlaying)
  77.                 ShowFinalText(0, 15,
  78.                     "Вы не смогли собрать все косточки...",
  79.                     ConsoleColor.DarkRed);
  80.             else
  81.                 ShowFinalText(0, 15,
  82.                     "Позравляем, вы откопали все косточки в этом дворе!",
  83.                     ConsoleColor.DarkGreen);
  84.         }
  85.  
  86.         private static void ChangeDirection(ConsoleKeyInfo key, ref int playerDX, ref int playerDY)
  87.         {
  88.             switch (key.Key)
  89.             {
  90.                 case ConsoleKey.UpArrow:
  91.                     playerDX = -1; playerDY = 0;
  92.                     break;
  93.                 case ConsoleKey.DownArrow:
  94.                     playerDX = 1; playerDY = 0;
  95.                     break;
  96.                 case ConsoleKey.LeftArrow:
  97.                     playerDX = 0; playerDY = -1;
  98.                     break;
  99.                 case ConsoleKey.RightArrow:
  100.                     playerDX = 0; playerDY = 1;
  101.                     break;
  102.             }
  103.         }
  104.  
  105.         private static void DrawBone(Point bonePosition, Point oldBonePosition)
  106.         {
  107.             Console.SetCursorPosition(oldBonePosition.Y, oldBonePosition.X);
  108.             Console.Write(" ");
  109.  
  110.             Console.SetCursorPosition(bonePosition.Y, bonePosition.X);
  111.             Console.Write("!");
  112.         }
  113.  
  114.         private static void Move(ref Point position, int DX, int DY)
  115.         {
  116.             Console.SetCursorPosition(position.Y, position.X);
  117.             Console.Write(" ");
  118.  
  119.             position.X += DX;
  120.             position.Y += DY;
  121.  
  122.             Console.SetCursorPosition(position.Y, position.X);
  123.             Console.Write("@");
  124.         }
  125.  
  126.         private static void ShowActualScore(int x, int y, int score, int maxScore)
  127.         {
  128.             Console.SetCursorPosition(x, y);
  129.             Console.ForegroundColor = ConsoleColor.DarkCyan;
  130.             Console.WriteLine($"Косточек собрано {score} из {maxScore}");
  131.             Console.ForegroundColor = ConsoleColor.DarkRed;
  132.         }
  133.  
  134.         private static void ShowFinalText(int x, int y, string text, ConsoleColor foregroundColor)
  135.         {
  136.             Console.SetCursorPosition(x, y);
  137.             Console.ForegroundColor = foregroundColor;
  138.             Console.WriteLine(text);
  139.             Console.ReadKey();
  140.         }
  141.  
  142.         private static void PrintMap(char[,] map, List<Point> emptyCellsPositions)
  143.         {
  144.             Console.ForegroundColor = ConsoleColor.Yellow;
  145.  
  146.             for (int i = 0; i < map.GetLength(0); i++)
  147.             {
  148.                 for (int j = 0; j < map.GetLength(1); j++)
  149.                 {
  150.                     if (map[i, j] == ' ')
  151.                         emptyCellsPositions.Add(new Point(i, j));
  152.                     Console.Write(map[i, j]);
  153.                 }
  154.                 Console.WriteLine();
  155.             }
  156.  
  157.             Console.ForegroundColor = ConsoleColor.DarkRed;
  158.         }
  159.     }
  160. }
Add Comment
Please, Sign In to add comment