阿萨德
2026-11-19 19:53:29
发布于:浙江
#include <GLFW/glfw3.h>
#include <GL/gl.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
// 3D向量结构体
struct Vec3 {
float x, y, z;
Vec3(float x = 0, float y = 0, float z = 0) : x(x), y(y), z(z) {}
Vec3 operator+(const Vec3& v) const { return Vec3(x + v.x, y + v.y, z + v.z); }
Vec3 operator-(const Vec3& v) const { return Vec3(x - v.x, y - v.y, z - v.z); }
Vec3 operator*(float s) const { return Vec3(x * s, y * s, z * s); }
float length() const { return sqrt(x*x + y*y + z*z); }
Vec3 normalized() const {
float len = length();
if (len > 0) return Vec3(x/len, y/len, z/len);
return Vec3(0, 0, 0);
}
};
// 角色类
class Character {
public:
Vec3 position;
Vec3 rotation;
float health;
float speed;
std::string name;
bool isAttacking;
float attackCooldown;
float attackRange;
float attackDamage;
Character(std::string name, Vec3 startPos)
: name(name), position(startPos), health(100), speed(0.05f),
isAttacking(false), attackCooldown(0), attackRange(2.0f), attackDamage(10) {}
void Move(float dx, float dz) {
position.x += dx * speed;
position.z += dz * speed;
// 限制在场景范围内
position.x = std::max(-10.0f, std::min(10.0f, position.x));
position.z = std::max(-10.0f, std::min(10.0f, position.z));
}
void Attack() {
if (attackCooldown <= 0) {
isAttacking = true;
attackCooldown = 0.5f;
}
}
void Update(float deltaTime) {
if (attackCooldown > 0) {
attackCooldown -= deltaTime;
if (attackCooldown <= 0) {
isAttacking = false;
}
}
}
bool CheckHit(Character& other) {
if (isAttacking) {
float dist = (position - other.position).length();
return dist < attackRange;
}
return false;
}
void TakeDamage(float damage) {
health -= damage;
if (health < 0) health = 0;
}
};
// 绘制立方体(用于构建角色和场景)
void DrawCube(float size, float r, float g, float b) {
float h = size / 2.0f;
glColor3f(r, g, b);
// 前面
glBegin(GL_QUADS);
glVertex3f(-h, -h, h); glVertex3f(h, -h, h);
glVertex3f(h, h, h); glVertex3f(-h, h, h);
// 后面
glVertex3f(-h, -h, -h); glVertex3f(-h, h, -h);
glVertex3f(h, h, -h); glVertex3f(h, -h, -h);
// 左面
glVertex3f(-h, -h, -h); glVertex3f(-h, -h, h);
glVertex3f(-h, h, h); glVertex3f(-h, h, -h);
// 右面
glVertex3f(h, -h, -h); glVertex3f(h, h, -h);
glVertex3f(h, h, h); glVertex3f(h, -h, h);
// 上面
glVertex3f(-h, h, -h); glVertex3f(-h, h, h);
glVertex3f(h, h, h); glVertex3f(h, -h, h);
// 下面
glVertex3f(-h, -h, -h); glVertex3f(h, -h, h);
glVertex3f(h, -h, h); glVertex3f(-h, -h, h);
glEnd();
}
// 绘制角色
void DrawCharacter(Character& character, float r, float g, float b) {
glPushMatrix();
glTranslatef(character.position.x, character.position.y, character.position.z);
// 身体
DrawCube(1.0f, r, g, b);
// 头
glPushMatrix();
glTranslatef(0, 0.8f, 0);
DrawCube(0.5f, r*0.8f, g*0.8f, b*0.8f);
glPopMatrix();
// 攻击特效
if (character.isAttacking) {
glPushMatrix();
glTranslatef(0, 0.3f, 1.0f);
DrawCube(0.3f, 1.0f, 1.0f, 0.0f);
glPopMatrix();
}
glPopMatrix();
}
// 绘制地面
void DrawGround() {
glColor3f(0.3f, 0.6f, 0.3f);
glBegin(GL_QUADS);
glVertex3f(-10, -0.5f, -10);
glVertex3f(10, -0.5f, -10);
glVertex3f(10, -0.5f, 10);
glVertex3f(-10, -0.5f, 10);
glEnd();
// 网格线
glColor3f(0.2f, 0.4f, 0.2f);
glBegin(GL_LINES);
for (int i = -10; i <= 10; i++) {
glVertex3f(i, -0.49f, -10);
glVertex3f(i, -0.49f, 10);
glVertex3f(-10, -0.49f, i);
glVertex3f(10, -0.49f, i);
}
glEnd();
}
// 绘制血条
void DrawHealthBar(Character& character, float offsetX) {
glPushMatrix();
glTranslatef(character.position.x + offsetX, character.position.y + 1.5f, character.position.z);
// 背景
glColor3f(0.2f, 0.2f, 0.2f);
glBegin(GL_QUADS);
glVertex3f(-0.5f, 0, 0);
glVertex3f(0.5f, 0, 0);
glVertex3f(0.5f, 0.1f, 0);
glVertex3f(-0.5f, 0.1f, 0);
glEnd();
// 血量
float healthRatio = character.health / 100.0f;
glColor3f(1.0f - healthRatio, healthRatio, 0);
glBegin(GL_QUADS);
glVertex3f(-0.5f, 0, 0);
glVertex3f(-0.5f + healthRatio, 0, 0);
glVertex3f(-0.5f + healthRatio, 0.1f, 0);
glVertex3f(-0.5f, 0.1f, 0);
glEnd();
glPopMatrix();
}
// 绘制文字(简化版)
void DrawText(float x, float y, const char* text) {
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2f(x, y);
for (const char* c = text; *c != '\0'; c++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *c);
}
}
// 主函数
int main() {
// 初始化GLFW
if (!glfwInit()) {
stdcerr << "Failed to initialize GLFW" << stdendl;
return -1;
}
// 配置OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// 创建窗口
GLFWwindow* window = glfwCreateWindow(800, 600, "3D林泽坚大战贺宇翔", nullptr, nullptr);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glViewport(0, 0, 800, 600);
// 启用深度测试
glEnable(GL_DEPTH_TEST);
// 创建角色
Character lin("林泽坚", Vec3(-3, 0, 0));
Character he("贺宇翔", Vec3(3, 0, 0));
// 相机参数
float cameraAngle = 0;
float cameraHeight = 8;
float cameraDistance = 12;
// 游戏状态
bool gameOver = false;
std::string winner = "";
// 渲染循环
while (!glfwWindowShouldClose(window)) {
// 处理输入
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
// 林泽坚控制 (WASD + F攻击)
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) lin.Move(0, -1);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) lin.Move(0, 1);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) lin.Move(-1, 0);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) lin.Move(1, 0);
if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS) lin.Attack();
// 贺宇翔控制 (方向键 + 小键盘0攻击)
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) he.Move(0, -1);
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) he.Move(0, 1);
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) he.Move(-1, 0);
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) he.Move(1, 0);
if (glfwGetKey(window, GLFW_KEY_KP_0) == GLFW_PRESS) he.Attack();
// 相机控制
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) cameraAngle -= 0.02f;
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) cameraAngle += 0.02f;
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) cameraDistance -= 0.1f;
if (glfwGetKey(window, GLFW_KEY_T) == GLFW_PRESS) cameraDistance += 0.1f;
// 更新角色
lin.Update(0.016f);
he.Update(0.016f);
// 检测攻击
if (lin.CheckHit(he)) {
he.TakeDamage(lin.attackDamage);
}
if (he.CheckHit(lin)) {
lin.TakeDamage(he.attackDamage);
}
// 检查游戏结束
if (lin.health <= 0) {
gameOver = true;
winner = "贺宇翔";
}
if (he.health <= 0) {
gameOver = true;
winner = "林泽坚";
}
// 清屏
glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 设置相机
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = 800.0f / 600.0f;
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float camX = sin(cameraAngle) * cameraDistance;
float camZ = cos(cameraAngle) * cameraDistance;
gluLookAt(camX, cameraHeight, camZ, 0, 0, 0, 0, 1, 0);
// 绘制场景
DrawGround();
DrawCharacter(lin, 0.2f, 0.4f, 0.9f); // 蓝色 - 林泽坚
DrawCharacter(he, 0.9f, 0.2f, 0.2f); // 红色 - 贺宇翔
// 绘制血条
DrawHealthBar(lin, 0);
DrawHealthBar(he, 0);
// 游戏结束显示
if (gameOver) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 800, 600, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
std::string gameOverText = "游戏结束! " + winner + " 获胜!";
DrawText(300, 280, gameOverText.c_str());
DrawText(280, 320, "按 ESC 退出");
}
// 交换缓冲区
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
这里空空如也


















有帮助,赞一个