Improve rendering and gameplay

This commit is contained in:
Marvin Rohrbach
2018-05-26 09:12:54 +02:00
parent ce9e703c6e
commit 7712aa4a0f

View File

@@ -1,11 +1,14 @@
using CoopSweeper.GameTypes;
using System;
using System.Text;
using System.Threading;
namespace CoopSweeper
{
class Program
{
const int MAP_POS_X = 3;
const int MAP_POS_Y = 3;
static void Main(string[] args)
{
@@ -14,12 +17,26 @@ namespace CoopSweeper
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
var game = new Game();
game.GenerateGame(15, 10, 15);
var cursorPosX = 0;
var cursorPosY = 0;
StartNewGame(game, cursorPosX, cursorPosY);
game.GameFinished += won =>
{
Console.SetCursorPosition(0, 0);
if (won)
Console.Write("You won!");
else
Console.Write("You lost!");
DrawMap(MAP_POS_X, MAP_POS_Y, game.Map, cursorPosX, cursorPosY);
Console.ReadKey();
StartNewGame(game, cursorPosX, cursorPosY);
};
ConsoleKey key = (ConsoleKey)(-1);
do
{
var oldCursorPosX = cursorPosX;
var oldCursorPosY = cursorPosY;
bool fullRedraw = false;
switch(key)
{
case ConsoleKey.UpArrow:
@@ -31,16 +48,27 @@ namespace CoopSweeper
case ConsoleKey.RightArrow:
cursorPosX++; break;
case ConsoleKey.Spacebar:
fullRedraw = true;
game.Reveal(cursorPosX, cursorPosY); break;
case ConsoleKey.F:
fullRedraw = true;
game.ToggleMark(cursorPosX, cursorPosY); break;
}
if (cursorPosX < 0) cursorPosX = 0;
if (cursorPosY < 0) cursorPosY = 0;
if (cursorPosX >= game.Map.GetLength(0)) cursorPosX = game.Map.GetLength(0) - 1;
if (cursorPosY >= game.Map.GetLength(1)) cursorPosY = game.Map.GetLength(1) - 1;
DrawBorder(2, 2, game.Map.GetLength(0) + 2, game.Map.GetLength(1) + 2);
if (fullRedraw)
DrawMap(3, 3, game.Map, cursorPosX, cursorPosY);
else if(oldCursorPosX != cursorPosX || oldCursorPosY != cursorPosY)
{
Console.SetCursorPosition(oldCursorPosX + 3, oldCursorPosY + 3);
DrawChar(game.Map[oldCursorPosX, oldCursorPosY], false);
Console.SetCursorPosition(cursorPosX + 3, cursorPosY + 3);
DrawChar(game.Map[cursorPosX, cursorPosY], true);
}
Console.SetCursorPosition(0, 0);
Console.CursorVisible = false;
} while ((key = Console.ReadKey().Key) != ConsoleKey.Escape);
}
@@ -57,11 +85,16 @@ namespace CoopSweeper
c = ' ';
break;
case DisplayState.NONE:
bgChanged = true;
fgChanged = true;
Console.ForegroundColor = ConsoleColor.Gray;
c = '◌';
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.Gray;
//c = '◌';
c = ' ';
break;
case DisplayState.QUESTIONMARK:
bgChanged = true;
Console.BackgroundColor = ConsoleColor.Gray;
c = '?';
break;
case DisplayState.BOMB:
@@ -74,7 +107,9 @@ namespace CoopSweeper
break;
case DisplayState.FLAG:
fgChanged = true;
Console.ForegroundColor = ConsoleColor.Cyan;
bgChanged = true;
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.DarkRed;
c = 'F';
break;
case DisplayState.NUMBER1:
@@ -86,7 +121,24 @@ namespace CoopSweeper
case DisplayState.NUMBER7:
case DisplayState.NUMBER8:
fgChanged = true;
switch(state)
{
case DisplayState.NUMBER1:
Console.ForegroundColor = ConsoleColor.Green;
break;
case DisplayState.NUMBER2:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case DisplayState.NUMBER3:
Console.ForegroundColor = ConsoleColor.DarkYellow;
break;
case DisplayState.NUMBER4:
Console.ForegroundColor = ConsoleColor.DarkRed;
break;
default:
Console.ForegroundColor = ConsoleColor.Red;
break;
}
c = ((int)state).ToString()[0];
break;
}
@@ -105,6 +157,15 @@ namespace CoopSweeper
Console.BackgroundColor = oldBg;
}
public static void StartNewGame(Game game, int cursorPosX, int cursorPosY)
{
game.GenerateGame(100, 30, 15);
Console.Clear();
Console.SetCursorPosition(0, 0);
DrawBorder(MAP_POS_X - 1, MAP_POS_X - 1, game.Map.GetLength(0) + 2, game.Map.GetLength(1) + 2);
DrawMap(MAP_POS_X, MAP_POS_Y, game.Map, cursorPosX, cursorPosY);
}
private static void DrawMap(int posX, int posY, IField[,] map, int cursorX, int cursorY)
{
var res = new string[map.GetLength(1)];