c언어 포트리스 질문이요
미쁘다
포트리스 게임 소스입니다.
충돌검사 함수에서 막혀있는데
고수분들 조언좀 부탁드립니다#include stdio.h
#include conio.h
#include Windows.h
#define LEFT 75
#define RIGHT 77
#define UP 72
#define C 99
#define Z 122
#define M 109
#define Space 32
/* 게임 판의 크기 */
#define GBOARD_WIDTH 100
#define GBOARD_HEIGHT 50
/* 게임 판을 그릴 기준이 되는 위치 */
#define GBOARD_ORIGIN_X 0
#define GBOARD_ORIGIN_Y 30
int curPosX, curPosY;
int block_id;
int block_base;
static int gameBoardInfo[GBOARD_HEIGHT+1][GBOARD_WIDTH+2];
typedef struct point
{
int x;
int y;
} point;
////////////////////////////////////////////////////////////////////////char blockModel[][4][4] =
{
{
{1, 1, 1, 0},
{0, 1, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0} },
{
{0, 1, 1, 1},
{0, 0, 1, 0},
{1, 1, 1, 1},
{0, 0, 0, 0} },
{
{0, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0} },
};
////////////////////////////////////////////////////////////////////////
void SetCurrentCursorPos(float x, float y)
{
COORD pos={x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
//////////////////////////////////////////////////////////////////////
point GetCurrentCursorPos(void)
{
point curPoint;
CONSOLE_SCREEN_BUFFER_INFO curInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
curPoint.x=curInfo.dwCursorPosition.X;
curPoint.y=curInfo.dwCursorPosition.Y;
return curPoint;
}
void RemoveCursor(void)
{
CONSOLE_CURSOR_INFO curInfo;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&curInfo);
curInfo.bVisible=0;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&curInfo);
}
////////////////////////////////////////////////////////////////////////
void DrawGameBoard()
{
int x, y;
SetCurrentCursorPos(GBOARD_ORIGIN_X, GBOARD_ORIGIN_Y);
for(x=GBOARD_ORIGIN_X; x=GBOARD_WIDTH; x++)
{
for(y=GBOARD_ORIGIN_Y; y=GBOARD_HEIGHT; y++)
{
printf(◎);
}
}
for(y=GBOARD_ORIGIN_Y; yGBOARD_HEIGHT; y++)
{
for(x=GBOARD_ORIGIN_X; xGBOARD_WIDTH; x++)
{
gameBoardInfo[y][x]=1;
}
}
}
////////////////////////////////////////////////////////////////////////
void DisplayTank(char blockInfo[][4])
{
int y, x;
point curPos=GetCurrentCursorPos();
for(y=0; y4; y++)
{
for(x=0; x4; x++)
{
SetCurrentCursorPos(curPos.x+x*2, curPos.y+y);
if(blockInfo[y][x] == 1)
printf(〓);
}
}
SetCurrentCursorPos(curPos.x, curPos.y);
}
/////////////////////////////////////////////////////////////////////////
void DisplayGun(char blockInfo[][4])
{
int y, x;
point curPos=GetCurrentCursorPos();
for(y=0; y4; y++)
{
for(x=0; x4; x++)
{
SetCurrentCursorPos(curPos.x+x, curPos.y+y);
if(blockInfo[y][x] == 1)
printf(o);
}
}
SetCurrentCursorPos(curPos.x, curPos.y);
}
////////////////////////////////////////////////////////////////////////
void DeleteTank(char blockInfo[][4])
{
int y, x;
point curPos=GetCurrentCursorPos();
for(y=0; y4; y++)
{
for(x=0; x4; x++)
{
SetCurrentCursorPos(curPos.x+x, curPos.y+y);
if(blockInfo[y][x] == 1)
printf( );
}
}
SetCurrentCursorPos(curPos.x, curPos.y);
}
////////////////////////////////////////////////////////////////////////
int DetectCollision(int posX, int posY, char blockModel[4][4])
{
int x, y;
/* gameBoardInfo 배열의 좌표로 변경 */
int arrX= (posX-GBOARD_ORIGIN_X+8)/2;
int arrY= posY-(G posY-(GBOARD_ORIGIN_Y-4);
/* 충돌 검사 */
for(x=0; x4; x++)
{
for(y=0; y4; y++)
{
/* Short Circuit Evaluation에 의해 배열 일부만 검사 */
if(blockModel[y][x]==1 && gameBoardInfo[arrY+y][arrX+x]==1)
return 0;
}
}
return 1;
}
////////////////////////////////////////////////////////////////////////
void ONERight()
{
DeleteTank(blockModel[1]);
curPosX +=1;
SetCurrentCursorPos(curPosX, curPosY);
DisplayTank(blockModel[1]);
}
////////////////////////////////////////////////////////////////////////
void ONELeft()
{
if(!DetectCollision(curPosX-2, curPosY, blockModel[block_id])){
return;
}
DeleteTank(blockModel[1]);
curPosX -=1;
SetCurrentCursorPos(curPosX, curPosY);
DisplayTank(blockModel[1]);
}
////////////////////////////////////////////////////////////////////////
void ShootThirty()
{
if(DetectCollision(curPosX, curPosY, blockModel[2])){
return ;
}
for(curPosX=GBOARD_ORIGIN_X+8; curPosX=GBOARD_WIDTH; curPosX++){
curPosY = (14*(curPosX-28)*(curPosX-28))/(20*20)+14;
Sleep(100);
DeleteTank(blockModel[2]);
SetCurrentCursorPos(curPosX, curPosY);
DisplayGun(blockModel[2]);
}
}
////////////////////////////////////////////////////////////////////////
void ShootSixty()
{
if(!DetectCollision(curPosX, curPosY, blockModel[2])){
return;
}
for(curPosX=GBOARD_ORIGIN_X+8; curPosX=GBOARD_WIDTH; curPosX++){
curPosY = (28*(curPosX-20)*(curPosX-20))/144;
Sleep(100);
DeleteTank(blockModel[2]);
SetCurrentCursorPos(curPosX, curPosY);
DisplayGun(blockModel[2]);
}
}
////////////////////////////////////////////////////////////////////////
void RedrawBlocks(void)
{
int x, y;
int cursX, cursY;
for(y=0; yGBOARD_HEIGHT; y++)
{
for(x=1; xGBOARD_WIDTH+1; x++)
{
cursX= x*2 + GBOARD_ORIGIN_X;
cursY= y + GBOARD_ORIGIN_Y;
SetCurrentCursorPos(cursX, cursY);
if(gameBoardInfo[y][x]==1)
{
printf(■);
}
else
{
printf( );
}
}
}
}
////////////////////////////////////////////////////////////////////////
void ProcessKeyInput(curPosX)
{
int i, key, speed;
speed = 100;
for(i=0; i100; i++)
{
if(_kbhit() != 0)
{
key=_getch();
switch(key)
{
case LEFT:
ONELeft();
break;
case RIGHT:
ONERight();
break;
case C:
ShootThirty();
break;
case Z:
ShootSixty();
break;
}
}
Sleep(speed);
}
}
/////////////////////////////////////////////////////////////////////////////////
void RemoveMap()
{
int x,y,line;
for(y=GBOARD_HEIGHT-1; y0; y--)
{
for(x=1; xGBOARD_WIDTH+1; x++)
{
if(gameBoardInfo[y][x]!=1)
break;
}
if(x==(GBOARD_WIDTH+1))
{
for(line=0; y-line0; line++)
{
memcpy(&gameBoardInfo[y-line][1],&gameBoardInfo[(y-line)-1][1],
GBOARD_WIDTH * sizeof(int)
);
}
y++;
}
}
RedrawBlocks();
}
/////////////////////////////////////////////////////////////////////////////////
int main(void)
{
while(1)
{
RemoveCursor();
DrawGameBoard();
SetCurrentCursorPos(GBOARD_ORIGIN_X, GBOARD_ORIGIN_Y-4);
DisplayTank(blockModel[0]);
ProcessKeyInput();
}
getchar();
return 0;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2704497 | int double char 같은것좀 좀만 더 가르쳐 주세요 (6) | 로지 | 2025-08-10 |
2704473 | 구조체 배열 초기화 질문 | 꽃은별 | 2025-08-10 |
2704445 | 배열과 조건문에 관한질문 (3) | 찬솔큰 | 2025-08-10 |
2704417 | 이진 트리 깊이 값 구하는것 질문입니다. | 푸른잎 | 2025-08-10 |
2704391 | 자료 유형에 관련된 문제입니다. (2) | 늘솜 | 2025-08-09 |
2704365 | c언어 문제안되서물어봅니다 (3) | 맑은가람 | 2025-08-09 |
2704338 | 다항식의 뺄셈에 대해 질문드립니다... (1) | 여자 | 2025-08-09 |
2704311 | 센서를 이용한 라인 주행인데 명령어좀 알려주세요 ㅠ_ㅠ (3) | 초록이 | 2025-08-09 |
2704284 | 배열for문;; (3) | 은솜 | 2025-08-08 |
2704255 | readline(int fd, char *buf, int nbytes)함수를 구현하는법좀..ㅠㅠ | 파라나 | 2025-08-08 |
2704196 | 간단한 c언어좀 만들어주세요 (2) | 슬S2아 | 2025-08-08 |
2704118 | 성적에서 등수 구하기 (4) | 딥레드 | 2025-08-07 |
2704062 | 알고리즘 알려주세요 | 나리 | 2025-08-06 |
2704035 | 답은 나오는데 과정에서 `` 약간 이상합니다.ㅎㅎ 답만나와버려요 | 핫블랙 | 2025-08-06 |
2703979 | 문자열 EEPROM 작성 방법 문의드립니다. | 그녀는귀여웠다 | 2025-08-06 |
2703954 | 키보드를 입력하면 캐릭터를 움직이기 질문 (7) | 좋아해 | 2025-08-05 |
2703924 | 계속 에러가... (4) | 눈꽃 | 2025-08-05 |
2703897 | 배열의 최댓값과 최솟값 차이 구하기 (1) | 하늬 | 2025-08-05 |
2703869 | C언어 질문입니다 급해용!!! ㅠㅠ (2) | 덕이 | 2025-08-05 |
2703814 | C로 프로그램을 만들때 도스창 말고 다르게 만드는방법이 있을까요? (2) | 소심한여자 | 2025-08-04 |