[ChatGPT] Sample Code 샘플 코드
[ChatGPT][SFML][2D RPG 게임 개발] C++ 및 SFML을 사용하여 2D RPG 게임에서 스테이지 간 이동을 위한 시스템을 구현 샘플
byungwoo733
2023. 1. 12. 01:04
728x90
반응형
SMALL
C++ 및 SFML을 사용하여 2D RPG 게임에서 스테이지 간 이동을 위한 시스템을 구현 샘플
// Here's an example of how you might implement a system for moving between stages in a 2D RPG game using C++ and SFML
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <map>
class Stage {
public:
virtual void onEnter(sf::RenderWindow& window) = 0;
virtual void update(sf::RenderWindow& window) = 0;
virtual void onExit() = 0;
};
class Town : public Stage {
public:
void onEnter(sf::RenderWindow& window) override {
std::cout << "Welcome to the Town!" << std::endl;
// load town background
background.loadFromFile("town.png");
}
void update(sf::RenderWindow& window) override {
std::cout << "You are exploring the Town." << std::endl;
window.clear();
sf::Sprite sprite(background);
window.draw(sprite);
window.display();
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
// handle input
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
std::cout << "Exit Town" << std::endl;
onExit();
}
}
void onExit() override {
std::cout << "You are leaving the Town." << std::endl;
}
private:
sf::Texture background;
};
class Forest : public Stage {
public:
void onEnter(sf::RenderWindow& window) override {
std::cout << "Welcome to the Forest!" << std::endl;
// load forest background
background.loadFromFile("forest.png");
}
void update(sf::RenderWindow& window) override {
std::cout << "You are exploring the Forest." << std::endl;
window.clear();
sf::Sprite sprite(background);
window.draw(sprite);
window.display();
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
std::cout << "Exit Forest" << std::endl;
onExit();
}
}
void onExit() override {
std::cout << "You are leaving the Forest." << std::endl;
}
private:
sf::Texture background;
};
class Game {
public:
Game() {
window.create(sf::VideoMode(800, 600), "2D RPG Game");
stages["town"] = std::make_unique<Town>();
stages["forest"] = std::make_unique<Forest>();
currentStage = stages["town"].get();
currentStage->onEnter(window);
}
void run() {
while (window.isOpen()) {
currentStage->update(window);
if (currentStage == stages["town"].get() && sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
currentStage->onExit();
currentStage = stages["forest"].get();
currentStage->onEnter(window);
} else if (currentStage == stages["forest"].get() && sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
currentStage->onExit();
currentStage = stages["town"].get();
currentStage->onEnter(window);
}
}
}
private:
sf::RenderWindow window;
std::map<std::string, std::unique_ptr<Stage>> stages;
Stage* currentStage;
};
int main() {
Game game;
game.run();
return 0;
}
이 예에서 Game 클래스에는 게임의 다양한 단계를 모두 저장하는 단계 맵이 있습니다. 지도의 키는 문자열(이 경우 "town" 및 "forest")이고 값은 각각 Town 및 Forest 클래스의 인스턴스에 대한 std::unique_ptrs입니다. currentStage 멤버 변수는 플레이어가 있는 현재 스테이지에 대한 포인터입니다. 실행 기능에는 현재 단계에서 반복적으로 업데이트를 호출하고 단계 사이를 이동하기 위한 입력을 확인하는 기본 게임 루프가 포함되어 있습니다. onExit 및 onEnter 기능은 단계 간에 전환할 때 정리 및 설정을 처리하는 데 사용됩니다.
728x90
LIST