#include <stdio.h>
#include <windows.h>
#include <Windows.h>
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
int x=5, y=5,i,j,x1=0,y1=0,dx,dy,points=0,l=4,level=1;
void textcolor(int colorNum) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colorNum);
}
void gotoxy(int x, int y)
{
COORD Pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
void map()
{
gotoxy(0,0);
for(i=1; i<=49; i++)
{
for(j=1; j<=49; j++)
{
if(i!=0||i!=49||j!=0||j!=49)
{
printf(" ");
}
}
printf("\n");
}
}
void place(a,b)
{
gotoxy(a,b);
textcolor(10);
printf("0");
textcolor(15);
x1=a;
y1=b;
}
void placetrap(a,b)
{
for(i=1;i<=l;i++){
gotoxy(a,b);
textcolor(2);
printf("0");
textcolor(15);
dx=a;
dy=b;
}
}
void setup()
{
gotoxy(0,0);
for(i=1; i<=50; i++)
{
for(j=1; j<=50; j++)
{
if(i==0||i==50||j==0||j==50)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
// 1. 매크로로 좌우상하를 설정한다.
int main(void)
{
srand(time(NULL));
char c;
int size=1;
setup();
gotoxy(0,0);
for(i=1; i<=50; i++)
{
for(j=1; j<=50; j++)
{
if(i==0||i==50||j==0||j==50)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
gotoxy(10,10);
while (1)
{
if (_kbhit()) // 2. while문안에서 키보드 눌렸을 시 if문이 실행된다.
{
c = _getch(); // 3. 방향키가 입력됬을 때 224 00 이 버퍼에 있다. 224부터 빼준다.
if (c == -32) // 4. -32로 입력되면
{
c = _getch();// 5. 뒤의 값 00을 판별하여 좌우상하 출력
gotoxy(x,y);
printf(" ");
switch (c)
{
case LEFT:
if(x>0)
{
x=x-size;
}
break;
case RIGHT:
if(x<48)
{
x=x+size;
}
break;
case UP:
if(y>0)
{
y=y-size;
}
break;
case DOWN:
if(y<48)
{
y=y+size;
}
break;
}
gotoxy(x,y);
printf("#");
if(x==x1&&y==y1){
place(rand()%48,rand()%48);
points++;
gotoxy(dx,dy);
printf(" ");
placetrap(rand()%48,rand()%48);
}
else if(points==level*10){
system("cls");
gotoxy(10,10);
printf("Level complete!");
printf("\n loading level %d...",level+1);
Sleep(4000);
gotoxy(10,11);
printf("loading complete");
Sleep(1000);
system("cls");
setup();
}
else if(x==dx&&y==dy){
l=l-1;
if(l<2){
system("cls");
gotoxy(10,10);
printf("Game over!\npoints : %d",points);
break;
}
}
else{
placetrap(dx,dy);
place(x1,y1);
}
gotoxy(52,30);
printf("x : %d y :%d",x,y);
gotoxy(52,31);
printf("points : %d",points);
gotoxy(52,32);
for(i=1;i<=l;i++){
printf(" ");
}
gotoxy(52,32);
for(i=1;i<=l-1;i++){
textcolor(12);
printf("♥");
textcolor(15);
}
}
}
}
//♡♥
Sleep(1000);
return 0;
}