티스토리 뷰

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

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

 

Player.h

#pragma once
#include "value.h"
#define JUMP_BLOCK_MAX 2

enum JUMP_DIR {
	JD_STOP,
	JD_UP,
	JD_DOWN
};

class CPlayer {
private:
	POINT m_tPos;
	bool m_bJump;
	int m_iJumpDir;
	int m_iJumpState;
	int m_iScore;
	bool m_bComplete;
public:
	CPlayer();
	~CPlayer();
	bool Init();
	int GetX() { return m_tPos.x; }
	int GetY() { return m_tPos.y; }
	void SetPos(int x, int y) {
		m_tPos.x = x;
		m_tPos.y = y;
	}
	void Update();
	int GetScore() { return m_iScore; }
	bool GetComplete() { return m_bComplete; }
	bool SetNotComplete() { m_bComplete = false; return m_bComplete; }
	int SetZeroScore() { m_iScore = 0; return m_iScore; }
};

Player.cpp

#include "Player.h"
#include "MapManager.h"
#include "Stage.h"

CPlayer::CPlayer() {
}

CPlayer::~CPlayer() {
}

bool CPlayer::Init() {
	m_tPos.x = 0;
	m_tPos.y = 8;
	m_bJump = false;
	m_iJumpDir = JD_STOP;
	m_iJumpState = 0;
	m_iScore = 0;
	m_bComplete = false;

	return true;
}

void CPlayer::Update() {
	CStage* pStage = CMapManager::GetInst()->GetStage();

	if (GetAsyncKeyState(VK_LEFT) & 0x8000) {
		if (pStage->GetBlock(m_tPos.x - 1, m_tPos.y) != SBT_WALL) {
			--m_tPos.x;
			if (m_tPos.x < 0)
				m_tPos.x = 0;
		}
	}
	if (GetAsyncKeyState(VK_RIGHT) & 0x8000) {
		if (pStage->GetBlock(m_tPos.x + 1, m_tPos.y) != SBT_WALL) {
			++m_tPos.x;
			if (m_tPos.x >= BLOCK_X)
				m_tPos.x = 49;
		}
	}
	if (GetAsyncKeyState(VK_UP) & 0x8000 && !m_bJump) {
		m_bJump = true;
		m_iJumpDir = JD_UP;
		m_iJumpState = 0;
	}
	if (m_bJump) {
		CStage* pStage = CMapManager::GetInst()->GetStage();
		switch (m_iJumpDir) {

		case JD_UP:

			++m_iJumpState;
			if (m_iJumpState > JUMP_BLOCK_MAX) {
				m_iJumpState = JUMP_BLOCK_MAX;
				m_iJumpDir = JD_DOWN;
			}
			else if (pStage->GetBlock(m_tPos.x, m_tPos.y - 1) == SBT_WALL) {
				--m_iJumpState;
				m_iJumpDir = JD_DOWN;
			}
			else {
				--m_tPos.y;
			}
			break;

		case JD_DOWN:
			 
			if (m_tPos.y >= BLOCK_Y) {
				cout << "플레이어 사망" << endl;
				system("pause");
				m_tPos.y = BLOCK_Y - 1;
			}
			else if (pStage->GetBlock(m_tPos.x, m_tPos.y + 1) == SBT_WALL) {
				m_iJumpDir = JD_STOP;
				m_bJump = false;
			}
			else
				++m_tPos.y;
			break;
		}
	}
	if (pStage->GetBlock(m_tPos.x, m_tPos.y + 1) != SBT_WALL && !m_bJump) {
		++m_tPos.y;
	}

	if (pStage->GetBlock(m_tPos.x, m_tPos.y) == SBT_COIN) {
		pStage->ChangeBlock(m_tPos.x, m_tPos.y, SBT_ROAD);
		m_iScore += 1000;
	}
	else if (pStage->GetBlock(m_tPos.x, m_tPos.y) == SBT_END) {
		m_bComplete = true;
	}
	if (m_tPos.y >= BLOCK_Y) {
		cout << "플레이어 사망" << endl;
		m_tPos = pStage->GetStart();
		m_iScore = 0;
		pStage->ResetStage();
		system("pause");
	}
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
TAG
more
«   2025/02   »
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
글 보관함