namespace BrickGame5
{
public class BLOCKDATA
{
public int nLife; //벽돌을 표시하는 변수
public int nX, nY; //벽돌x, y좌표
}
}
namespace BrickGame5
{
public class Block
{
//벽돌이 여러개
BLOCKDATA[] tBlock = new BLOCKDATA[50];
public Block() //생성자
{
//벽돌초기화
for(int i = 0; i <tBlock.Length; i++)
{
tBlock[i] = new BLOCKDATA();
tBlock[i].nLife = 0;
tBlock[i].nX = 0;
tBlock[i].nY = 0;
}
}
//Search함수 주어진 좌표가 현재 설정된 블록 배열 중에 중복된 것이 있는지를
//검색해주는 함수이다.
//현재 최대 블록 50개 설정가능
//매번 50개의 블록을 전부 검색할 필요없이 스테이지마다 할당된 블록
//안에서만 검색
//Search함수는 첫번째 인자의 nEnd변수는 블로그이 범위가 된다.
//이 함수는 SetBlock함수에서 호출이 된다.
public int Search(int nEnd, int nX, int nY)
{
for(int i = 0; i < nEnd; i++)
{
if (tBlock[i].nY == nY) //Y좌표가 같고
{
if (tBlock[i].nX == nX || (tBlock[i].nX + 1) == 1) //특수문자여서
return 1; //같으면 1을 반환
}
}
return 0; //같지 않으면 0을 반환
}
//범위 안에서 블록의 중복 검사를 하는 이유
public void SetBlock(int nBlockCount)
{
int nX, nY;
Random r = new Random();
for(int i = 0; i<nBlockCount; i++)
{
tBlock[i].nLife = 1;
while(true) //중복찾기
{
nX = r.Next(2, 66); //2~65범위 안에서
nY = r.Next(2, 16); //2~15범위 안에서
if(Search(i, nX, nY) == 0)
{
//중복이 아님
tBlock[i].nX = nX;
tBlock[i].nY = nY;
break; //한개를 만들고 for문 탈출
}
}
}
}
//필요 함수들
public void Initialize()
{
SetBlock(20);
}
public void Progress()
{
}
public void Render()
{
for(int i = 0; i < 20; i++)
{
Console.SetCursorPosition(tBlock[i].nX, tBlock[i].nY);
Console.Write("■");
}
}
public void Release()
{
}
}
}
namespace BrickGame5
{
public class Program
{
static void Main(string[] args)
{
Block block = new Block();
block.Initialize();
block.Progress();
block.Render();
block.Release();
}
}
}
728x90
'프로그래밍 > 콘솔프로젝트' 카테고리의 다른 글
BrickGame <최종> (0) | 2023.07.13 |
---|---|
공 튕기기 (0) | 2023.07.12 |
Ball Bar로 잡기 (0) | 2023.07.11 |
공의 이동 (0) | 2023.07.11 |
공 벽 튕기기 (0) | 2023.07.11 |