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

[ChatGPT][SFML][2D 게임 개발] 2D 게임 캐릭터 스프라이트 SFML 샘플 코드

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

[SFML] 2D 게임 캐릭터 스프라이트 SFML 샘플 코드

// here is an example of how you could use a 2D character sprite sheet in C++ using the SFML library
#include <SFML/Graphics.hpp>

int main() {
    // Load the sprite sheet
    sf::Texture spriteSheet;
    spriteSheet.loadFromFile("character.png");

    // Create a sprite
    sf::Sprite sprite;
    sprite.setTexture(spriteSheet);

    // Create a window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Sprite Sheet Example");

    // Set the starting animation frame
    int frame = 0;

    // Main game loop
    while (window.isOpen()) {
        // Handle events
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }

        // Update the animation frame
        frame = (frame + 1) % 8; // 8 is the number of frames in the sprite sheet
        sprite.setTextureRect(sf::IntRect(frame * 64, 0, 64, 64)); // 64 is the width and height of each frame

        // Clear the window
        window.clear();

        // Draw the sprite
        window.draw(sprite);

        // Display the window
        window.display();
    }

    return 0;
}

/* here is an example of how you could handle input controls for a character in a 2D game using C++ and SFML library
// Update the character's position based on input
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
    sprite.move(-5, 0);
    // Set the animation to the walking left animation
    sprite.setTextureRect(sf::IntRect(frame * 64, 64, 64, 64));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
    sprite.move(5, 0);
    // Set the animation to the walking right animation
    sprite.setTextureRect(sf::IntRect(frame * 64, 0, 64, 64));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
    sprite.move(0, -5);
    // Set the animation to the jumping animation
    sprite.setTextureRect(sf::IntRect(frame * 64, 128, 64, 64));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
    sprite.move(0, 5);
    // Set the animation to the crouching animation
    sprite.setTextureRect(sf::IntRect(frame * 64, 192, 64, 64));
}

*/
728x90
LIST

댓글