大佬们能改一下这个代码吗,顺便提高一下对手AI的能力,和随机性,美化一下武器和人物的样子,样子复杂点,请在完成后加一个注释?这是一个游戏代码,内涵注释的(这是蒟蒻调这个代码的第十三天,AI说的都改不出来,所以请勿提交AI答案)要求:适用于小熊猫c++3.1:
#include <iostream>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <cmath>
#include <cstdlib>
#include <bits/stdc++.h>
#ifndef WINDOW_PROC_H
#define WINDOW_PROC_H
//#include "WindowProc.h"
// 在这里声明 WndProc 函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
#endif
// 常量定义
const char* szClassName = "3DFightingGameClass";
const char* szAppName = "3D Fighting Game";
const char* szSelectClassName = "SelectWindowClass";
const char* szSelectAppName = "CharacterAndPropSelect";
// 角色相关常量
const float playerRadius = 20.0f;
const float playerHeight = 50.0f;
const float jumpForce = 10.0f;
const float gravity = 1.0f;
const float rotationSpeed = 5.0f;
const int attackDamage = 10;
const int initialHealth = 100;
// 定义角色样式数量和道具数量
const int MAX_CHARACTER_STYLES = 3;
const int MAX_PROPS = 5;
// 特殊道具标识,假设 0 为通用道具,1 - MAX_CHARACTER_STYLES 对应各角色特殊道具
const int GENERAL_PROP = 0;
// 新增用于人机控制的相关常量
const int HUMAN_AI_MOVE_INTERVAL = 30;
const int HUMAN_AI_MOVE_SPEED = 3;
const int HUMAN_AI_ATTACK_CHANCE = 10;
const int HUMAN_AI_JUMP_CHANCE = 5;
const int HUMAN_AI_TARGET_DISTANCE = 150;
// 全局变量
HINSTANCE hInstance;
HWND hwndMain;
HDC hdc;
HGLRC hglrc;
HWND hwndSelect;
float player1X = 0;
float player1Y = 0;
float player1Z = -100;
bool player1Attacking = false;
bool player1Jumping = false;
float player1JumpVelocity = 0;
float player1RotationX = 0; // 玩家 1 绕 X 轴旋转角度
float player1RotationY = 0; // 玩家 1 绕 Y 轴旋转角度
int player1Health = initialHealth;
int player1CharacterStyle = 0;
int player1Prop = 0;
float player2X = 0;
float player2Y = 0;
float player2Z = 100;
bool player2Attacking = false;
bool player2Jumping = false;
float player2JumpVelocity = 0;
float player2RotationX = 0; // 玩家 2 绕 X 轴旋转角度
float player2RotationY = 0; // 玩家 2 绕 Y 轴旋转角度
int player2Health = initialHealth;
int player2CharacterStyle = 0;
int player2Prop = 0;
// 用于存储角色样式和道具的图形数据(简单示例,可按需完善)
int characterStyles[3] = { 0, 0, 0 }; // 假设这里存储绘制不同角色样式的标识或数据指针
int props[5] = { 0, 0, 0, 0, 0 }; // 假设这里存储绘制不同道具的标识或数据指针
// 标记玩家 2 是否为电脑控制(人机模式),0 表示正常玩家,1 表示人机
int isPlayer2AI = 0;
// 人形身体相关尺寸常量
const float humanBodyWidth = 10.0f;
const float humanBodyHeight = 30.0f;
const float humanHeadRadius = 5.0f;
const float humanArmLength = 15.0f;
const float humanLegLength = 20.0f;
// 初始化 OpenGL 设置
void InitOpenGL() {
PIXELFORMATDESCRIPTOR pfd;
int iPixelFormat;
hdc = GetDC(hwndMain);
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cRedBits = 0;
pfd.cRedShift = 0;
pfd.cGreenBits = 0;
pfd.cGreenShift = 0;
pfd.cBlueBits = 0;
pfd.cBlueShift = 0;
pfd.cAlphaBits = 0;
pfd.cAlphaShift = 0;
pfd.cAccumBits = 0;
pfd.cAccumRedBits = 0;
pfd.cAccumGreenBits = 0;
pfd.cAccumBlueBits = 0;
pfd.cAccumAlphaBits = 0;
pfd.cDepthBits = 16;
pfd.cStencilBits = 0;
pfd.cAuxBuffers = 0;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.bReserved = 0;
pfd.dwLayerMask = 0;
pfd.dwVisibleMask = 0;
pfd.dwDamageMask = 0;
iPixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, iPixelFormat, &pfd);
hglrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hglrc);
// 新增光照相关初始化设置
// 启用光照
glEnable(GL_LIGHTING);
// 设置环境光强度
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
// 设置平行光属性
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat lightPosition[] = { 0.0f, 0.0f, -1.0f, 0.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
// 启用平行光
glEnable(GL_LIGHT0);
// 以下是原有的投影矩阵和模型视图矩阵初始化代码
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// 绘制玩家,根据角色样子、血量、道具等因素调整绘制逻辑
void DrawPlayer(float x, float y, float z, bool attacking, float rotationX, float rotationY, int characterStyle, int health, int prop) {
glPushMatrix();
glTranslatef(x, y, z);
glRotatef(rotationX, 0.0f, 1.0f, 0.0f);
glRotatef(rotationY, 1.0f, 0.0f, 0.0f);
// 设置材质属性(这里简单示例,可根据实际调整材质特性)
GLfloat ambientMaterial[] = { 0.8f, 0.6f, 0.4f, 1.0f };
GLfloat diffuseMaterial[] = { 0.8f, 0.6f, 0.4f, 1.0f };
GLfloat specularMaterial[] = { 0.2f, 0.2f, 0.2f, 1.0f };
glMaterialfv(GL_FRONT, GL_AMBIENT, ambientMaterial);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularMaterial);
glMaterialf(GL_FRONT, GL_SHININESS, 32.0f);
float colorFactor;
if (attacking) {
glColor3f(1.0f, 0.0f, 0.0f);
}
else {
colorFactor = (float)health / initialHealth;
glColor3f(colorFactor, colorFactor, colorFactor);
}
// 绘制身体(简单用长方体模拟)
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
glScalef(humanBodyWidth, humanBodyHeight, humanBodyWidth);
glutSolidCube(1.0);
glPopMatrix();
// 绘制头部(用球体模拟)
glPushMatrix();
glTranslatef(0.0f, humanBodyHeight, 0.0f);
glScalef(humanHeadRadius, humanHeadRadius, humanHeadRadius);
glutSolidSphere(1.0, 20, 20);
glPopMatrix();
// 绘制左臂(用长方体模拟)
glPushMatrix();
glTranslatef(-humanBodyWidth, humanBodyHeight / 2, 0.0f);
glScalef(humanArmLength, humanBodyWidth / 2, humanBodyWidth / 2);
glutSolidCube(1.0);
glPopMatrix();
// 绘制右臂(用长方体模拟)
glPushMatrix();
glTranslatef(humanBodyWidth, humanBodyHeight / 2, 0.0f);
glScalef(humanArmLength, humanBodyWidth / 2, humanBodyWidth / 2);
glutSolidCube(1.0);
glPopMatrix();
// 绘制左腿(用长方体模拟)
glPushMatrix();
glTranslatef(0.0f, -humanBodyHeight / 2, 0.0f);
glScalef(humanBodyWidth / 2, humanLegLength, humanBodyWidth / 2);
glutSolidCube(1.0);
glPopMatrix();
// 绘制右腿(用长方体模拟)
glPushMatrix();
glTranslatef(0.0f, -humanBodyHeight / 2, 0.0f);
glScalef(humanBodyWidth / 2, humanLegLength, humanBodyWidth / 2);
glutSolidCube(1.0);
glPopMatrix();
// 绘制所选道具(根据不同道具标识绘制不同图形,修改为新的道具绘制逻辑)
switch (prop) {
case GENERAL_PROP:
// 绘制长刀刀身(简单用长方体模拟)
glPushMatrix();
glTranslatef(0.0f, 0.0f, -10.0f);
glScalef(0.5f, 20.0f, 0.2f);
glutSolidCube(1.0);
glPopMatrix();
// 绘制长刀刀柄(用圆柱体模拟)
glPushMatrix();
glTranslatef(0.0f, -10.0f, 0.0f);
glScalef(0.5f, 5.0f, 0.5f);
glutSolidCone(1.0, 1.0, 10, 10);
glPopMatrix();
break;
case 1:
// 绘制锤子头部(用较大的长方体模拟,体现大的特点)
glPushMatrix();
glTranslatef(0.0f, 0.0f, -10.0f);
glScalef(5.0f, 5.0f, 5.0f);
glutSolidCube(1.0);
glPopMatrix();
// 绘制锤子柄(用圆柱体模拟)
glPushMatrix();
glTranslatef(0.0f, -15.0f, 0.0f);
glScalef(1.0f, 15.0f, 1.0f);
glutSolidCone(1.0, 1.0, 10, 10);
glPopMatrix();
// 绘制锤子上的刺(简单用小长方体模拟,多个表示有刺)
for (int i = 0; i < 4; ++i) {
glPushMatrix();
glTranslatef(-2.0f, 2.0f, -10.0f);
glScalef(1.0f, 1.0f, 1.0f);
glutSolidCube(1.0);
glPopMatrix();
}
break;
case 2:
// 绘制枪身(用长方体模拟)
glPushMatrix();
glTranslatef(0.0f, 0.0f, -10.0f);
glScalef(2.0f, 10.0f, 2.0f);
glutSolidCube(1.0);
glPopMatrix();
// 绘制枪柄(用长方体模拟)
glPushMatrix();
glTranslatef(0.0f, -10.0f, 0.0f);
glScalef(1.0f, 5.0f, 1.0f);
glutSolidCube(1.0);
glPopMatrix();
// 绘制枪口(简单用小圆柱体模拟,示意发射位置)
glPushMatrix();
glTranslatef(2.0f, 0.0f, 0.0f);
glScalef(0.5f, 1.0f, 0.5f);
glutSolidCone(1.0, 1.0, 10, 10);
glPopMatrix();
// 简单示意枪发射子弹击中对方减血逻辑(这里只是简单判断是否攻击状态,实际需要更多判断如距离、碰撞检测等)
if (attacking) {
float dx = std::abs(player1X - player2X);
float dy = std::abs(player1Y - player2Y);
float dz = std::abs(player1Z - player2Z);
if (dx <= 10 && dy <= 10 && dz <= 10) {
player2Health -= attackDamage;
if (player2Health < 0) {
player2Health = 0;
}
}
}
break;
case 3:
// 绘制长鞭(简单用一系列相连的小长方体模拟软的效果,这里只是简单示意,实际更复杂)
for (int i = 0; i < 10; ++i) {
glPushMatrix();
glTranslatef(0.0f, 0.0f, -10.0f + (i * 2.0f));
glScalef(0.5f, 1.0f, 0.5f);
glutSolidCube(1.0);
glPopMatrix();
}
break;
default:
break;
}
glPopMatrix();
}
// 人机(玩家 2)的具体行为逻辑
void HandlePlayer2AI() {
// 计算玩家 1 和玩家 2 之间的距离
float dx = player1X - player2X;
float dy = player1Y - player2Y;
float dz = player1Z - player2Z;
float distance = std::sqrt(dx * dx + dy * dy + dz * dz);
// 判断是否在攻击范围内,根据距离和攻击概率决定是否攻击
if (distance <= HUMAN_AI_TARGET_DISTANCE) {
int randomNum = rand() % 100;
if (randomNum < HUMAN_AI_ATTACK_CHANCE) {
player2Attacking = true;
if (std::abs(player2X - player1X) <= 10 && std::abs(player2Y - player1Y) <= 10 && std::abs(player2Z - player1Z) <= 10) {
player1Health -= attackDamage;
if (player1Health < 0) {
player1Health = 0;
}
}
} else {
player2Attacking = false;
}
}
// 判断是否跳跃,根据跳跃概率决定
int jumpRandom = rand() % 100;
if (jumpRandom < HUMAN_AI_JUMP_CHANCE) {
if (!player2Jumping) {
player2Jumping = true;
player2JumpVelocity = jumpForce;
}
}
// 根据玩家 1 位置决定移动方向
if (player1X > player2X) {
player2X += HUMAN_AI_MOVE_SPEED;
} else if (player1X < player2X) {
player2X -= HUMAN_AI_MOVE_SPEED;
}
if (player1Z > player2Z) {
player2Z += HUMAN_AI_MOVE_SPEED;
} else if (player1Z < player2Z) {
player2Z -= HUMAN_AI_MOVE_SPEED;
}
}
// 处理玩家移动,区分玩家 1 和人机(玩家 2)的操作逻辑
void HandlePlayerMovement() {
// 处理玩家 1 移动逻辑
if (GetAsyncKeyState('W') & 0x8000) {
player1Z -= 5;
}
if (GetAsyncKeyState('S') & 0x8000) {
player1Z += 5;
}
if (GetAsyncKeyState('A') & 0x8000) {
player1X -= 5;
}
if (GetAsyncKeyState('D') & 0x8000) {
player1X += 5;
}
if (GetAsyncKeyState('J') & 0x8000) {
player1Attacking = true;
if (std::abs(player1X - player2X) <= 10 && std::abs(player1Y - player2Y) <= 10 && std::abs(player1Z - player2Z) <= 10) {
player2Health -= attackDamage;
if (player2Health < 0) {
player2Health = 0;
}
}
} else {
player1Attacking = false;
}
if (GetAsyncKeyState(VK_SPACE) & 0x8000) {
if (!player1Jumping) {
player1Jumping = true;
player1JumpVelocity = jumpForce;
}
}
if (GetAsyncKeyState('Q') & 0x8000) {
player1RotationY -= rotationSpeed;
}
if (GetAsyncKeyState('E') & 0x8000) {
player1RotationY += rotationSpeed;
}
// 判断玩家 2 是否为人机模式
if (isPlayer2AI == 0) {
// 处理玩家 2 移动逻辑(正常玩家操作)
if (GetAsyncKeyState(VK_UP) & 0x8000) {
player2Z -= 5;
}
if (GetAsyncKeyState(VK_DOWN) & 0x8000) {
player2Z += 5;
}
if (GetAsyncKeyState(VK_LEFT) & 0x8000) {
player2X -= 5;
}
if (GetAsyncKeyState(VK_RIGHT) & 0x8000) {
player2X += 5;
}
if (GetAsyncKeyState(VK_RETURN) & 0x8000) {
player2Attacking = true;
if (std::abs(player2X - player1X) <= 10 && std::abs(player2Y - player1Y) <= 10 && std::abs(player2Z - player1Z) <= 10) {
player1Health -= attackDamage;
if (player1Health < 0) {
player1Health = 0;
}
}
} else {
player2Attacking = false;
}
if (GetAsyncKeyState(VK_RETURN) & 0x8000) {
if (!player2Jumping) {
player2Jumping = true;
player2JumpVelocity = jumpForce;
}
}
if (GetAsyncKeyState(VK_PRIOR) & 0x8000) {
player2RotationY -= rotationSpeed;
}
if (GetAsyncKeyState(VK_NEXT) & 0x8000) {
player2RotationY += rotationSpeed;
}
} else {
// 如果是人机模式,执行电脑控制逻辑
HandlePlayer2AI();
}
}
// 简单的获取指定范围内随机数的函数示例(不完善,仅供示意)
int GetRandomNumber(int minVal, int maxVal) {
return minVal + rand() % (maxVal - minVal + 1);
}
// 碰撞检测
void CheckCollision() {
float dx = player1X - player2X;
float dy = player1Y - player2Y;
float dz = player1Z - player2Z;
float distance = std::sqrt(dx * dx + dy * dy + dz * dz);
if (distance <= 2 * playerRadius) {
// 简单的碰撞处理,将玩家分开
if (dx > 0) {
player1X += 5;
player2X -= 5;
} else {
player1X -= 5;
player2X += 5;
}
if (dz > 0) {
player1Z += 5;
player2Z -= 5;
} else {
player1Z -= 5;
player2Z += 5;
}
}
}
// 选择界面窗口过程,处理角色样子和道具选择等操作
LRESULT CALLBACK SelectWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE: {
PIXELFORMATDESCRIPTOR pfd;
int iPixelFormat;
HDC hdc = GetDC(hwnd);
// 初始化像素格式描述符
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
// 选择合适的像素格式
iPixelFormat = ChoosePixelFormat(hdc, &pfd);
if (iPixelFormat == 0) {
MessageBox(hwnd, "无法选择合适的像素格式", "错误", MB_OK | MB_ICONERROR);
return -1;
}
// 设置像素格式
if (!SetPixelFormat(hdc, iPixelFormat, &pfd)) {
MessageBox(hwnd, "无法设置像素格式", "错误", MB_OK | MB_ICONERROR);
return -1;
}
// 创建 OpenGL 渲染上下文
HGLRC hglrcSelect = wglCreateContext(hdc);
if (hglrcSelect == NULL) {
MessageBox(hwnd, "无法创建 OpenGL 渲染上下文", "错误", MB_OK | MB_ICONERROR);
return -1;
}
// 激活 OpenGL 渲染上下文
if (!wglMakeCurrent(hdc, hglrcSelect)) {
MessageBox(hwnd, "无法激活 OpenGL 渲染上下文", "错误", MB_OK | MB_ICONERROR);
return -1;
}
// OpenGL 初始化设置
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 800.0f / 600.0f, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
return 0;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// 绘制角色样子选项
for (int i = 0; i < MAX_CHARACTER_STYLES; ++i) {
glPushMatrix();
switch (i) {
case 0:
glTranslatef(-50.0f, 0.0f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
break;
case 1:
glTranslatef(0.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
break;
case 2:
glTranslatef(50.0f, 0.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
break;
}
gluSphere(gluNewQuadric(), playerRadius, 20, 20);
glPopMatrix();
}
// 绘制道具选项
for (int i = 0; i < MAX_PROPS; ++i) {
glPushMatrix();
switch (i) {
case 0:
glTranslatef(-30.0f, -30.0f, -50.0f);
glColor3f(0.5f, 0.5f, 0.5f);
break;
case 1:
glTranslatef(-30.0f, 30.0f, -50.0f);
glColor3f(1.0f, 0.0f, 0.0f);
break;
case 2:
glTranslatef(30.0f, 30.0f, -50.0f);
glColor3f(0.0f, 1.0f, 0.0f);
break;
case 3:
glTranslatef(30.0f, -30.0f, -50.0f);
glColor3f(0.0f, 0.0f, 1.0f);
break;
case 4:
glTranslatef(0.0f, 0.0f, -50.0f);
glColor3f(1.0f, 1.0f, 0.0f);
break;
}
glScalef(5.0f, 5.0f, 5.0f);
glutSolidCube(1.0);
glPopMatrix();
}
// 处理鼠标点击选择逻辑
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000) {
POINT mousePos;
GetCursorPos(&mousePos);
ScreenToClient(hwnd, &mousePos);
// 假设窗口的视口是整个客户区
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
// 进行视口坐标到 OpenGL 坐标的转换
GLdouble modelview[16];
GLdouble projection[16];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
GLdouble winX = static_cast<GLdouble>(mousePos.x);
GLdouble winY = viewport[3] - static_cast<GLdouble>(mousePos.y);
GLdouble winZ;
// 获取鼠标点击位置的深度值
glReadPixels(static_cast<GLint>(winX), static_cast<GLint>(winY), 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &winZ);
GLdouble worldX, worldY, worldZ;
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &worldX, &worldY, &worldZ);
// 检查角色样式选择区域
if (worldX >= -50 - playerRadius && worldX <= -50 + playerRadius &&
worldY >= -playerRadius && worldY <= playerRadius) {
player1CharacterStyle = 0;
player2CharacterStyle = 0;
} else if (worldX >= -playerRadius && worldX <= playerRadius &&
worldY >= -playerRadius && worldY <= playerRadius) {
player1CharacterStyle = 1;
player2CharacterStyle = 1;
} else if (worldX >= 50 - playerRadius && worldX <= 50 + playerRadius &&
worldY >= -playerRadius && worldY <= playerRadius) {
player1CharacterStyle = 2;
player2CharacterStyle = 2;
}
// 检查道具选择区域
if (worldX >= -30 - 5 && worldX <= -30 + 5 &&
worldY >= -30 - 5 && worldY <= -30 + 5) {
player1Prop = GENERAL_PROP;
player2Prop = GENERAL_PROP;
} else if (worldX >= -30 - 5 && worldX <= -30 + 5 &&
worldY >= 30 - 5 && worldY <= 30 + 5) {
player1Prop = 1;
player2Prop = 1;
} else if (worldX >= 30 - 5 && worldX <= 30 + 5 &&
worldY >= 30 - 5 && worldY <= 30 + 5) {
player1Prop = 2;
player2Prop = 2;
} else if (worldX >= 30 - 5 && worldX <= 30 + 5 &&
worldY >= -30 - 5 && worldY <= -30 + 5) {
player1Prop = 3;
player2Prop = 3;
} else if (worldX >= -5 && worldX <= 5 &&
worldY >= -5 && worldY <= 5) {
player1Prop = 4;
player2Prop = 4;
}
}
SwapBuffers(hdc);
EndPaint(hwnd, &ps);
return 0;
}
case WM_DESTROY: {
// 释放 OpenGL 渲染上下文
HGLRC hglrcSelect = wglGetCurrentContext();
HDC hdc = wglGetCurrentDC();
if (hglrcSelect) {
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hglrcSelect);
}
if (hdc) {
ReleaseDC(hwnd, hdc);
}
PostQuitMessage(0);
return 0;
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
// 处理键盘输入事件
void HandleKeyboardInput() {
if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
PostQuitMessage(0);
}
}
// 声明 UpdateGameLogic 函数
void UpdateGameLogic();
// 游戏主循环
void GameLoop() {
MSG msg;
while (true) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// 处理玩家输入
HandleKeyboardInput();
HandlePlayerMovement();
// 更新游戏逻辑
UpdateGameLogic();
// 重绘窗口
InvalidateRect(hwndMain, NULL, TRUE);
}
}
// 更新游戏逻辑
void UpdateGameLogic() {
// 碰撞检测
CheckCollision();
// 更新玩家 1 的跳跃状态
if (player1Jumping) {
player1Z -= player1JumpVelocity;
player1JumpVelocity -= gravity;
if (player1Z <= 0) {
player1Z = 0;
player1Jumping = false;
player1JumpVelocity = 0;
}
}
// 更新玩家 2 的跳跃状态
if (player2Jumping) {
player2Z -= player2JumpVelocity;
player2JumpVelocity -= gravity;
if (player2Z <= 0) {
player2Z = 0;
player2Jumping = false;
player2JumpVelocity = 0;
}
}
// 如果玩家 2 是 AI,更新其行为
if (isPlayer2AI) {
HandlePlayer2AI();
}
}
// 主窗口过程函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE: {
InitOpenGL();
return 0;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// 设置相机位置
gluLookAt(0.0, 0.0, 200.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// 绘制玩家 1
DrawPlayer(player1X, player1Y, player1Z, player1Attacking, player1RotationX, player1RotationY, player1CharacterStyle, player1Health, player1Prop);
// 绘制玩家 2
DrawPlayer(player2X, player2Y, player2Z, player2Attacking, player2RotationX, player2RotationY, player2CharacterStyle, player2Health, player2Prop);
SwapBuffers(hdc);
EndPaint(hwnd, &ps);
return 0;
}
case WM_DESTROY: {
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hglrc);
ReleaseDC(hwnd, hdc);
PostQuitMessage(0);
return 0;
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
void UpdateGameLogic();
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int CmdShow) {
WNDCLASSEX wc;
MSG Msg;
// 注册选择界面窗口类
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = SelectWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szSelectClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, "窗口类注册失败!", "错误", MB_ICONERROR);
return 0;
}
// 创建正式游戏窗口
hwndMain = CreateWindowEx(
WS_EX_CLIENTEDGE,
szClassName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
800, 600,
NULL, NULL,
hInst, NULL
);
if (!hwndMain) {
MessageBox(NULL, "正式游戏窗口创建失败!", "错误", MB_ICONERROR);
return 0;
}
// 显示并更新正式游戏窗口
ShowWindow(hwndMain, CmdShow);
UpdateWindow(hwndMain);
// 初始化 OpenGL 设置
InitOpenGL();
// 进入游戏主循环
GameLoop();
// 释放 OpenGL 渲染上下文
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hglrc);
ReleaseDC(hwndMain, hdc);
return Msg.wParam;
}
// 主窗口过程函数,处理正式游戏窗口的消息
LRESULT CALLBACK WnpProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
// 窗口创建时的初始化操作,这里已在 InitOpenGL 中完成
break;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// 清除颜色缓冲区和深度缓冲区
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// 设置视角
gluLookAt(0.0f, 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
// 绘制玩家 1
DrawPlayer(player1X, player1Y, player1Z, player1Attacking, player1RotationX, player1RotationY, player1CharacterStyle, player1Health, player1Prop);
// 绘制玩家 2
DrawPlayer(player2X, player2Y, player2Z, player2Attacking, player2RotationX, player2RotationY, player2CharacterStyle, player2Health, player2Prop);
// 交换前后缓冲区,显示绘制结果
SwapBuffers(hdc);
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY:
// 窗口销毁时发送退出消息
PostQuitMessage(0);
break;
default:
// 默认消息处理
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
现在的报错信息:D:/桌面/lliii.cpp 0 -1 In function 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)':
D:/桌面/lliii.cpp 848 20 [警告] 'Msg.tagMSG::wParam' may be used uninitialized [-Wmaybe-uninitialized]
D:/桌面/lliii.cpp 795 13 [说明] 'Msg' declared here
D:/桌面/in function InitOpenGL()' 0 -1 D:/桌面/lliii.cpp 126 0 undefined reference to
__imp_ChoosePixelFormat'
D:/桌面/lliii.cpp 127 0 undefined reference to __imp_SetPixelFormat' D:/桌面/lliii.cpp 508 0 undefined reference to
__imp_ChoosePixelFormat'
D:/桌面/lliii.cpp 515 0 undefined reference to __imp_SetPixelFormat' D:/桌面/lliii.cpp 666 0 undefined reference to
__imp_SwapBuffers'
D:/桌面/lliii.cpp 777 0 undefined reference to __imp_SwapBuffers' D:/桌面/lliii.cpp 874 0 undefined reference to
__imp_SwapBuffers'
D:/桌面/collect2.exe 0 -1 [错误] ld returned 1 exit status
私信我:QQ3898506298
过关
还是算了吧,似乎不是很对
解好了,没加一个东西