Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Homework31
- {
- class Progam
- {
- static void Main()
- {
- Console.CursorVisible = false;
- bool isOpen = true;
- int playerX = 3;
- int playerY = 5;
- int score = 0;
- char[,] map =
- {
- {'#', '#','#','#','#','#','#','#','#', '#','#','#','#','#', '#','#','#','#','#','#','#','#', '#','#','#','#'},
- {'#', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ', '#','.',' ','#'},
- {'#', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ', '#','.',' ','#'},
- {'#', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ', '#','.',' ','#'},
- {'#', '#','#','#','#','#','#','#','#', '#','#',' ','.','.', '.','.','.',' ',' ',' ',' ',' ', '#','#',' ','#'},
- {'#', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ','#'},
- {'#', ' ',' ',' ',' ',' ',' ',' ',' ', ' ','#',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ','#'},
- {'#', '#','#','#','#','#','#','#','#', '#','#',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ','#'},
- {'#', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ','#'},
- {'#', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ','#'},
- {'#', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ','.', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ','#'},
- {'#', '#','#','#','#','#','#','#','#', ' ',' ',' ',' ','.', ' ',' ',' ','#','#','#','#','#', '#','#','#','#'},
- {'#', '.','.','.','.','.','.','.','#', ' ',' ',' ',' ','.', ' ',' ',' ','#',' ',' ',' ',' ', ' ',' ',' ','#'},
- {'#', '.','.','.','.','.','.','.','#', ' ',' ',' ',' ','.', ' ',' ',' ','#',' ',' ',' ',' ', ' ',' ',' ','#'},
- {'#', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ','.', ' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ','#'},
- {'#', '#','#','#','#','#','#','#','#', '#','#','#','#','#', '#','#','#','#','#','#','#','#', '#','#','#','#'}
- };
- while (isOpen)
- {
- DrawMap(map, ConsoleColor.Magenta);
- DrawPlayer(playerX, playerY, ConsoleColor.Yellow);
- ShowScore(score);
- HandleInput(ref playerX, ref playerY, map, ref score);
- Console.Clear();
- }
- }
- static void DrawMap(char[,] map, ConsoleColor color = ConsoleColor.Blue)
- {
- Console.ForegroundColor = color;
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Console.Write(map[i, j]);
- }
- Console.WriteLine();
- }
- }
- static void HandleInput(ref int playerX, ref int playerY, char[,] map, ref int score)
- {
- int[] direction = GetDirection();
- int nextPlayerPositionX = playerX + direction[0];
- int nextPlayerPositionY = playerY + direction[1];
- TryMovePlayer(ref playerX, ref playerY, nextPlayerPositionX, nextPlayerPositionY, map);
- score = IncreaseNumberOfPlayerPoints(nextPlayerPositionX, nextPlayerPositionY, map, score);
- }
- static int[] GetDirection()
- {
- const ConsoleKey MoveUpKey = ConsoleKey.W;
- const ConsoleKey MoveDownKey = ConsoleKey.S;
- const ConsoleKey MoveLeftKey = ConsoleKey.A;
- const ConsoleKey MoveRightKey = ConsoleKey.D;
- int[] direction = { 0, 0 };
- Console.SetCursorPosition(0, 17);
- Console.ForegroundColor = ConsoleColor.DarkCyan;
- Console.WriteLine($"{MoveRightKey} - Передвинуть игрока вправо\n{MoveLeftKey} - Передвинуть игрока влево\n{MoveUpKey} - Передвинуть игрока вверх\n{MoveDownKey} - Передвинуть игрока вниз");
- ConsoleKeyInfo pressedKey = Console.ReadKey();
- if (pressedKey.Key == MoveUpKey)
- direction[1] = -1;
- else if (pressedKey.Key == MoveDownKey)
- direction[1] = 1;
- else if (pressedKey.Key == MoveLeftKey)
- direction[0] = -1;
- else if (pressedKey.Key == MoveRightKey)
- direction[0] = 1;
- return direction;
- }
- static void DrawPlayer(int playerX, int playerY, ConsoleColor color = ConsoleColor.Red)
- {
- char player = '@';
- Console.SetCursorPosition(playerX, playerY);
- Console.ForegroundColor = color;
- Console.Write(player);
- }
- static void ShowScore(int score)
- {
- Console.SetCursorPosition(36, 0);
- Console.Write($"Количество собранных очков: {score}.");
- }
- static void TryMovePlayer(ref int playerX, ref int playerY, int nextPlayerPositionX, int nextPlayerPositionY, char[,] map)
- {
- char emptiness = ' ';
- if (map[nextPlayerPositionY, nextPlayerPositionX] == emptiness)
- {
- playerX = nextPlayerPositionX;
- playerY = nextPlayerPositionY;
- }
- }
- static int IncreaseNumberOfPlayerPoints(int nextPlayerPositionX, int nextPlayerPositionY, char[,] map, int score)
- {
- char emptiness = ' ';
- char berry = '.';
- if (map[nextPlayerPositionY, nextPlayerPositionX] == berry)
- {
- score++;
- map[nextPlayerPositionY, nextPlayerPositionX] = emptiness;
- }
- return score;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement