✅콘솔 텍스트게임 제작하기
namespace project_s
{
internal class Program
{
struct Position
{
public int x;
public int y;
}
static void Main(string[] args)
{
bool gameOver = false;
Position playerPos;
char[,] map;
Start();
while (gameOver == false)
{
Render();
ConsoleKey key = Input();
Update();
}
End();
}
static void Start()
{
}
static void Render()
{
}
static void PrintPlayer()
{
}
static ConsoleKey Input()
{
return Console.ReadKey(true).Key;
}
static void Update()
{
}
static void Move()
{
}
static bool IsClear()
{
}
static void End()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleProject
{
class Program
{
struct Position
{
public int x;
public int y;
}
static void Main(string[] args)
{
bool gameOver = false;
Position playerPos;
playerPos.x = 0;
playerPos.y = 0;
Position goalPos;
bool[,] map;
Start(ref playerPos, out goalPos, out map);
while (gameOver == false)
{
Render(playerPos, goalPos, map);
ConsoleKey key = Input();
Update(key, ref playerPos, goalPos, map, ref gameOver);
}
End();
}
static void Start(ref Position playerPos, out Position goalPos, out bool[,] map)
{
Console.CursorVisible = false;
playerPos.x = 1;
playerPos.y = 1;
goalPos.x = 13;
goalPos.y = 8;
map = new bool[10, 15]
{
{false,false,false,false,false,false,false,false,false,false,false,false,false,false, false},
{false, true, true,false, true, true, true, true, true, true, true, true, true, true, false},
{false, true, true,false, true, true, true, true, true, true, true, true, true, true, false},
{false, true, true,false, true, true, true, true, true, true, true, true, true, true, false},
{false, true, true,false, true, true, true, true, true, true, true, true, true, true, false},
{false, true, true, true, true, true, true, true, true, true, true, true, true, true, false},
{false, true, true, true, true, true, true, true, true, true, true, true, true, true, false},
{false,false,false,false, true, true, true, true, true, true, true, true, true, true, false},
{true, true, true, false, true, true, true, true, true, true, true, true, true, true, false},
{true, true, true, false,false,false,false,false,false,false,false,false,false,false, false},
};
ShowTitle();
}
static void ShowTitle()
{
Console.WriteLine("----------------");
Console.WriteLine(" 레전드 미로찾기 ");
Console.WriteLine("----------------");
Console.WriteLine();
Console.WriteLine("아무키나 눌러서 시작하세요....");
Console.ReadKey(true);
Console.Clear();
}
static void Render(Position playerPos, Position goalPos, bool[,] map)
{
Console.SetCursorPosition(0, 0);
PrintMap(map);
PrintPlayer(playerPos);
PrintGoal(goalPos);
}
static void PrintMap(bool[,] map)
{
for (int y = 0; y < map.GetLength(0); y++)
{
for (int x = 0; x < map.GetLength(1); x++)
{
if (map[y, x] == false)
{
Console.Write('■');
}
else
{
Console.Write(' ');
}
}
Console.WriteLine();
}
}
static void PrintPlayer(Position playerPos)
{
Console.SetCursorPosition(playerPos.x, playerPos.y);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("★");
Console.ResetColor();
}
static void PrintGoal(Position goalPos)
{
Console.SetCursorPosition(goalPos.x, goalPos.y);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("G");
Console.ResetColor();
}
static ConsoleKey Input()
{
ConsoleKey input = Console.ReadKey(true).Key;
return input;
}
static void Update(ConsoleKey key, ref Position plyerPos, Position goalPos, bool[,] map, ref bool gameOver)
{
Move(key, ref plyerPos, map);
bool isClear = CheckGameClear(plyerPos, goalPos);
if (isClear)
{
gameOver = true;
}
}
static void Move(ConsoleKey key, ref Position plyerPos, bool[,] map)
{
switch (key)
{
case ConsoleKey.A:
case ConsoleKey.LeftArrow:
if (map[plyerPos.y, plyerPos.x - 1] == true)
{
plyerPos.x--;
}
break;
case ConsoleKey.D:
case ConsoleKey.RightArrow:
if (map[plyerPos.y, plyerPos.x + 1] == true)
{
plyerPos.x++;
}
break;
case ConsoleKey.W:
case ConsoleKey.UpArrow:
if (map[plyerPos.y - 1, plyerPos.x] == true)
{
plyerPos.y--;
}
break;
case ConsoleKey.S:
case ConsoleKey.DownArrow:
if (map[plyerPos.y + 1, plyerPos.x] == true)
{
plyerPos.y++;
}
break;
}
}
static bool CheckGameClear(Position plyerPos, Position goalPos)
{
bool success = (plyerPos.x == goalPos.x) && (plyerPos.y == goalPos.y);
return success;
}
static void End()
{
Console.Clear();
Console.WriteLine("축하합니다!! 미로 찾기에 성공하셨습니다!");
}
}
}
✅게임 실행파일 -> 프로젝트 폴더에서 bin 폴더 안에 게임 실행파일(.exe) 파일이 실행파일