/***
*****게임 기능 정리**********
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;
int px=10, py=10; //player_x , player_y
void gotoxy(int x, int y)
{
COORD Pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
void line(int r, int r2, int r3, int r4)
{
for (int x=0; x<31; x++){
for (int y=0; y<31; y++){
gotoxy(x, y);
if (x==0||y==0||x==30||y==30){
printf (".");
}
else if(x==px&&y==py){
printf("*");
}
else if(x==r&&y==r2){
printf ("@");
}
else if(x==r3&&y==r4){
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 1 );
printf ("!");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 15 );
}
else{
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()%29+1;
int random3 = rand()%29+1;
int random4=rand()%29+1;
for (;;) {
if (_kbhit()) { //키보드 입력 확인 (true / false)
c = _getch(); // 방향키 입력시 224 00이 들어오게 되기에 앞에 있는 값 224를 없앰
if(c=='q')
{
gotoxy (0,32);
printf ("정말로 종료 하시겠습니까?\n예:a 아니요:b\n");
c=getch();
if (c=='a'){
printf ("\n아무 키나 눌러서 종료");
return 0;
}
else if(c=='b'){
system("cls");
}
}
if (c == -32) { // -32로 입력되면
c = _getch(); // 새로 입력값을 판별하여 상하좌우 출력
switch (c) {
case LEFT:
if (px==1)
break;
px--;
break;
case RIGHT:
if (px==29)
break;
px++;
break;
case UP:
if (py==1)
break;
py--;
break;
case DOWN:
if (py==29)
break;
py++;
break;
}
}
line(random, random2, random3, random4);
if (px==random&&py==random2)
{
printf (" ");
random = rand()%29+1;
random2 = rand()%29+1;
hp+=10;
gotoxy(random, random2);
printf ("@");
}
if (random3==px&&random4==py){
printf (" ");
random3 = rand()%29+1;
random4 = rand()%29+1;
hp-=10;
gotoxy (random3, random4);
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 1 );
printf ("!");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 15 );
}
init();
gotoxy(px,py);
}
if (hp==0){
system("cls");
gotoxy(0, 2);
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 9);
printf ("Game over\n");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 15 );
return 0;
}
}
return 0;
}
***/
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
int px=10 ;
int py=10 ;
void gotoxy(int x, int y)
{
COORD Pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
void map(int r1, int r2)
{
for (int x=0; x<31; x++){
for (int y=0; y<31; y++){
gotoxy(x, y);
if (x==0||y==0||x==30||y==30){
printf (".");
}
printf (" ");
else if(x==px&&y==py){
printf("*"); //뱀출력
}
}
}
}
int main()
{
char c;
srand(time(NULL)); //랜덤 시드 설정 (최초 한 번만!)
int random = rand()%29+1;
int random2=rand()%29+1;
for (;;){
if (c == -32) { // -32로 입력되면
c = getch(); // 새로 입력값을 판별하여 상하좌우 출력
switch (c) {
case LEFT:
px--;
map(random, random2); //방향대로 직진
break;
case RIGHT:
px++;
map(random, random2);
break;
case UP:
py--;
map(random, random2);
break;
case DOWN:
py++;
map(random, random2);
break;
}
}
}
}
/*
1.한칸 가고 뒤에 삭제 기다리고 가고 삭제 기다림(반복)
2.방향키 입력 받음=>방향바꿈 이후 1번처럼
3.@를 먹으면 한번 뒤에 삭제 하지 않음
4.벽//몸에 닿으면 종료
*/