예제 코드
using System;
using System.Diagnostics;
using System.Reflection;
using static System.Console;
namespace EatStarsGame
{
class Program
{
#region
//원하는 좌표에 문자열 출력
static void Print(int x, int y, string shape)
{
SetCursorPosition(x,y);
Write(shape);
SetCursorPosition(x, y+1);
Write(shape);
}
static void Print(int x, int y, string shape1, string shape2)
{
SetCursorPosition(x, y);
Write(shape1);
SetCursorPosition(x, y + 1);
Write(shape2);
}
//플레이어 입력
static void Input(ref int Player_x, ref int Player_y)
{
const int EDGE_X = 1,EDGE_Y = 1;
ConsoleKey key = ReadKey(true).Key;
switch (key)
{
case ConsoleKey.UpArrow:
if (EDGE_Y != Player_y) Player_y--;
break;
case ConsoleKey.DownArrow:
Player_y++;
break;
case ConsoleKey.LeftArrow:
if (EDGE_X != Player_x) Player_x-=2;
break;
case ConsoleKey.RightArrow:
Player_x+=2;
break;
}
}
#endregion
static void Main(string[] args)
{
#region 선언 및 초기화
//선언부
CursorVisible = false;
Random r = new Random();
int Player_x = 1, Player_y = 1;
int star1_x = 0, star1_y = 0;
int star2_x = 0, star2_y = 0;
int star3_x = 0, star3_y = 0;
bool star1_b = true, star2_b = true, star3_b = true;
//const string PLAYER_SHAPE = "★", STAR_SHAPE = "☆";
const string PLAYER_SHAPE1 = "■■", STAR_SHAPE1 = "□□";
const int EDGE_X = 1, EDGE_Y = 1;
//초기화
star1_x = r.Next(1, 50);
if (star1_x % 2 == 0) star1_x++;
star2_x = r.Next(1, 50);
if (star2_x % 2 == 0) star2_x++;
star3_x = r.Next(1, 50);
if (star3_x % 2 == 0) star3_x++;
star1_y = r.Next(1, 20);
star2_y = r.Next(1, 20);
star3_y = r.Next(1, 20);
while (true)
{
//출력
//printe(x,y,shape = PLAYER_SHAPE)
//별
Clear();
if (star1_b)
{
//SetCursorPosition(star1_x, star1_y);
//Write(STAR_SHAPE);
//Print(star1_x, star1_y, STAR_SHAPE);
Print(star1_x, star1_y, STAR_SHAPE1, STAR_SHAPE1);
}
if (star2_b)
{
//SetCursorPosition(star2_x, star1_y);
//Write(STAR_SHAPE);
//Print(star2_x, star2_y, STAR_SHAPE);
Print(star2_x, star2_y, STAR_SHAPE1, STAR_SHAPE1);
}
if (star3_b)
{
//Write(STAR_SHAPE);
//Print(star3_x, star3_y, STAR_SHAPE);
Print(star3_x, star3_y, STAR_SHAPE1, STAR_SHAPE1);
}
//플레이어
//Print(Player_x, Player_y,PLAYER_SHARE);
Print(Player_x, Player_y,PLAYER_SHAPE1,PLAYER_SHAPE1);
//키보드입력
Input(ref Player_x, ref Player_y);
//결과처리
//if(Player_x == star1_x && Player_y == star1_y)
//{
// star1_b = false;
//}
//if (Player_x == star2_x && Player_y == star2_y)
//{
// star2_b = false;
//}
//if (Player_x == star3_x && Player_y == star3_y)
//{
// star3_b = false;
//}
if ((Player_x >= star1_x - 2 && Player_x <= star1_x + 2) &&
(Player_y >= star1_y - 1 && Player_y <= star1_y + 1))
{
star1_b = false;
}
if ((Player_x >= star2_x - 2 && Player_x <= star2_x + 2) &&
(Player_y >= star2_y - 1 && Player_y <= star2_y + 1))
{
star2_b = false;
}
if ((Player_x >= star3_x - 2 && Player_x <= star3_x + 2) &&
(Player_y >= star3_y - 1 && Player_y <= star3_y + 1))
{
star3_b = false;
}
//클리어 조건
if (!star1_b && !star2_b && !star3_b)
{
Clear();
SetCursorPosition(5, 2);
Write("게임 클리어");
return;
}
}
}
}
}
결과
// Initial State
// ■■
//
// □□
// □□
//
// □□
// □□
//
// □□
// □□
//
// Player moves to the right
//
// ■■
//
// □□
// □□
//
// □□
// □□
//
// □□
// □□
// Player collects the star at (7,3)
//
// ■■
//
//
//
// □□
// □□
//
// □□
// □□
// 게임 클리어