/***
*****게임 기능 정리**********
1. 상하좌우 키보드로 플레이어 *을 움직인다
2. 아이템@를 획득하면 체력이 10 상승한다.
3. 함정 !를 건들이면 체력이 10 깎인다.
4. 체력이 0이 될 경우 게임을 끝낸다.
5. Q를 누르면 종료창을 띄운다. (a=종료, b=취소)
6. 30*30밖으로는 못나가도록한다, 맵의 경계선 표시 (숙제)
***/
#include <stdio.h>
#include <conio.h>
#include <windows.h>
// 상하좌우 상수값 설정
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
int hp=10;
void gotoxy(int x, int y)
{
COORD Pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
void line()
{
for (int x=0; x<31; x++){
for (int y=0; y<30; y++){
if (x==0||y==0||x==30||y==29){
gotoxy(x, y);
printf (".");
}
}
}
}
void init() //초기상태
{
gotoxy(50,2);
printf("체력 : %d",hp);
}
int main(void) {
char c;
srand(time(NULL)); //랜덤 시드 설정 (최초 한 번만!)
int random = rand()%29+1;
int random2=rand()%28+1;
int random3 = rand()%29+1;
int random4=rand()%28+1;
int x=10, y=10;
for (;;) {
if (_kbhit()) { //키보드 입력 확인 (true / false)
c = _getch(); // 방향키 입력시 224 00이 들어오게 되기에 앞에 있는 값 224를 없앰
if(c=='q')
{
gotoxy (0,30);
printf ("정말로 종료 하시겠습니까?\n예:a 아니요:b");
c=getch();
if (c=='a'){
return 0;
}
}
if (c == -32) { // -32로 입력되면
c = _getch(); // 새로 입력값을 판별하여 상하좌우 출력
switch (c) {
case LEFT:
if (x==1)
break;
x--;
break;
case RIGHT:
if (x==29)
break;
x++;
break;
case UP:
if (y==1)
break;
y--;
break;
case DOWN:
if (y==28)
break;
y++;
break;
}
}
system("cls");
gotoxy(random, random2);
printf ("@");
if (random==x&&random2==y){
random = rand()%29+1;
random2 = rand()%29+1;
hp+=10;
}
gotoxy(random3, random4);
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 1 );
printf ("!");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 15 );
if (random3==x&&random4==y){
random3 = rand()%29+1;
random4 = rand()%28+1;
hp-=10;
}
line();
init();
gotoxy(x,y);
printf("*");
}
if (hp==0){
system("cls");
gotoxy(0, 31);
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 9);
printf ("Game over");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 15 );
return 0;
}
}
return 0;
}