Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace graNaKonsole
- {
- class hud
- {
- static Byte bHpBarLength = 20;
- static public void drawHP()
- {
- Byte bHp = map.oPlayer.bHP;
- Byte bMaxHp = map.oPlayer.bMaxHP;
- Double dHpPercent = Convert.ToDouble(bHp) / Convert.ToDouble(bMaxHp);
- ConsoleColor ccLight, ccDark;
- if(dHpPercent <= 0.25)
- {
- ccLight = ConsoleColor.Red;
- ccDark = ConsoleColor.DarkRed;
- }
- else if(dHpPercent <= 0.5)
- {
- ccLight = ConsoleColor.Yellow;
- ccDark = ConsoleColor.DarkYellow;
- }
- else
- {
- ccLight = ConsoleColor.Green;
- ccDark = ConsoleColor.DarkGreen;
- }
- char[] szBarOutTxt = new char[bHpBarLength];
- string szBarText = String.Format("{0}/{1}", bHp, bMaxHp);
- szBarText.CopyTo(0, szBarOutTxt, (bHpBarLength + 1 - szBarText.Length) / 2, szBarText.Length);
- Console.ForegroundColor = ConsoleColor.Red;
- Console.SetCursorPosition(0, 0);
- Console.Write(" \x03 ");
- Console.ForegroundColor = ccDark;
- Console.BackgroundColor = ccLight;
- Console.Write(szBarOutTxt.Take(bHpBarLength * bHp / bMaxHp).ToArray());
- Console.ForegroundColor = ccLight;
- Console.BackgroundColor = ccDark;
- Console.Write(szBarOutTxt.Skip(bHpBarLength * bHp / bMaxHp).ToArray());
- Console.BackgroundColor = ConsoleColor.Black;
- }
- static public void drawAll()
- {
- drawHP();
- }
- }
- class map
- {
- static public List<entity> lstEntities;
- static public living oPlayer;
- static public Byte bX, bY;
- static public Byte bMarginY;
- static public bool[,] aWalls;
- static public void generate(Byte x, Byte y)
- {
- lstEntities = new List<entity>();
- bX = x;
- bY = y;
- bMarginY = 1;
- aWalls = new bool[bX, bY];
- Console.SetBufferSize(x, y+10);
- Console.SetWindowSize(x, y+10);
- Random rr = new Random();
- for (x = 0; x != bX; ++x)
- for (y = 0; y != bY; ++y)
- {
- if (x == 0 || x == bX - 1 || y == 0 || y == bY - 1)
- aWalls[x, y] = true;
- else
- {
- if (rr.Next(10) < 1)
- aWalls[x, y] = true;
- else
- aWalls[x, y] = false;
- }
- }
- display();
- }
- static public void display()
- {
- char[] aBfr = new char[bX*bY];
- Byte x, y;
- // fill bfr with walls
- Console.ForegroundColor = ConsoleColor.DarkGray;
- for (y = 0; y != bY; ++y)
- {
- for (x = 0; x != bX; ++x)
- if (map.aWalls[x, y])
- aBfr[x + bX*y] = '#';
- else
- aBfr[x + bX*y] = ' ';
- }
- Console.SetCursorPosition(0, bMarginY);
- Console.Write(aBfr);
- //Console.SetWindowPosition(0, 0);
- Console.SetCursorPosition(0, 0);
- }
- }
- class entity:IEquatable<entity>
- {
- public Byte bPosX, bPosY;
- Int32 lId;
- Char cAppearance;
- ConsoleColor oColor;
- public bool Equals(entity ent)
- {
- if (this.lId == ent.lId)
- return true;
- return false;
- }
- public void draw()
- {
- Console.SetCursorPosition(bPosX, bPosY + map.bMarginY);
- Console.ForegroundColor = oColor;
- Console.Write(cAppearance);
- }
- public entity(Byte x, Byte y, Char c, ConsoleColor cc)
- {
- bPosX = x;
- bPosY = y;
- cAppearance = c;
- oColor = cc;
- draw();
- map.lstEntities.Add(this);
- lId = map.lstEntities.Count;
- }
- public entity(Char c, ConsoleColor cc)
- {
- Random rr = new Random();
- bool done = false;
- while(!done)
- {
- bPosX = (Byte)rr.Next(1, map.bX-1);
- bPosY = (Byte)rr.Next(1, map.bY-1);
- done = true;
- foreach(entity ent in map.lstEntities)
- {
- if (ent.bPosX == bPosX && ent.bPosY == bPosY)
- {
- done = false;
- break;
- }
- }
- }
- cAppearance = c;
- oColor = cc;
- draw();
- map.lstEntities.Add(this);
- lId = map.lstEntities.Count;
- }
- public void erase()
- {
- Console.SetCursorPosition(bPosX, bPosY + map.bMarginY);
- Console.Write(' ');
- }
- public void move(SByte dx, SByte dy)
- {
- if (map.aWalls[bPosX + dx, bPosY + dy])
- return;
- foreach(entity ent in map.lstEntities)
- if((ent.bPosX == bPosX + dx) && (ent.bPosY == bPosY + dy))
- {
- ent.interact();
- return;
- }
- erase();
- bPosX = (Byte)(bPosX + dx);
- bPosY = (Byte)(bPosY + dy);
- draw();
- }
- public virtual void interact()
- {
- }
- }
- class living:entity
- {
- public Byte bHP;
- public Byte bMaxHP;
- public living(Byte x, Byte y, Char c, ConsoleColor cc, Byte hp, Byte maxHP): base(x,y,c,cc)
- {
- bHP = hp;
- bMaxHP = maxHP;
- }
- }
- class potion: entity
- {
- Byte bHP;
- public override void interact()
- {
- map.oPlayer.bHP = (Byte)Math.Min(map.oPlayer.bHP+bHP, map.oPlayer.bMaxHP);
- this.erase();
- map.lstEntities.Remove(this);
- hud.drawHP();
- }
- public potion(Byte x, Byte y, Byte hp): base(x, y, '\x03', ConsoleColor.Red)
- {
- bHP = hp;
- }
- public potion(Byte hp): base('\x03', ConsoleColor.Red)
- {
- bHP = hp;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- map.generate(80, 25);
- map.oPlayer = new living(5, 5, '\x02', ConsoleColor.Green, 5, 50);
- for(Byte i = 0; i < 20; ++i)
- {
- new potion(10);
- }
- hud.drawAll();
- while (true)
- {
- if (Console.KeyAvailable)
- {
- ConsoleKey kk = Console.ReadKey(true).Key;
- switch (kk)
- {
- case ConsoleKey.Escape:
- return;
- case ConsoleKey.W:
- map.oPlayer.move(0, -1);
- break;
- case ConsoleKey.S:
- map.oPlayer.move(0, 1);
- break;
- case ConsoleKey.A:
- map.oPlayer.move(-1, 0);
- break;
- case ConsoleKey.D:
- map.oPlayer.move(1, 0);
- break;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement