Advertisement
tehKaiN

napierdalanka 28.05.2015

May 28th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace gra
  8. {
  9.     class map
  10.     {
  11.         static Byte bX, bY;
  12.         static public Byte bMarginY;
  13.         static public bool[,] aWalls;
  14.  
  15.         static public void generate(Byte x, Byte y)
  16.         {
  17.             bX = x;
  18.             bY = y;
  19.             bMarginY = 1;
  20.             aWalls = new bool[bX, bY];
  21.  
  22.             Console.SetBufferSize(x, y+10);
  23.             Console.SetWindowSize(x, y+10);
  24.  
  25.             Random rr = new Random();
  26.             for (x = 0; x != bX; ++x)
  27.                 for (y = 0; y != bY; ++y)
  28.                 {
  29.                     if (x == 0 || x == bX - 1 || y == 0 || y == bY - 1)
  30.                         aWalls[x, y] = true;
  31.                     else
  32.                     {
  33.                         if (rr.Next(10) < 1)
  34.                             aWalls[x, y] = true;
  35.                         else
  36.                             aWalls[x, y] = false;
  37.                     }
  38.                 }
  39.             display();
  40.         }
  41.  
  42.         static public void display()
  43.         {
  44.             char[] aBfr = new char[bX*bY];
  45.             Byte x, y;
  46.             // fill bfr with walls
  47.             Console.ForegroundColor = ConsoleColor.DarkGray;
  48.             for (y = 0; y != bY; ++y)
  49.             {
  50.                 for (x = 0; x != bX; ++x)
  51.                     if (map.aWalls[x, y])
  52.                         aBfr[x + bX*y] = '#';
  53.                     else
  54.                         aBfr[x + bX*y] = ' ';
  55.             }
  56.  
  57.             Console.SetCursorPosition(0, bMarginY);
  58.             Console.Write(aBfr);
  59.             //Console.SetWindowPosition(0, 0);
  60.             Console.SetCursorPosition(0, 0);
  61.         }
  62.  
  63.     }
  64.  
  65.     class entity
  66.     {
  67.         public Byte bPosX, bPosY;
  68.         Char cAppearance;
  69.         ConsoleColor oColor;
  70.  
  71.         public void draw()
  72.         {
  73.             Console.SetCursorPosition(bPosX, bPosY + map.bMarginY);
  74.             Console.ForegroundColor = oColor;
  75.             Console.Write(cAppearance);
  76.         }
  77.  
  78.         public entity(Byte x, Byte y, Char c, ConsoleColor cc)
  79.         {
  80.             bPosX = x;
  81.             bPosY = y;
  82.             cAppearance = c;
  83.             oColor = cc;
  84.             draw();
  85.  
  86.         }
  87.         public void erase()
  88.         {
  89.             Console.SetCursorPosition(bPosX, bPosY + map.bMarginY);
  90.             Console.Write(' ');
  91.         }
  92.  
  93.         public void move(SByte dx, SByte dy)
  94.         {
  95.             if (!map.aWalls[bPosX + dx, bPosY + dy])
  96.             {
  97.                 erase();
  98.                 bPosX = (Byte)(bPosX + dx);
  99.                 bPosY = (Byte)(bPosY + dy);
  100.                 draw();
  101.             }
  102.         }
  103.  
  104.     }
  105.  
  106.     class Program
  107.     {
  108.         static void Main(string[] args)
  109.         {
  110.             Console.CursorVisible = false;
  111.             map.generate(80, 25);
  112.  
  113.             entity oPlayer = new entity(5, 5, '\x02', ConsoleColor.Green);
  114.  
  115.             while (true)
  116.             {
  117.                 if (Console.KeyAvailable)
  118.                 {
  119.                     ConsoleKey kk = Console.ReadKey(true).Key;
  120.  
  121.                     switch (kk)
  122.                     {
  123.                         case ConsoleKey.Escape:
  124.                             return;
  125.                         case ConsoleKey.W:
  126.                             oPlayer.move(0, -1);
  127.                             break;
  128.                         case ConsoleKey.S:
  129.                             oPlayer.move(0, 1);
  130.                             break;
  131.                         case ConsoleKey.A:
  132.                             oPlayer.move(-1, 0);
  133.                             break;
  134.                         case ConsoleKey.D:
  135.                             oPlayer.move(1, 0);
  136.                             break;
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement