#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define SPACEC 32
int x, y;
int map[100][100] = {0};
int color = 11; // 출력 색상
int playerType = 1; //A 인지 *인지
int a=80,r,t;
void gotoXY(int x, int y)
{
COORD Pos;
Pos.X = x;
Pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
void init()
{
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
for(int i=0; i<20; i++) {
for(int j=0; j<40; j++) {
if(i==0 || j==0 || i==19 ||j==39) {
map[i][j] = 9;
printf("~");
}
else {
printf(" ");
}
}
printf("\n");
}
item();
gotoXY(50,4);
printf("1~9의 수를 누르면 플레이어의 색이 바뀝니다");
gotoXY(50,2);
printf("%HP:");
gotoXY(53,2);
printf("%d",a);
gotoXY(50,3);
printf("asdw는 A를, 방향키는 *을 조종합니다");
gotoXY(x, y);
printf("*");
}
void item(){ //아이템의 랜덤 위치 지정
srand(time(NULL));
r = rand()%38 + 1;
t = rand()%18 + 1;
gotoXY(r,t);
printf("?");
}
int main()
{
char c;
x = 14;
y = 7;
init();
while(1)
{
if (kbhit()) //키보드 입력 확인 (true / false)
{
c = _getch();
if(49<=c && c<=57){
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), c-48 );
color=c-48;
}
switch(c){
case 75: //LEFT
if(x==1) {
continue;
}
playerType=1;
a--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
x--;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==1 ? '*' : 'A');
break;
case 77: // RIGHT
if(x==38) {
continue;
}
playerType=1;
a--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
x++;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==1 ? '*' : 'A');
break;
case 72: // UP
if(y==1) {
continue;
}
playerType=1;
a--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
y--;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==1 ? '*' : 'A');
break;
case 80: // DOWN
if(y==18) {
continue;
}
playerType=1;
a--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
y++;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==1 ? '*' : 'A');
break;
case 97:
if(x==1) {
continue;
}
playerType=1;
a--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
x--;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==2 ? '*' : 'A');
break;
case 100:
if(x==38) {
continue;
}
playerType=1;
a--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
x++;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==2 ? '*' : 'A');
break;
case 119:
if(y==1) {
continue;
}
playerType=1;
a--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
y--;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==2 ? '*' : 'A');
break;
case 115:
if(y==18) {
continue;
}
playerType=1;
a--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
y++;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==2 ? '*' : 'A');
break;
}
//바뀐 HP를 다시 출력
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
gotoXY(50,2);
printf("%HP:");
gotoXY(53,2);
printf("%3d",a);
//플레이어가 아이템위치에 왔다면 (1. hp감소?증가? 2. 새로운 아이템 생성)
if(map[x][y]==map[r][t]){
item();
for(int i=0;i<=20;i++)
{
a++;
}
}
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
gotoXY(50,2);
printf("%HP:");
gotoXY(53,2);
printf("%3d",a);
//다시 원래 색상으로 변경
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color );
gotoXY(92,4);
if(a<0)
{
system("cls");
gotoXY(30,10);
printf("Game Over");
Sleep(4000);
return 0;
}
if(a>200)
{
}
}
}
}