Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- namespace CSLightFirst
- {
- class Program
- {
- static void Main()
- {
- Console.CursorVisible = false;
- Random rand = new Random();
- Point playerPosition = new Point(3, 3);
- int playerDX = 0, playerDY = 0;
- Point bonePosition = new Point(1, 1);
- int collectedBones = 0, allBones = 10;
- bool isPlaying = true;
- bool isBoneEat = true;
- List<Point> emptyCellsPositions = new List<Point>();
- char[,] map =
- {
- {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', '#', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}
- };
- PrintMap(map, emptyCellsPositions);
- while (isPlaying)
- {
- Console.SetCursorPosition(playerPosition.Y, playerPosition.X);
- Console.Write('@');
- System.Threading.Thread.Sleep(250);
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- ChangeDirection(key, ref playerDX, ref playerDY);
- }
- Move(ref playerPosition, playerDX, playerDY);
- if (isBoneEat)
- {
- isBoneEat = false;
- Point newBonePosition = emptyCellsPositions[rand.Next(0, emptyCellsPositions.Count)];
- map[newBonePosition.X, newBonePosition.Y] = '!';
- map[bonePosition.X, bonePosition.Y] = ' ';
- DrawBone(newBonePosition, bonePosition);
- bonePosition = newBonePosition;
- }
- if (map[playerPosition.X, playerPosition.Y] == '!')
- {
- collectedBones++;
- isBoneEat = true;
- }
- ShowActualScore(0, map.GetLength(0) + 2, collectedBones, allBones);
- if (map[playerPosition.X, playerPosition.Y] == '#')
- isPlaying = false;
- if (collectedBones == allBones)
- break;
- }
- if (!isPlaying)
- ShowFinalText(0, 15,
- "Вы не смогли собрать все косточки...",
- ConsoleColor.DarkRed);
- else
- ShowFinalText(0, 15,
- "Позравляем, вы откопали все косточки в этом дворе!",
- ConsoleColor.DarkGreen);
- }
- private static void ChangeDirection(ConsoleKeyInfo key, ref int playerDX, ref int playerDY)
- {
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- playerDX = -1; playerDY = 0;
- break;
- case ConsoleKey.DownArrow:
- playerDX = 1; playerDY = 0;
- break;
- case ConsoleKey.LeftArrow:
- playerDX = 0; playerDY = -1;
- break;
- case ConsoleKey.RightArrow:
- playerDX = 0; playerDY = 1;
- break;
- }
- }
- private static void DrawBone(Point bonePosition, Point oldBonePosition)
- {
- Console.SetCursorPosition(oldBonePosition.Y, oldBonePosition.X);
- Console.Write(" ");
- Console.SetCursorPosition(bonePosition.Y, bonePosition.X);
- Console.Write("!");
- }
- private static void Move(ref Point position, int DX, int DY)
- {
- Console.SetCursorPosition(position.Y, position.X);
- Console.Write(" ");
- position.X += DX;
- position.Y += DY;
- Console.SetCursorPosition(position.Y, position.X);
- Console.Write("@");
- }
- private static void ShowActualScore(int x, int y, int score, int maxScore)
- {
- Console.SetCursorPosition(x, y);
- Console.ForegroundColor = ConsoleColor.DarkCyan;
- Console.WriteLine($"Косточек собрано {score} из {maxScore}");
- Console.ForegroundColor = ConsoleColor.DarkRed;
- }
- private static void ShowFinalText(int x, int y, string text, ConsoleColor foregroundColor)
- {
- Console.SetCursorPosition(x, y);
- Console.ForegroundColor = foregroundColor;
- Console.WriteLine(text);
- Console.ReadKey();
- }
- private static void PrintMap(char[,] map, List<Point> emptyCellsPositions)
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- if (map[i, j] == ' ')
- emptyCellsPositions.Add(new Point(i, j));
- Console.Write(map[i, j]);
- }
- Console.WriteLine();
- }
- Console.ForegroundColor = ConsoleColor.DarkRed;
- }
- }
- }
Add Comment
Please, Sign In to add comment