본문 바로가기
[ChatGPT] Sample Code 샘플 코드

[ChatGPT][SFML][2D 게임 개발] C++ 및 SFML을 사용하여 2D 게임에서 스테이지 간 이동을 위한 시스템을 구현 SFML 샘플

by byungwoo733 2023. 1. 12.
728x90
반응형
SMALL

C++ 및 SFML을 사용하여 2D 게임에서 스테이지 간 이동을 위한 시스템을 구현 SFML 샘플 

// Here's an example of how you might implement a system for moving between stages in a 2D game using C++ and SFML
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <map>

class Stage {
 public:
  virtual void onEnter() = 0;
  virtual void update(sf::RenderWindow& window) = 0;
  virtual void handleInput(sf::RenderWindow& window) = 0;
  virtual void onExit() = 0;
};

class Menu : public Stage {
 public:
  void onEnter() override {
    std::cout << "Welcome to the Menu!" << std::endl;
  }

  void update(sf::RenderWindow& window) override {
    // Draw menu background
    window.clear();
    window.draw(menuSprite);
    window.display();
  }

  void handleInput(sf::RenderWindow& window) override {
    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 menu" << std::endl;
      onExit();
    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
      std::cout << "Change to level 1" << std::endl;
      onExit();
    }
  }

  void onExit() override {
    std::cout << "You are leaving the Menu." << std::endl;
  }
 private:
  sf::Sprite menuSprite;
};

class Level1 : public Stage {
 public:
  void onEnter() override {
    std::cout << "Welcome to Level 1!" << std::endl;
  }

  void update(sf::RenderWindow& window) override {
    // Draw Level 1 background
    window.clear();
    window.draw(level1Sprite);
    window.display();
  }

  void handleInput(sf::RenderWindow& window) override {
    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 level 1" << std::endl;
      onExit();
    }
  }

  void onExit() override {
    std::cout << "You are leaving Level 1." << std::endl;
  }
 private:
  sf::Sprite level1Sprite;
};

class Game {
 public:
  Game() {
    window.create(sf::VideoMode(800, 600), "2D Game");
    stages["menu"] = std::make_unique<Menu>();
    stages["level1"] = std::make_unique<Level1>();
    currentStage = stages["menu"].get();
    currentStage->onEnter();
  }

void changeStage(const std::string& stageName) {
    currentStage->onExit();
    currentStage = stages[stageName].get();
    currentStage->onEnter();
  }

int main() {
  Game game;
  while (game.window.isOpen()) {
    game.currentStage->handleInput(game.window);
    game.currentStage->update(game.window);
  }
  return 0;
}

이렇게 하면 Game 클래스의 인스턴스가 생성되고 창이 열려 있는 동안 계속되는 루프에 들어갑니다. 루프 내에서 현재 단계의 handleInput 메서드를 호출하여 사용자 입력을 처리한 다음 현재 단계의 업데이트 메서드를 호출하여 게임 상태를 업데이트하고 현재 장면을 그립니다. 게임 루프에 대한 간단한 예이며, 충돌 및 물리 검사, 점수 매기기 등과 같은 더 많은 기능을 추가할 수 있습니다. 업데이트 또는 handleInput 내에서 단계가 변경되었는지 확인할 수도 있습니다. 변경된 경우 새 단계 이름으로 changeStage 메서드를 호출하십시오. game.window는 sf::RenderWindow 클래스의 인스턴스이며 SFML에 의해 생성된 창을 포함하고 있으므로 원하는 대로 이름을 변경할 수 있습니다.

 

728x90
LIST

댓글