#include <stdio.h>
#include <conio.h>
#include <windows.h>
void clrscr(void);
void gotoxy(int x, int y);
#define ScreenWidth 80
#define ScreenHeight 24
int Block_x[4][4] = {
{ -1, 0, 0, 1 },
{ -1, 0, 0, 0 },
{ -1, 0, 0, 1 },
{ 0, 0, 0, 1 }
};
int Block_y[4][4] = {
{ 0, 0, -1, 0 },
{ 0, 0, 1, -1 },
{ 0, 0, 1, 0 },
{ -1, 0, 1, 0 }
};
void DrawBoundary();
void DrawBlock(int x, int y, int dir, char c);
int main()
{
int x = ScreenWidth / 4, y = ScreenHeight / 4;
int dir = 0;
int oldx = -1, oldy = -1;
int oldDir = -1;
char key = 0;
DrawBoundary();
while (key != 27){ // ESC
if (oldDir >= 0)
DrawBlock(oldx, oldy, oldDir, ' ');
DrawBlock(x, y, dir, 'O');
key = _getch();
oldDir = dir;
oldx = x;
oldy = y;
switch (key){
case 'u':
dir = (dir + 1) % 4;
break;
case 'o':
dir = (dir + 3) % 4;
break;
case 'i':
if (y > 3)
y--;
break;
case 'k':
if (y < ScreenHeight - 2)
y++;
break;
case 'j':
if (x > 3)
x--;
break;
case 'l':
if (x < ScreenWidth - 2)
x++;
break;
}
}
return 0;
}
void DrawBoundary()
{
int i = 0;
system("cls");
gotoxy(1, 1);
for (i = 0; i < ScreenWidth; i++)
putchar('#');
for (i = 1; i <= ScreenHeight; i++){
gotoxy(1, i);
putchar('#');
gotoxy(ScreenWidth, i);
putchar('#');
}
gotoxy(1, ScreenHeight);
for (i = 0; i < ScreenWidth; i++)
putchar('#');
gotoxy(2, ScreenHeight);
printf(" Move : 'i', 'j', 'k' and 'l' keys. Rotate : 'u' and 'o'. ESC : finish. ");
}
void DrawBlock(int x, int y, int dir, char c)
{
int i = 0;
for (i = 0; i < 4; i++){
gotoxy(x - Block_x[dir][i], y - Block_y[dir][i]);
putchar(c);
}
}
void gotoxy(int x, int y) // move cursor to (x, y)
{
COORD Pos = { x - 1, y - 1 };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
해당 소스는 교수님께 첨부받은 테트리스.c 파일중 일부 발췌했습니다.
완벽히 테트리스 기능이 아닌 그냥 기본적인 움직임만 컨트롤 하는 소스입니다.
왠만한 코드분석은 끝낸상태에서 궁금한 점이 생겨 염치불구하고 글 써 봅니다.
"ㅗ"이런모양의 블럭을 키보드 i j k l 입력으로 상 하 좌 우로 움직이며 u o 입력으로 방향전환이 됩니다.
이 소스를 살짝 응용하여 프로젝트 과제를 수행도중 아무리 찍어도 안나와서... ㅠㅠ
제작하려는 프로그램 중 한 기능인데
"ㅗ" 이모양과 "ㅜ" 이 모양으로 선택하고 enter키를 눌렀을때
ㅗ ㅜ 모양과 일치 한지 안한지 검사가 필요합니다.
디버깅으로 아무리 찍어봐도 0과 1 -1로 구성댄 배열의 숫자변화가 없어서
어떻게 ㅗ ㅜ 를 표현하는지가 궁금합니다.
제가 하고자하는 조건검사는
Block_ㅗ 배열에는 ㅗ라고 정렬된 0,1,-1
Block_ㅜ 배열에는 ㅜ라고 정의된 0,1,-1이 있으며
Block_x 와 Block_ㅗ 가 같은지에 대한 조건검사가 필요합니다.
디버깅할때
○
○○○ 이렇게 순차적으로 그려줄줄 알았는데
○ ○
○○ -> ○○ -> ○○○ 이런식으로 그림을 그리더라고요..
도움! 한번 요청해보겠습니다 ㅠ