#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 hp=200,r,t,n,m,v,b,o,p;
void gotoXY(int x, int y)
{
COORD Pos;
Pos.X = x;
Pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
// 빨 50 노 100 초 800 노 900 빨
void viewhp()
{
if(hp<=50 || hp>=900)
{
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 4 );
}
else if(hp<=100 || hp>=800)
{
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 14 );
}
else
{
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 10 );
}
gotoXY(50,4);
printf("[ ");
for(int i=0;i<hp/20;i++){
printf("+");
}
for(int i=hp/20+1;i<50;i++){
printf(" ");
}
printf(" ]");
gotoXY(50,3);
printf("%HP:");
gotoXY(53,3);
printf("%3d",hp);
}
void init()
{
system( "mode con lines=30 cols=110" );
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");
}
item1();
item2();
item3();
item4();
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
gotoXY(50,6);
printf("1~9의 수를 누르면 플레이어의 색이 바뀝니다");
viewhp();
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
gotoXY(50,7);
printf("asdw는 A를, 방향키는 *을 조종합니다");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
gotoXY(50,5);
printf("+한개는 hp20 입니다");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
gotoXY(50,8);
printf("프로그램을 종료하고 싶으시면");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
gotoXY(50,9);
printf("shift버튼을 누르고 b버튼을 눌러 주세요");
gotoXY(x, y);
printf("*");
}
void item1(){ ///아이템의 랜덤 위치 지정
r = rand()%38 + 1;
t = rand()%18 + 1;
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
gotoXY(r,t);
printf("+");
gotoXY(0,23);
printf("r = %d t = %d",r,t);
}
void item2(){
n=rand()%38+1;
m=rand()%18+1;
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
gotoXY(n,m);
printf("-");
gotoXY(0,24);
printf("n = %d m = %d",n,m);
}
void item3(){
v=rand()%38+1;
b=rand()%18+1;
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
gotoXY(v,b);
printf("%%");
gotoXY(0,25);
printf("v = %d b = %d",v,b);
}
void item4(){
o=rand()%38+1;
p=rand()%18+1;
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
gotoXY(o,p);
printf("x");
gotoXY(0,26);
printf("o = %d p = %d",o,p);
}
int main()
{
char c;
x = 14;
y = 7;
srand(time(NULL));
init();
while(1)
{
if (kbhit()) ///키보드 입력 확인 (true / false)
{
//gotoXY(0,22);
//printf(" ");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
gotoXY(0,20);
printf("x = %2d y = %2d",x,y);
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;
hp--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color );
x--;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==1 ? '*' : 'A');
break;
case 77: /// RIGHT
if(x==38) {
continue;
}
playerType=1;
hp--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color );
x++;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==1 ? '*' : 'A');
break;
case 72: /// UP
if(y==1) {
continue;
}
playerType=1;
hp--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color );
y--;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==1 ? '*' : 'A');
break;
case 80: /// DOWN
if(y==18) {
continue;
}
playerType=1;
hp--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color );
y++;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==1 ? '*' : 'A');
break;
case 97: ///A
if(x==1) {
continue;
}
playerType=1;
hp--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color );
x--;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==2 ? '*' : 'A');
break;
case 100: ///D
if(x==38) {
continue;
}
playerType=1;
hp--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color );
x++;
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==2 ? '*' : 'A');
break;
case 119: ///w
if(y==1) {
continue;
}
playerType=1;
hp--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
y--;
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color );
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==2 ? '*' : 'A');
break;
case 115: ///S
if(y==18) {
continue;
}
playerType=1;
hp--;
map[x][y] = 0;
gotoXY(x, y);
printf(" ");
y++;
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color );
map[x][y] = 7;
gotoXY(x, y);
printf("%c", playerType==2 ? '*' : 'A');
break;
case 66:
system("cls");
return 0;
}
///플레이어가 아이템위치에 왔다면 (1. hp감소?증가? 2. 새로운 아이템 생성)
if(x==r&&y==t){
item1();
hp+=20;
gotoXY(0,22);
printf("+ 아이템을 획득");
}
else if(x==n&&y==m){
item2();
hp-=20;
gotoXY(0,22);
printf("- 아이템을 획득");
}
else if(x==v&&y==b){
item3();
hp/=2;
gotoXY(0,22);
printf("%% 아이템을 획득");
}
else if(x==o&&y==p){
item4();
hp*=2;
gotoXY(0,22);
printf("x 아이템을 획득");
}
///바뀐 HP를 다시 출력
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
viewhp();
///다시 원래 색상으로 변경
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color );
gotoXY(92,4);
if(hp<1)
{
system("cls");
gotoXY(30,10);
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
printf("Game Over");
Sleep(3500);
return 0;
}
else if(hp>1000)
{
system("cls");
gotoXY(30,10);
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
printf("Game Over");
gotoXY(30,9);
printf("너무 체력이 많아서 사망");
Sleep(3500);
return 0;
}
else if(hp==777)
{
system("cls");
gotoXY(30,10);
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
printf("Game Over");
gotoXY(30,9);
printf("HP가 너무 행운이여서 사망");
Sleep(3500);
return 0;
}
else if(hp==666)
{
system("cls");
gotoXY(30,10);
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
printf("Game Over");
gotoXY(30,9);
printf("HP가 너무 불행이여서 사망");
Sleep(3500);
return 0;
}
}
}
}