티스토리 뷰

마리오 게임을 구현해 보았다. 유튜브를 참고하였고, 내가 수정한 부분들도 있다. 유튜브 링크는:

https://www.youtube.com/watch?v=nD5OuMdS9FU 이다.

 

Stage.h

#pragma once
#include "value.h"

enum STAGE_BLOCK_TYPE {
	SBT_WALL = '0',
	SBT_ROAD = '1',
	SBT_START = '2',
	SBT_END = '3',
	SBT_COIN = '4'
};

class CStage {
private:
	char m_cOriginStage[BLOCK_Y][BLOCK_X];
	char m_cStage[BLOCK_Y][BLOCK_X];
	POINT m_tStart;
	POINT m_tEnd;
public:
	CStage();
	~CStage();
	bool Init();
	bool Init(char* pFileName);
	void Render();
	POINT GetStart() {
		return m_tStart;
	}
	POINT GetEnd() {
		return m_tEnd;
	}
	char GetBlock(int x, int y) { return m_cStage[y][x]; }
	void ChangeBlock(int x, int y, STAGE_BLOCK_TYPE eBlock) {
		m_cStage[y][x] = eBlock;
	}
	void ResetStage();
	int SaveCoin(int y, int x);
	char** GetStage() { return (char**)m_cStage; }
};

Stage.cpp

#include "Stage.h"
#include "FileStream.h"
#include "Player.h"
#include "ObjectManager.h"
#include "MapManager.h"
#include <string.h>

CStage::CStage() {
}

CStage::~CStage() {
}

bool CStage::Init() {
	return true;
}

bool CStage::Init(char* pFileName) {
	CFileStream file;
	char rt[20] = "rt";
	if (!file.Open(pFileName, rt))
		return false;
	for (int i = 0; i < 10; ++i) {
		int iSize = 0;
		file.ReadLine(m_cOriginStage[i], iSize);
		for (int j = 0; j < 50; ++j) {
			m_cStage[i][j] = m_cOriginStage[i][j];
			if (m_cOriginStage[i][j] == SBT_START) {
				m_tStart.x = j;
				m_tStart.y = i;
			}
			else if (m_cOriginStage[i][j] == SBT_END) {
				m_tEnd.x = j;
				m_tEnd.y = i;
			}
		}
	}
	return true;
}

void CStage::Render() {
	CPlayer* pPlayer = CObjectManager::GetInst()->GetPlayer();
	int iX = pPlayer->GetX();
	int iY = pPlayer->GetY();

	int iYCount = iY + (RENDER_BLOCK_Y / 2);
	if (iYCount >= BLOCK_Y)
		iYCount = BLOCK_Y - 1;
	
	int iYMin = iYCount - (RENDER_BLOCK_Y - 1);
	if (iYMin < 0)
		iYMin = 0;

	int iXCount = iX + RENDER_BLOCK_X;
	if (iXCount > BLOCK_X)
		iXCount = BLOCK_X;

	int iXMin = iX;
	if (iXMin > BLOCK_X - RENDER_BLOCK_X)
		iXMin = BLOCK_X - RENDER_BLOCK_X;

	for (int i = iYMin; i <= iYCount; ++i) {

		for (int j = iXMin; j < iXCount; ++j) {
			if (i == iY && j == iX)
				cout << 't';
			else if (m_cStage[i][j] == SBT_WALL)
				cout << "■";
			else if (m_cStage[i][j] == SBT_ROAD)
				cout << "  ";
			else if (m_cStage[i][j] == SBT_START)
				cout << "  ";
			else if (m_cStage[i][j] == SBT_END)
				cout << "E";
			else if (m_cStage[i][j] == SBT_COIN)
				cout << "★";
		}
		cout << endl;
	}
}

void CStage::ResetStage() {
	for (int i = 0; i < BLOCK_Y; ++i) {
		for (int j = 0; j < BLOCK_X; ++j) {
			m_cStage[i][j] = m_cOriginStage[i][j];
		}
	}
}

int CStage::SaveCoin(int y, int x) {
	int SaveData[10][50] = { 0, };
	CStage* pStage = CMapManager::GetInst()->GetStage();
	if (pStage->GetBlock(x, y) == SBT_COIN)
		SaveData[y][x] = 1;
	return SaveData[y][x];
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
TAG
more
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함