Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int playerX = 0, playerY = 0;
- int hp = 9;
- bool isAlive = true;
- int dX = 0, dY = 0;
- char[,] map = LoadMapIntegrated(ref playerX, ref playerY); //LoadMap("map1", ref playerX, ref playerY);
- while (isAlive)
- {
- Console.Clear();
- DrawBar(0, 0, hp);
- DrawMap(map);
- DrawPlayer(playerX, playerY);
- ConsoleKeyInfo key = Console.ReadKey(true);
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- dX = 0;
- dY = -1;
- break;
- case ConsoleKey.DownArrow:
- dX = 0;
- dY = 1;
- break;
- case ConsoleKey.LeftArrow:
- dX = -1;
- dY = 0;
- break;
- case ConsoleKey.RightArrow:
- dX = 1;
- dY = 0;
- break;
- }
- bool isMoved = TryMove(map, ref playerX, ref playerY, dX, dY);
- if (isMoved)
- {
- CheckItemGround(map, playerX, playerY, ref hp);
- if (hp <=0)
- {
- isAlive = false;
- DrawBar(0, 0, hp);
- DrawMap(map);
- DrawPlayer(playerX, playerY);
- }
- }
- }
- Console.SetCursorPosition(0, 15);
- Console.WriteLine("Game over");
- Console.ReadKey();
- }
- static bool TryMove(char[,] map, ref int playerX, ref int playerY, int dX, int dY)
- {
- if (map[playerY + dY,playerX + dX] != '#')
- {
- playerX += dX;
- playerY += dY;
- return true;
- }
- return false;
- }
- static void CheckItemGround(char[,] map, int playerX, int playerY, ref int hp)
- {
- if (map[playerY, playerX] == '*')
- {
- hp--;
- }
- else if (map[playerY, playerX] == '+')
- {
- hp++;
- map[playerY, playerX] = ' ';
- if (hp > 10)
- {
- hp = 10;
- }
- }
- }
- static void DrawPlayer(int playerX, int playerY)
- {
- Console.SetCursorPosition(playerX, playerY + 3);
- Console.Write("X");
- }
- static void DrawMap(char[,] map)
- {
- Console.SetCursorPosition(0, 3);
- 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 char[,] LoadMap(string name, ref int playerX, ref int playerY)
- {
- string[] file = File.ReadAllLines("maps/"+name+".txt");
- char[,] map = new char[file.Length, file[0].Length];
- for (int i = 0; i < file.GetLength(0); i++)
- {
- for (int j = 0; j < file[0].Length; j++)
- {
- map[i, j] = file[i][j];
- if (map[i, j] == 'X')
- {
- playerX = j;
- playerY = i;
- map[i, j] = ' ';
- }
- }
- }
- return map;
- }
- static char[,] LoadMapIntegrated(ref int playerX, ref int playerY)
- {
- string[] file = new string[] {"########################",
- "# + #",
- "# ###### #",
- "# # #",
- "# # # *## + #",
- "# # #* # #",
- "# X ** #",
- "# #",
- "########################"};
- char[,] map = new char[file.Length, file[0].Length];
- for (int i = 0; i < file.GetLength(0); i++)
- {
- for (int j = 0; j < file[0].Length; j++)
- {
- map[i, j] = file[i][j];
- if (map[i, j] == 'X')
- {
- playerX = j;
- playerY = i;
- map[i, j] = ' ';
- }
- }
- }
- return map;
- }
- static void DrawBar(int posX, int posY, int hp, ConsoleColor color = ConsoleColor.Red)
- {
- Console.SetCursorPosition(posX, posY);
- string bar = "";
- for (int i = 0; i < hp; i++)
- {
- bar += " ";
- }
- Console.Write("[");
- ConsoleColor prevColor = Console.BackgroundColor;
- Console.BackgroundColor = color;
- Console.Write(bar);
- Console.BackgroundColor = prevColor;
- bar = "";
- for (int i = hp; i < 10; i++)
- {
- bar += "_";
- }
- bar += "]";
- Console.Write(bar);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement