fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. return 0;
  7. }
Success #stdin #stdout 0.01s 5284KB
stdin
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <GL/glew.h>
#include <iostream>
#include <vector>

// 简单的3D向量类
struct Vector3 {
    float x, y, z;
    Vector3(float x = 0, float y = 0, float z = 0) : x(x), y(y), z(z) {}
};

// 玩家类
class Player {
public:
    Vector3 position;
    Vector3 direction;

    Player() : position(0, 0, -5), direction(0, 0, 1) {}

    void move(float deltaTime) {
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
            position.z += direction.z * deltaTime * 5.0f;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
            position.z -= direction.z * deltaTime * 5.0f;
        }
    }
};

// 子弹类
class Bullet {
public:
    Vector3 position;
    Vector3 velocity;

    Bullet(Vector3 pos, Vector3 vel) : position(pos), velocity(vel) {}

    void update(float deltaTime) {
        position.x += velocity.x * deltaTime;
        position.y += velocity.y * deltaTime;
        position.z += velocity.z * deltaTime;
    }
};

// 主程序
int main() {
    // 创建窗口
    sf::Window window(sf::VideoMode(800, 600), "3D Shooting Game", sf::Style::Default, sf::ContextSettings(32));
    window.setVerticalSyncEnabled(true);

    // 初始化GLEW
    glewExperimental = GL_TRUE;
    glewInit();

    // 启用深度测试
    glEnable(GL_DEPTH_TEST);

    // 玩家和子弹
    Player player;
    std::vector<Bullet> bullets;

    // 游戏循环
    sf::Clock clock;
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // 处理输入
        float deltaTime = clock.restart().asSeconds();
        player.move(deltaTime);

        // 射击
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
            bullets.push_back(Bullet(player.position, player.direction));
        }

        // 更新子弹
        for (auto& bullet : bullets) {
            bullet.update(deltaTime);
        }

        // 清除屏幕
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // 设置视角
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, 800.0f / 600.0f, 0.1f, 100.0f);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(player.position.x, player.position.y, player.position.z,
                  player.position.x + player.direction.x,
                  player.position.y + player.direction.y,
                  player.position.z + player.direction.z,
                  0, 1, 0);

        // 绘制玩家(简单的一个点)
        glBegin(GL_POINTS);
        glVertex3f(player.position.x, player.position.y, player.position.z);
        glEnd();

        // 绘制子弹
        glColor3f(1, 0, 0);
        for (auto& bullet : bullets) {
            glBegin(GL_POINTS);
            glVertex3f(bullet.position.x, bullet.position.y, bullet.position.z);
            glEnd();
        }

        // 显示内容
        window.display();
    }

    return 0;
}
stdout
Standard output is empty