Advertisement
AlexStraga87

Brave 2

Feb 17th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp1
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {            
  14.             int playerX = 0, playerY = 0;
  15.             int hp = 9;
  16.             bool isAlive = true;
  17.             int dX = 0, dY = 0;
  18.  
  19.             char[,] map = LoadMapIntegrated(ref playerX, ref playerY); //LoadMap("map1", ref playerX, ref playerY);
  20.  
  21.             while (isAlive)
  22.             {
  23.                 Console.Clear();
  24.                 DrawBar(0, 0, hp);
  25.                 DrawMap(map);
  26.                 DrawPlayer(playerX, playerY);
  27.  
  28.                 ConsoleKeyInfo key = Console.ReadKey(true);
  29.                 switch (key.Key)
  30.                 {
  31.                     case ConsoleKey.UpArrow:
  32.                         dX = 0;
  33.                         dY = -1;                        
  34.                         break;
  35.                     case ConsoleKey.DownArrow:
  36.                         dX = 0;
  37.                         dY = 1;
  38.                         break;
  39.                     case ConsoleKey.LeftArrow:
  40.                         dX = -1;
  41.                         dY = 0;
  42.                         break;
  43.                     case ConsoleKey.RightArrow:
  44.                         dX = 1;
  45.                         dY = 0;
  46.                         break;
  47.                 }
  48.  
  49.                 bool isMoved = TryMove(map, ref playerX, ref playerY, dX, dY);
  50.                 if (isMoved)
  51.                 {
  52.                     CheckItemGround(map, playerX, playerY, ref hp);
  53.                     if (hp <=0)
  54.                     {
  55.                         isAlive = false;
  56.                         DrawBar(0, 0, hp);
  57.                         DrawMap(map);
  58.                         DrawPlayer(playerX, playerY);
  59.                     }  
  60.                 }                              
  61.             }
  62.  
  63.             Console.SetCursorPosition(0, 15);
  64.             Console.WriteLine("Game over");
  65.             Console.ReadKey();
  66.         }
  67.  
  68.         static bool TryMove(char[,] map, ref int playerX, ref int playerY, int dX, int dY)
  69.         {
  70.             if (map[playerY + dY,playerX + dX] != '#')
  71.             {                
  72.                 playerX += dX;
  73.                 playerY += dY;
  74.                 return true;
  75.             }
  76.             return false;
  77.         }
  78.  
  79.         static void CheckItemGround(char[,] map, int playerX, int playerY, ref int hp)
  80.         {
  81.             if (map[playerY, playerX] == '*')
  82.             {
  83.                 hp--;
  84.             }
  85.             else if (map[playerY, playerX] == '+')
  86.             {
  87.                 hp++;
  88.                 map[playerY, playerX] = ' ';
  89.                 if (hp > 10)
  90.                 {
  91.                     hp = 10;
  92.                 }
  93.             }
  94.         }
  95.  
  96.         static void DrawPlayer(int playerX, int playerY)
  97.         {
  98.             Console.SetCursorPosition(playerX, playerY + 3);
  99.             Console.Write("X");
  100.         }
  101.  
  102.         static void DrawMap(char[,] map)
  103.         {
  104.             Console.SetCursorPosition(0, 3);
  105.             for (int i = 0; i < map.GetLength(0); i++)
  106.             {
  107.                 for (int j = 0; j < map.GetLength(1); j++)
  108.                 {
  109.                     Console.Write(map[i,j]);
  110.                 }
  111.                 Console.WriteLine();
  112.             }            
  113.         }
  114.  
  115.         static char[,] LoadMap(string name, ref int playerX, ref int playerY)
  116.         {
  117.             string[] file = File.ReadAllLines("maps/"+name+".txt");
  118.             char[,] map = new char[file.Length, file[0].Length];
  119.  
  120.             for (int i = 0; i < file.GetLength(0); i++)
  121.             {
  122.                 for (int j = 0; j < file[0].Length; j++)
  123.                 {
  124.                     map[i, j] = file[i][j];
  125.                     if (map[i, j] == 'X')
  126.                     {
  127.                         playerX = j;
  128.                         playerY = i;
  129.                         map[i, j] = ' ';
  130.                     }
  131.                 }
  132.             }
  133.             return map;
  134.         }
  135.  
  136.         static char[,] LoadMapIntegrated(ref int playerX, ref int playerY)
  137.         {
  138.             string[] file = new string[] {"########################",
  139.                                           "#       +              #",
  140.                                           "# ######               #",
  141.                                           "#   #                  #",
  142.                                           "# # # *##    +         #",
  143.                                           "# # #* #               #",
  144.                                           "#   X    **            #",
  145.                                           "#                      #",
  146.                                           "########################"};
  147.             char[,] map = new char[file.Length, file[0].Length];
  148.  
  149.             for (int i = 0; i < file.GetLength(0); i++)
  150.             {
  151.                 for (int j = 0; j < file[0].Length; j++)
  152.                 {
  153.                     map[i, j] = file[i][j];
  154.                     if (map[i, j] == 'X')
  155.                     {
  156.                         playerX = j;
  157.                         playerY = i;
  158.                         map[i, j] = ' ';
  159.                     }
  160.                 }
  161.             }
  162.             return map;
  163.         }
  164.  
  165.         static void DrawBar(int posX, int posY, int hp, ConsoleColor color = ConsoleColor.Red)
  166.         {
  167.             Console.SetCursorPosition(posX, posY);
  168.             string bar = "";
  169.             for (int i = 0; i < hp; i++)
  170.             {
  171.                 bar += " ";
  172.             }
  173.             Console.Write("[");
  174.  
  175.             ConsoleColor prevColor = Console.BackgroundColor;            
  176.             Console.BackgroundColor = color;
  177.             Console.Write(bar);
  178.             Console.BackgroundColor = prevColor;
  179.  
  180.             bar = "";
  181.             for (int i = hp; i < 10; i++)
  182.             {
  183.                 bar += "_";
  184.             }
  185.  
  186.             bar += "]";
  187.             Console.Write(bar);
  188.         }
  189.  
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement