20220414_게임을 만들어보자!

2022. 4. 14. 17:32C++

콘솔용 탈출게임 10 * 10 크기의 맵을 구성한다
(2차원 배열) 플레이어가 존재한다. (0,0)
탈출구가 존재한다. (0,0) 을 제외한 랜덤 설정                      // 입력
WASD 키입력을 통해 플레이어를 이동시킨다.                     // 처리
플레이어가 탈출구에 도달하면 "탈출 성공" 출력 후 종료       // 출력
SYSTEM ("cls"); 를 이용해 콘솔 초기화를 사용한다.

이런 느낌으로 만들면 된다. 

저같은 경우는 이런 코드로 만들었음.

더보기
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

int main()
{      
srand(time(NULL));
int px = 0;  // P x좌표
int py = 0;  // P y좌표       
int endx = rand() % 10; // E x좌표
int endy = rand() % 10; // E y좌표
         
while (1)  //반복문
{        
        for (int y = 0; y < 10; y++)  // 세로
        {
            for (int x = 0; x < 10; x++) // 가로
            {
                if (x == px && y == py)
                    cout << "P"; 
                else if (x == endx && y == endy)
                    cout << "E";
                else
                    cout << "#";
            }cout << endl; //줄 바꿈
        }
            
        int input = _getch();

            switch (input)
            {
            case 'a':
                if (px > 0) // a누르면 왼쪽으로 감
                {
                    px -= 1;
                }break;
            case 'd':
                if (px < 9) // d 누르면 오른쪽으로 감
                {
                    px += 1;
                }break;
            case 'w':      // w 누르면 위로 올라감
                if (py > 0)
                {
                    py -= 1;
                }break;
            case 's':
                if (py < 9)  // s 누르면 밑으로 감
                {
                    py += 1;
                }break;
            default:
                break;
            }
            system("cls"); // 종료후 재시작
            if (px == endx && py == endy) { 
                cout << "탈출에 성공하엿습니다. " << endl; // P가 E에 닿을경우 게임종료
                break;
            }
                
                
    }
}

 과제 : 미궁탈출 게임의 업데이트


 사용할 수 있는 모든 요소에 구조체를 활용해서 소스 코드를 개선해 보세요.
 tile : 지형 정보를 넣고 (숲,늪, 평지)
 player : 피로도를 넣고
 플레이어가 어느 지형에 있는지, 플레리어으이 피로도가 얼마인지 (숲 -1 ,늪 -3 , 평지 0 )
 표시되도록 수정해 봅시다.
 

업데이트를 해봤음..

↓↓↓↓↓↓↓↓    텍스쳐 변경

불 물 힐 요소 스테미너 추가

더보기

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

struct place
{
    int yr = rand() % 10;
    int xr = rand() % 10;
    int x;
    int y;
}finish, player, fire, ice, holly;



int main()
{
   srand(time(NULL));
   player.x = 0;  // P x좌표
   player.y = 0;  // P y좌표       
   
   finish.x = rand() % 10; // E x좌표
   finish.y = rand() % 10; // E y좌표
   
   ice.x = rand() % 10; // 빙결 x좌표
   ice.y = rand() % 10; // 빙결 y좌표
   holly.x = rand() % 10; // 힐 x좌표
   holly.y = rand() % 10;// 힐 y좌표
   
   int player_stas  = 100;
   
   for (int i = 0; i < 10; i++)
   {
       fire.x = rand() % 10;  //  불 x좌표
       fire.y = rand() % 10;//  불 y좌표

   }
   
        while (1)  //반복문
        {   
           
            
            for (int y = 0; y < 10; y++)  // 세로
            {
                for (int x = 0; x < 10; x++) // 가로
                {
                    if (x == player.x && y == player.y)
                        cout << "●";
                    else if (x == finish.x && y == finish.y)
                        cout << "★";                   
                    else if (x == fire.x && y == fire.y)
                        cout << "火";
                    else if (x == ice.x && y == ice.y)
                        cout << "水";
                    else if (x == holly.x && y == holly.y)
                        cout << "♥";
                    else
                        cout << "○";
                }cout << endl; //줄 바꿈
            }cout << "플레이어 스테미너는 : " << player_stas << endl;
            cout << "플레이어 : ● /  출구 : ★ / 불 : 火 /물 : 水/ 힐 : ♥ /"<< endl;
            cout << "불 : 데미지 50 /물 : 데미지 10/ 힐 : 체력회복 10 /" << endl;
            int input = _getch();

            switch (input)
            { 
            case 'a':
                if (player.x > 0)
                {
                    player.x -= 1,  player_stas--;
                        
                }break;
            case 'd':
                if (player.x < 9)
                {
                    player.x += 1,  player_stas--;
                }break;
            case 'w':
                if (player.y > 0)
                { 
                    player.y -= 1, player_stas--;
                     
                }break;                
            case 's':
                if (player.y < 9)
                {
                    player.y += 1, player_stas--;
                     
                }                                             
            default:
                break;

            }
            system("cls");
            
            if (player.x == finish.x && player.y == finish.y) 
            {
                cout << "탈출에 성공하엿습니다. " << endl;
                break;
            }if (player.x == fire.x && player.y == fire.y)
            {
                player_stas -= 50;
            }
            if (player.x == ice.x && player.y == ice.y)
            {
                player_stas -= 10;
            }
            if (player.x == holly.x && player.y == holly.y)
            {
                player_stas += 10;
            }if (player_stas < 0)
                break;

        }

}

문제점 : 요소들 중복으로 먹어짐. 효과도 중복적용 가끔 요소들 겹쳐서 효과 겹침

'C++' 카테고리의 다른 글

객체지향 프로그래밍  (0) 2022.05.30
202204_15 c++ 구조체  (0) 2022.04.15
20220413_배운것~~~!  (0) 2022.04.13
20220412_ c++ 조건문 반복문 분기문  (0) 2022.04.12
220407_C++연산  (0) 2022.04.07