본문 바로가기

C++/C++ 기본

C++로 방향키 제어 게임 만들기

반응형

map에서

#은 길을 나타내고

^는 가시를 나타내고 

a 는 hp 회복인 apple

★ 는 현재 플레이어를 나타낸다

 

source.cpp

#include <iostream>
#include<Windows.h>
using namespace std;

const int n = 20;

char map[n][n+1] = {
	"####################",
	"#  ^^^^^^^^^^^^^^^^#",
	"# ^ MMMMMMMMMMMMM  #",
	"#                  #",
	"######             #",
	"#      a           #",
	"#                  #",
	"#                  #",
	"#   Y              #",
	"#                  #",
	"#                  #",
	"#                  #",
	"#                  #",
	"#                  #",
	"#                  #",
	"#                  #",
	"#                  #",
	"#                  #",
	"#               a  #",
	"####################",
};

int nowY = 1;
int nowX = 1;
int hp = 100;



void print() {
	system("cls"); //화면을 다지운다;
	for (int y = 0; y < n; y++) {
		for (int x = 0; x < n; x++) {
			//주인공 좌표일떄는 "@"
			if (y == nowY && x == nowX) {
				cout << "★";
			}
			else if (map[y][x] == ' ') {
				cout << "  ";
			}
			else if (map[y][x] == '^') cout << "△";
			else if (map[y][x] == 'Y') cout << "▽";
			else if (map[y][x] == 'M') cout << "◈";
			else if (map[y][x] == '#') cout << "■";
			else if (map[y][x] == 'a') cout << "♡";
			
		}
		cout << endl;
	}
	cout << endl;
	cout << "HP : " << hp << endl;
}



int main()
{
	system("color 9f");
	system("mode con lines=25 cols=45");

	print();

	while (1) {
		
		if (GetAsyncKeyState(VK_UP) & 0x8001) {
			if (map[nowY - 1][nowX] != '#') {
			nowY--;
			}
			if (map[nowY][nowX] == '^') hp -= 10;
			print();
			/*cout << "Up\n";*/
		}
		else if (GetAsyncKeyState(VK_DOWN) & 0x8001) {
			if (map[nowY + 1][nowX] != '#') {
				nowY++;
			}
			if (map[nowY][nowX] == '^') hp -= 10;
			//cout << "DOWN\n";
			print();
		}
		else if (GetAsyncKeyState(VK_LEFT) & 0x8001) {
			if (map[nowY][nowX -1] != '#') {
			nowX--;
			}
			if (map[nowY][nowX] == '^') hp -= 10;
			//cout << "LEFT\n";
			print();
		}
		else if (GetAsyncKeyState(VK_RIGHT) & 0x8001) {
			if (map[nowY][nowX + 1] != '#') {	
			nowX++;
			if (map[nowY][nowX] == '^') hp -= 10;
			}
			//cout << "RIGHT\n";
			print();
		}
		Sleep(100);
		if (map[nowY][nowX] == 'Y') {
			print();
			for (int i = 0; i <=2; i++) {
				Sleep(200);
				system("color c4");
				Sleep(200);
				system("color 9f");
				
			}
			system("cls");
			cout << "\n\n\n WIN HP: "<<hp <<" \n\n\n\n\n\n";
			Sleep(1000);
			break;
		}

		if (map[nowY][nowX] == 'M' || hp == 0) {
			print();
			for (int i = 0; i <= 2; i++) {
				system("color c4");
				Sleep(200);
				system("color 9f");
				Sleep(200);
			}
			system("cls");
			cout << "\n\n\n GAME OVER \n\n\n\n\n\n";
			Sleep(1000);
			break;
		}

		if (map[nowY][nowX] == 'a') {
			print();
			hp = 100;
			system("color a2");
			Sleep(100);
			system("color 9f");
			Sleep(100);
			map[nowY][nowX] = ' ';
			print();
		};
		
		
	}
	return 0;
};

오픈시 화면 
GAME OVER나 WIN시 나타나는 화면 변환 
WIN시 HP가 같이 나타난다
GAME OVER

 

반응형

'C++ > C++ 기본' 카테고리의 다른 글

visual studio debug 활용하기  (0) 2020.09.16
visual studio 설치하기  (0) 2020.09.16