跳到正文

休闲·娱乐

3d枪战( 3d后室改编)

#SUSPENDED#约 62 分钟
原文
文章信息与版本

文章信息

发布
2026年7月12日
收录
2026年7月13日
更新
2026年11月21日
篇幅
约 62 分钟
获赞
1

版本

  1. v2当前
  2. v1

版本:v1.5.2

别名:杀死恐怖奶奶(奶奶有5个)

必须看,不看不能玩!!!

运行前点开编译器工具里的编译选项,点最下面那个大框框,输入“-lgdi32 -luser32 -static-libgcc”,把小框勾上,就可以运行了!!!

玩家初始一把手枪,地图上加一些可拾取的武器,加特零(神器,射速极快!!!)、机枪、散弹枪、狙击枪(神器,点杀)、火箭筒(神器,范围杀)、激光枪,有血包,火箭筒炸得太近对自己有伤害,慎用,做了优化,不卡!!!

鼠标控制方向,上下键或wasd控制移动

运行时要等10s及以下才能加载好。

可以切换武器:

对应键盘按键切换的武器(没捡到的不能切换)
1手枪
2机枪
3散弹枪
4加特零
5激光枪
6狙击枪
7火箭筒

把奶奶杀完即可!!!

C++
#include <windows.h>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
//-lgdi32 -luser32 -static-libgcc
const int WIDTH = 800;
const int HEIGHT = 600;

int maze[12][12] = {
    {1,1,1,1,1,1,1,1,1,1,1,1},
    {1,0,1,0,1,0,1,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,1,1,0,1},
    {1,1,1,0,1,0,1,0,1,0,0,1},
    {1,0,1,0,1,0,1,0,1,0,0,1},
    {1,0,1,0,0,0,0,0,1,0,0,1},
    {1,0,1,1,1,1,1,0,1,1,0,1},
    {1,0,0,0,0,0,1,0,1,0,0,1},
    {1,0,1,0,1,0,1,0,0,0,0,1},
    {1,0,0,1,1,1,1,0,1,1,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1,1,1}
};
const int MAZE_W=12, MAZE_H=12;

// ── 玩家 ──
double px=1.5, py=1.5, dirX=-1, dirY=0, planeX=0, planeY=0.66;
bool isDead=false, isWin=false, hasFocus=false;
const float MOUSE_SENS=0.003f;
float playerHP=200.0f;
const float playerMaxHP=200.0f;
const float DAMAGE_RANGE=0.6f, DAMAGE_PER_SEC=12.0f;

// ── 武器系统 (7种) ──
enum WeaponType { WEAPON_PISTOL, WEAPON_MACHINEGUN, WEAPON_SHOTGUN, WEAPON_GATLING,
                  WEAPON_LASER, WEAPON_SNIPER, WEAPON_ROCKET, WEAPON_COUNT };
WeaponType currentWeapon=WEAPON_PISTOL;
float fireCooldown=0.0f;
bool weaponUnlocked[WEAPON_COUNT]={true,false,false,false,false,false,false};
struct WeaponInfo { float fireRate; int damage; int pellets; float spread; bool isRocket; float blastR; };
WeaponInfo weaponStats[WEAPON_COUNT]={
    {0.40f,25, 1,0.03f,false,0}, // 手枪
    {0.12f,12, 1,0.10f,false,0}, // 机枪
    {0.60f,18,10,0.35f,false,0}, // 散弹
    {0.03f, 8, 1,0.15f,false,0}, // 加特林
    {0.50f,50, 1,0.00f,false,0}, // 激光
    {1.20f,150,1,0.00f,false,0}, // 狙击
    {0.80f,50, 1,0.05f,true,2.0f},// 火箭(范围)
};
const char* weaponNames[WEAPON_COUNT]={"手枪","机枪","散弹","加特林","激光","狙击","火箭"};

// ── 激光 & 爆炸特效 ──
bool laserBeamActive=false; double laserEndX=0,laserEndY=0; float laserTimer=0.0f;
struct Explosion { double x,y; float timer; bool active; };
Explosion explosions[30];

// ── 子弹系统 ──
struct Bullet { double x,y,dx,dy; float life; int damage; bool active,isRocket; float blastR; };
const int MAX_BULLETS=150;
Bullet bullets[MAX_BULLETS];
const float BULLET_SPEED=18.0f, ROCKET_SPEED=10.0f;

// ── 敌人 (血量 + BFS缓存) ──
float enemyHP[5]={600.0f,600.0f,600.0f,2500.0f,600.0f};
float enemyMaxHPArr[5]={600.0f,600.0f,600.0f,2500.0f,600.0f};
float enemyDamage[5]={12.0f,12.0f,12.0f,25.0f,12.0f};
int eWPX[5]={-1,-1,-1,-1,-1}, eWPY[5]={-1,-1,-1,-1,-1};
int eLastPX[5]={-1,-1,-1,-1,-1}, eLastPY[5]={-1,-1,-1,-1,-1};

// ── 窗口 ──
HWND win; HDC bufDC; HBITMAP bufBmp; BITMAPINFO bmpInfo;
unsigned int* frameBuf; bool keyState[256]={false}; bool mouseDown=false;

// ── 纹理 ──
const int TEX_SIZE=64;
unsigned int texture[TEX_SIZE*TEX_SIZE], enemyTex[TEX_SIZE*TEX_SIZE];
unsigned int hpTex[TEX_SIZE*TEX_SIZE], weaponTex[6][TEX_SIZE*TEX_SIZE];
double zBuffer[WIDTH];

// ── 精灵 (3敌+3血+6武器=12) ──
struct Sprite { double x,y; unsigned int* tex; };
Sprite sprites[]={
    {3.5,3.5,enemyTex},{7.5,5.5,enemyTex},{5.5,8.5,enemyTex}, // 敌0-2
    {1.5,7.5,enemyTex},{9.5,3.5,enemyTex},                     // 敌3-4
    {9.5,1.5,hpTex},{5.5,5.5,hpTex},{1.5,9.5,hpTex},           // 血5-7
    {7.5,1.5,weaponTex[0]},{5.5,7.5,weaponTex[1]},             // 机枪8 散弹9
    {1.5,5.5,weaponTex[2]},{9.5,5.5,weaponTex[3]},             // 加特林10 激光11
    {5.5,3.5,weaponTex[4]},{7.5,6.5,weaponTex[5]},             // 狙击12 火箭13
};
const int SPRITE_COUNT=14, ENEMY_COUNT=5, HP_COUNT=3, WEAPON_COUNT_PICKUP=6;
bool hpActive[3]={true,true,true}, weaponActive[6]={true,true,true,true,true,true};
int g_spriteOrder[SPRITE_COUNT];
WeaponType weaponPickupType[6]={WEAPON_MACHINEGUN,WEAPON_SHOTGUN,WEAPON_GATLING,
                                WEAPON_LASER,WEAPON_SNIPER,WEAPON_ROCKET};

// ── 声明 ──
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
void CreateBuffer(); void DeleteBuffer();
void Render(); void Update(float dt);
bool IsBlock(double x,double y);
void GenerateTexture(); void GenerateEnemyTexture(); void GenerateHPTex();
void GenerateWeaponTex(int idx,unsigned int c);
void DrawMinimap(); void DrawHPBar(); void DrawEnemyHPBars(); void DrawLaserBeam(); void DrawExplosions();

// ═══════════════════ MAIN ═══════════════════
int main(){
    GenerateTexture(); GenerateEnemyTexture(); GenerateHPTex();
    GenerateWeaponTex(0,0xFFFF8800); GenerateWeaponTex(1,0xFFAA44FF);
    GenerateWeaponTex(2,0xFFCC2222); GenerateWeaponTex(3,0xFF00CCFF);
    GenerateWeaponTex(4,0xFF2244AA); GenerateWeaponTex(5,0xFFFF5500);
    const char cn[]="RayCastMaze";
    WNDCLASSEX wc={0}; wc.cbSize=sizeof(WNDCLASSEX);
    wc.lpfnWndProc=WinProc; wc.hInstance=GetModuleHandle(NULL); wc.lpszClassName=cn;
    RegisterClassEx(&wc);
    win=CreateWindowEx(0,cn,"3D Shooter Maze",WS_OVERLAPPEDWINDOW,100,100,WIDTH,HEIGHT,NULL,NULL,GetModuleHandle(NULL),NULL);
    ShowWindow(win,SW_SHOW); UpdateWindow(win); SetForegroundWindow(win); SetFocus(win); CreateBuffer();
    MSG msg; LARGE_INTEGER f,tN,tL; QueryPerformanceFrequency(&f); QueryPerformanceCounter(&tL);
    while(1){
        while(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
            if(msg.message==WM_QUIT) goto quit;
            if(msg.message!=WM_MOUSEMOVE){ TranslateMessage(&msg); DispatchMessage(&msg); }
        }
        QueryPerformanceCounter(&tN);
        float dt=(float)((double)(tN.QuadPart-tL.QuadPart)/f.QuadPart);
        if(dt>0.1f)dt=0.1f; tL=tN;
        Update(dt); Render();
        HDC hdc=GetDC(win); BitBlt(hdc,0,0,WIDTH,HEIGHT,bufDC,0,0,SRCCOPY); ReleaseDC(win,hdc);
        LARGE_INTEGER tE; QueryPerformanceCounter(&tE);
        while((double)(tE.QuadPart-tN.QuadPart)/f.QuadPart<0.0069){QueryPerformanceCounter(&tE);}
    }
    quit: DeleteBuffer(); return 0;
}

// ═══════════════════ WINDOW ═══════════════════
LRESULT CALLBACK WinProc(HWND h,UINT m,WPARAM w,LPARAM l){
    switch(m){
    case WM_SETFOCUS: hasFocus=true; ShowCursor(FALSE);
        { RECT r; GetClientRect(h,&r); POINT c={r.right/2,r.bottom/2}; ClientToScreen(h,&c); SetCursorPos(c.x,c.y); }
        break;
    case WM_KILLFOCUS: hasFocus=false; ShowCursor(TRUE); break;
    case WM_LBUTTONDOWN: mouseDown=true; break;
    case WM_LBUTTONUP: mouseDown=false; break;
    case WM_KEYDOWN: keyState[(BYTE)w]=true; break;
    case WM_KEYUP: keyState[(BYTE)w]=false; break;
    case WM_DESTROY: PostQuitMessage(0); break;
    default: return DefWindowProc(h,m,w,l);
    } return 0;
}
void CreateBuffer(){
    HDC hdc=GetDC(win); bufDC=CreateCompatibleDC(hdc);
    ZeroMemory(&bmpInfo,sizeof(bmpInfo));
    bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
    bmpInfo.bmiHeader.biWidth=WIDTH; bmpInfo.bmiHeader.biHeight=-HEIGHT;
    bmpInfo.bmiHeader.biPlanes=1; bmpInfo.bmiHeader.biBitCount=32; bmpInfo.bmiHeader.biCompression=BI_RGB;
    bufBmp=CreateDIBSection(bufDC,&bmpInfo,DIB_RGB_COLORS,(void**)&frameBuf,NULL,0);
    SelectObject(bufDC,bufBmp); ReleaseDC(win,hdc);
}
void DeleteBuffer(){ DeleteObject(bufBmp); DeleteDC(bufDC); }
bool IsBlock(double x,double y){
    int ix=(int)(x+0.2),iy=(int)(y+0.2),ix2=(int)(x-0.2),iy2=(int)(y-0.2);
    if(ix>=0&&ix<MAZE_W&&iy>=0&&iy<MAZE_H&&maze[iy][ix]==1)return true;
    if(ix2>=0&&ix2<MAZE_W&&iy2>=0&&iy2<MAZE_H&&maze[iy2][ix2]==1)return true;
    return false;
}

// ═══════════════════ TEXTURES ═══════════════════
void GenerateTexture(){
    for(int y=0;y<TEX_SIZE;y++)for(int x=0;x<TEX_SIZE;x++){
        unsigned int c; if(x%16==0||y%8==0)c=0x00555555;
        else{int bx=x;if((y/8)%2==1)bx+=8;c=(bx%16==0)?0x00555555:0x00AA2222;}
        texture[y*TEX_SIZE+x]=c;
    }
}
void GenerateEnemyTexture(){
    for(int y=0;y<TEX_SIZE;y++)for(int x=0;x<TEX_SIZE;x++){
        bool bd=(x>10&&x<54&&y>10&&y<54);
        bool ey=(x>18&&x<28&&y>20&&y<30)||(x>36&&x<46&&y>20&&y<30);
        if(ey)enemyTex[y*TEX_SIZE+x]=0xFFFFFFFF;
        else if(bd)enemyTex[y*TEX_SIZE+x]=0xFF22AA22;
        else enemyTex[y*TEX_SIZE+x]=0x00000000;
    }
}
void GenerateHPTex(){
    for(int y=0;y<TEX_SIZE;y++)for(int x=0;x<TEX_SIZE;x++){
        int cx=x-TEX_SIZE/2,cy=y-TEX_SIZE/2;
        bool cr=(abs(cx)<7&&abs(cy)<18)||(abs(cy)<7&&abs(cx)<18),bg=(abs(cx)<22&&abs(cy)<22);
        if(cr)hpTex[y*TEX_SIZE+x]=0xFFFF0000;else if(bg)hpTex[y*TEX_SIZE+x]=0xFFFFFFFF;
        else hpTex[y*TEX_SIZE+x]=0x00000000;
    }
}
void GenerateWeaponTex(int idx,unsigned int clr){
    unsigned int dark=0xFF333333,light=0xFFDDDDDD,white=0xFFFFFFFF;
    for(int y=0;y<TEX_SIZE;y++)for(int x=0;x<TEX_SIZE;x++){
        int cx=x-TEX_SIZE/2,cy=y-TEX_SIZE/2; bool in=false; unsigned int pc=clr;
        if(idx==0){ // 机枪: 枪身+弹匣+枪托
            if(cx>5&&cx<30&&abs(cy)<4)in=true;                    // 枪管
            else if(cx>-10&&cx<8&&abs(cy)<10)in=true;              // 枪身
            else if(cx>-28&&cx<-8&&cy>-7&&cy<7&&abs(cy)<7-abs(cx+18)/2){in=true;pc=dark;} // 枪托
            else if(cx>-5&&cx<3&&cy>8&&cy<16)in=true;              // 弹匣
            else if(cx>-8&&cx<-2&&cy>8&&cy<18){in=true;pc=dark;}   // 握把
        }else if(idx==1){ // 散弹枪: 宽双管+泵动
            if(cx>5&&cx<22&&abs(cy)<3)in=true;                     // 上管
            else if(cx>5&&cx<22&&abs(cy-6)<3)in=true;              // 下管
            else if(cx>-5&&cx<8&&abs(cy)<10)in=true;               // 泵动护木
            else if(cx>-25&&cx<-3&&abs(cy)<8)in=true;              // 枪身
            else if(cx>-12&&cx<-5&&cy>6&&cy<18){in=true;pc=dark;}  // 握把
        }else if(idx==2){ // 加特林: 旋转多管
            if(cx*cx+cy*cy<10*10){in=true;pc=dark;}                // 中心体
            for(int b=0;b<6;b++){float a=b*3.14159f*2/6;int bx=(int)(14*cos(a)),by=(int)(14*sin(a));
                if((cx-bx)*(cx-bx)+(cy-by)*(cy-by)<3*3){in=true;pc=clr;}} // 6根枪管
            if(cx*cx+cy*cy>12*12&&cx*cx+cy*cy<15*15)in=true;      // 外环
            if(cx*cx+cy*cy<3*3){in=true;pc=white;}                 // 中心亮点
        }else if(idx==3){ // 激光枪: 科幻手枪+发光口
            if(cx>-12&&cx<8&&abs(cy)<10)in=true;                   // 枪身
            else if(cx>6&&cx<24&&abs(cy)<3)in=true;                // 发射管
            else if(cx>22&&cx<26&&abs(cy)<5){in=true;pc=white;}    // 发光口
            else if(cx>-8&&cx<0&&cy>8&&cy<20){in=true;pc=dark;}    // 握把
            else if(cx>-10&&cx<-2&&cy>-3&&cy<5){in=true;pc=0xFFFF0000;} // 能量电池
        }else if(idx==4){ // 狙击枪: 长管+瞄准镜+双脚架
            if(cx>-5&&cx<28&&abs(cy)<2)in=true;                    // 枪管
            else if(cx>0&&cx<12&&cy>-10&&cy<-3)in=true;            // 瞄准镜
            else if(cx>10&&cx<13&&cy>-12&&cy<-1){in=true;pc=0xFF88CCFF;} // 镜片
            else if(cx>-15&&cx<5&&abs(cy)<7)in=true;               // 枪身
            else if(cx>-28&&cx<-12&&abs(cy)<5){in=true;pc=dark;}   // 枪托
            else if(cx>10&&cx<16&&cy>1&&cy<12){in=true;pc=dark;}   // 脚架竖
            else if(cx>8&&cx<18&&cy>10&&cy<13){in=true;pc=dark;}   // 脚架横
        }else{ // 火箭筒: 圆筒+弹头+尾翼
            if(cx>-20&&cx<20&&abs(cy)<8)in=true;                   // 筒身
            else if(cx>15&&cx<26&&abs(cy)<(cx-10)*0.6+1)in=true;   // 弹头锥
            else if(cx>-26&&cx<-18&&abs(cy)>5&&abs(cy)<14)in=true; // 尾翼
            else if(cx>-5&&cx<5&&cy>6&&cy<16){in=true;pc=dark;}    // 握把
        }
        weaponTex[idx][y*TEX_SIZE+x]=in?pc:0x00000000;
    }
}

// ═══════════════════ HUD ═══════════════════
void DrawMinimap(){
    int ms=100,cs=ms/MAZE_W,ox=WIDTH-ms-10,oy=10;
    for(int y=0;y<ms;y++)for(int x=0;x<ms;x++)frameBuf[(oy+y)*WIDTH+(ox+x)]=0x00000000;
    for(int y=0;y<MAZE_H;y++)for(int x=0;x<MAZE_W;x++)
        if(maze[y][x]==1)for(int dy=0;dy<cs;dy++)for(int dx=0;dx<cs;dx++)
            frameBuf[(oy+y*cs+dy)*WIDTH+(ox+x*cs+dx)]=0x00AAAAAA;
    for(int i=0;i<SPRITE_COUNT;i++){
        int ex=ox+(int)(sprites[i].x*cs),ey=oy+(int)(sprites[i].y*cs); unsigned int clr;
        if(i<ENEMY_COUNT){if(enemyHP[i]<=0)continue;clr=0x000000FF;}
        else if(i<ENEMY_COUNT+HP_COUNT){if(!hpActive[i-ENEMY_COUNT])continue;clr=0x00FF0000;}
        else{if(!weaponActive[i-ENEMY_COUNT-HP_COUNT])continue;clr=0x00FFAA00;}
        for(int dy=-1;dy<=1;dy++)for(int dx=-1;dx<=1;dx++)
            if(ex+dx>ox&&ex+dx<ox+ms&&ey+dy>oy&&ey+dy<oy+ms)frameBuf[(ey+dy)*WIDTH+(ex+dx)]=clr;
    }
    int ppx=ox+(int)(px*cs),ppy=oy+(int)(py*cs);
    for(int dy=-1;dy<=1;dy++)for(int dx=-1;dx<=1;dx++)
        if(ppx+dx>ox&&ppx+dx<ox+ms&&ppy+dy>oy&&ppy+dy<oy+ms)frameBuf[(ppy+dy)*WIDTH+(ppx+dx)]=0x0000FF00;
}

void DrawHPBar(){
    int bw=300,bh=24,bx=(WIDTH-bw)/2,by=10;
    float r=playerHP/playerMaxHP;if(r<0)r=0;if(r>1)r=1;
    for(int y=by;y<by+bh;y++)for(int x=bx;x<bx+bw;x++)frameBuf[y*WIDTH+x]=0xFF111111;
    int fill=(int)(r*(bw-4));
    unsigned int clr=(r>0.6f)?0xFF00DD00:(r>0.3f)?0xFFDDDD00:0xFFDD0000;
    for(int y=by+2;y<by+bh-2;y++)for(int x=bx+2;x<bx+2+fill;x++)frameBuf[y*WIDTH+x]=clr;
    for(int i=0;i<bw;i++){frameBuf[by*WIDTH+bx+i]=0xFFFFFFFF;frameBuf[(by+bh-1)*WIDTH+bx+i]=0xFFFFFFFF;}
    for(int i=0;i<bh;i++){frameBuf[(by+i)*WIDTH+bx]=0xFFFFFFFF;frameBuf[(by+i)*WIDTH+bx+bw-1]=0xFFFFFFFF;}
}

void DrawEnemyHPBars(){
    double id=1.0/(planeX*dirY-dirX*planeY);
    for(int si=0;si<SPRITE_COUNT;si++){
        int idx=g_spriteOrder[si]; if(idx>=ENEMY_COUNT||enemyHP[idx]<=0)continue;
        double sx=sprites[idx].x-px,sy=sprites[idx].y-py;
        double tY=id*(-planeY*sx+planeX*sy);if(tY<=0.1)continue;
        double tX=id*(dirY*sx-dirX*sy);
        int scrX=(int)((WIDTH/2)*(1+tX/tY)),sprH=abs((int)(HEIGHT/tY));
        int barY=-sprH/2+HEIGHT/2-8,barH=5,barW=(int)(sprH*0.7),barX=scrX-barW/2;
        if(barW<10){barW=10;barX=scrX-5;}if(barY<0)barY=0;if(barY+barH>=HEIGHT)barY=HEIGHT-barH-1;
        for(int y=barY;y<barY+barH;y++)for(int x=barX;x<barX+barW;x++)
            if(x>=0&&x<WIDTH)frameBuf[y*WIDTH+x]=0xFF330000;
        float r=enemyHP[idx]/enemyMaxHPArr[idx];if(r<0)r=0;
        int fW=(int)(r*barW);unsigned int clr2=(r>0.5f)?0xFF00CC00:0xFFCC0000;
        for(int y=barY;y<barY+barH;y++)for(int x=barX;x<barX+fW;x++)
            if(x>=0&&x<WIDTH)frameBuf[y*WIDTH+x]=clr2;
    }
}

void DrawLaserBeam(){
    if(!laserBeamActive)return;
    double id=1.0/(planeX*dirY-dirX*planeY);
    int scrX0=WIDTH/2,scrY0=HEIGHT/2;
    double bx=laserEndX-px,by=laserEndY-py;
    double tY=id*(-planeY*bx+planeX*by);if(tY<=0.1)return;
    double tX=id*(dirY*bx-dirX*by);
    int scrX1=(int)((WIDTH/2)*(1+tX/tY)),scrY1=HEIGHT/2;
    int dx=scrX1-scrX0,dy=scrY1-scrY0,steps=abs(dx)>abs(dy)?abs(dx):abs(dy);if(steps<1)steps=1;
    for(int s=0;s<=steps;s++){
        int sx=scrX0+(int)(dx*(float)s/steps),sy=scrY0+(int)(dy*(float)s/steps);
        for(int dy2=-2;dy2<=2;dy2++)for(int dx2=-2;dx2<=2;dx2++)
            if(sx+dx2>=0&&sx+dx2<WIDTH&&sy+dy2>=0&&sy+dy2<HEIGHT)frameBuf[(sy+dy2)*WIDTH+(sx+dx2)]=0xFFFF3333;
    }
}

void DrawExplosions(){
    double id=1.0/(planeX*dirY-dirX*planeY);
    for(int i=0;i<30;i++){
        if(!explosions[i].active)continue;
        double ex=explosions[i].x-px,ey=explosions[i].y-py;
        double tY=id*(-planeY*ex+planeX*ey);if(tY<=0.1)continue;
        double tX=id*(dirY*ex-dirX*ey);
        int scrX=(int)((WIDTH/2)*(1+tX/tY));
        float progress=1.0f-explosions[i].timer/0.35f;
        int radius=(int)((20.0f+60.0f*progress)/tY);if(radius<2)radius=2;if(radius>60)radius=60;
        for(int dy=-radius;dy<=radius;dy++){
            int py2=HEIGHT/2+dy;if(py2<0||py2>=HEIGHT)continue;
            for(int dx=-radius;dx<=radius;dx++){
                int px2=scrX+dx;if(px2<0||px2>=WIDTH)continue;
                int ds=dx*dx+dy*dy;
                if(ds<=radius*radius&&ds>=(radius-3)*(radius-3)){
                    unsigned int eclr=(progress>0.5f)?0xFFFF6600:0xFF884400;
                    frameBuf[py2*WIDTH+px2]=eclr;
                }
            }
        }
    }
}

// ═══════════════════ RENDER ═══════════════════
void Render(){
    memset(frameBuf,0,WIDTH*HEIGHT*4);
    // 墙壁 DDA
    for(int x=0;x<WIDTH;x++){
        double cX=2.0*x/WIDTH-1.0;
        double rDX=dirX+planeX*cX,rDY=dirY+planeY*cX;
        if(fabs(rDX)<1e-10)rDX=(rDX>=0)?1e-10:-1e-10;
        if(fabs(rDY)<1e-10)rDY=(rDY>=0)?1e-10:-1e-10;
        int mX=(int)px,mY=(int)py;
        double dX=fabs(1/rDX),dY=fabs(1/rDY),sX,sY;int stX,stY,side;bool hit=false;
        if(rDX<0){stX=-1;sX=(px-mX)*dX;}else{stX=1;sX=(mX+1.0-px)*dX;}
        if(rDY<0){stY=-1;sY=(py-mY)*dY;}else{stY=1;sY=(mY+1.0-py)*dY;}
        while(!hit){
            if(sX<sY){sX+=dX;mX+=stX;side=0;}else{sY+=dY;mY+=stY;side=1;}
            if(mX>=0&&mX<MAZE_W&&mY>=0&&mY<MAZE_H){if(maze[mY][mX]>0)hit=true;}else hit=true;
        }
        double pD=(side==0)?(mX-px+(1-stX)/2.0)/rDX:(mY-py+(1-stY)/2.0)/rDY;zBuffer[x]=pD;
        double wX=(side==0)?py+pD*rDY:px+pD*rDX;wX-=floor(wX);
        int tX=(int)(wX*TEX_SIZE);
        if(side==0&&rDX>0)tX=TEX_SIZE-tX-1;if(side==1&&rDY<0)tX=TEX_SIZE-tX-1;
        int lH=(int)(HEIGHT/pD),dT=-lH/2+HEIGHT/2;if(dT<0)dT=0;
        int dB=lH/2+HEIGHT/2;if(dB>=HEIGHT)dB=HEIGHT-1;
        for(int y=dT;y<=dB;y++){
            int d=y*256-HEIGHT*128+lH*128,tY=((d*TEX_SIZE)/lH)/256;
            unsigned int c=texture[TEX_SIZE*tY+tX];
            double sh=4.0/pD;if(sh>1.0)sh=1.0;if(side==1)sh*=0.7;
            int b=((c>>16)&0xFF)*sh,g=((c>>8)&0xFF)*sh,r=(c&0xFF)*sh;
            frameBuf[y*WIDTH+x]=(255<<24)|(r<<16)|(g<<8)|b;
        }
        for(int y=0;y<dT;y++)frameBuf[y*WIDTH+x]=0x00222222;
        for(int y=dB+1;y<HEIGHT;y++)frameBuf[y*WIDTH+x]=0x00777777;
    }
    // 精灵排序
    double sDist[SPRITE_COUNT];
    for(int i=0;i<SPRITE_COUNT;i++){double dx=sprites[i].x-px,dy=sprites[i].y-py;sDist[i]=dx*dx+dy*dy;g_spriteOrder[i]=i;}
    for(int i=0;i<SPRITE_COUNT-1;i++)for(int j=0;j<SPRITE_COUNT-i-1;j++)
        if(sDist[g_spriteOrder[j]]<sDist[g_spriteOrder[j+1]])std::swap(g_spriteOrder[j],g_spriteOrder[j+1]);
    double id=1.0/(planeX*dirY-dirX*planeY);
    // 精灵渲染
    for(int si=0;si<SPRITE_COUNT;si++){
        int idx=g_spriteOrder[si];
        if(idx<ENEMY_COUNT&&enemyHP[idx]<=0)continue;
        if(idx>=ENEMY_COUNT&&idx<ENEMY_COUNT+HP_COUNT&&!hpActive[idx-ENEMY_COUNT])continue;
        int wIdx=idx-ENEMY_COUNT-HP_COUNT;
        if(wIdx>=0&&!weaponActive[wIdx])continue;
        Sprite& spr=sprites[idx];
        double sX=spr.x-px,sY=spr.y-py;
        double tY=id*(-planeY*sX+planeX*sY);if(tY<=0.1)continue;
        double tX=id*(dirY*sX-dirX*sY);
        int scrX=(int)((WIDTH/2)*(1+tX/tY)),sprH=abs((int)(HEIGHT/tY)),sprW=abs((int)(HEIGHT/tY));
        int dsY=-sprH/2+HEIGHT/2,deY=sprH/2+HEIGHT/2,dsX=-sprW/2+scrX,deX=sprW/2+scrX;
        for(int st=dsX;st<deX;st++){
            int texX=(int)(256*(st-(-sprW/2+scrX))*TEX_SIZE/sprW)/256;
            if(st>=0&&st<WIDTH&&tY<zBuffer[st]){
                for(int y=dsY;y<deY;y++){
                    int texY=(int)(((y-(-sprH/2+HEIGHT/2))*TEX_SIZE)/sprH);
                    if(y>=0&&y<HEIGHT&&texY>=0&&texY<TEX_SIZE){
                        unsigned int c=spr.tex[TEX_SIZE*texY+texX];
                        if((c>>24)==0)continue;
                        double sh=4.0/tY;if(sh>1.0)sh=1.0;
                        int b=((c>>16)&0xFF)*sh,g=((c>>8)&0xFF)*sh,r=(c&0xFF)*sh;
                        frameBuf[y*WIDTH+st]=(255<<24)|(r<<16)|(g<<8)|b;
                    }
                }
            }
        }
    }
    // 子弹 & 火箭渲染
    for(int i=0;i<MAX_BULLETS;i++){
        if(!bullets[i].active)continue;
        double bX=bullets[i].x-px,bY=bullets[i].y-py;
        double tY=id*(-planeY*bX+planeX*bY);if(tY<=0.1)continue;
        double tX=id*(dirY*bX-dirX*bY);
        int scrX=(int)((WIDTH/2)*(1+tX/tY));
        int sz=bullets[i].isRocket?(int)(10.0/tY):(int)(6.0/tY);
        if(sz<1)sz=1;if(sz>15)sz=15;
        unsigned int bClr=bullets[i].isRocket?0xFFFF4400:0xFFFFDD00;
        for(int dy=-sz;dy<=sz;dy++){int py2=HEIGHT/2+dy;if(py2<0||py2>=HEIGHT)continue;
            for(int dx=-sz;dx<=sz;dx++){int px2=scrX+dx;if(px2<0||px2>=WIDTH)continue;
                if(dx*dx+dy*dy<=sz*sz)frameBuf[py2*WIDTH+px2]=bClr;}}
    }
    DrawLaserBeam();
    DrawExplosions();
    DrawEnemyHPBars();
    if(isDead){for(int i=0;i<WIDTH*HEIGHT;i++){unsigned int p=frameBuf[i];int r=((p>>16)&0xFF),g=((p>>8)&0xFF),b=p&0xFF;r=(r+255)/2;g=g/2;b=b/2;frameBuf[i]=(255<<24)|(r<<16)|(g<<8)|b;}}
    if(isWin){for(int i=0;i<WIDTH*HEIGHT;i++){unsigned int p=frameBuf[i];int r=((p>>16)&0xFF),g=((p>>8)&0xFF),b=p&0xFF;r=(r+255)/2;g=(g+255)/2;b=b/3;frameBuf[i]=(255<<24)|(r<<16)|(g<<8)|b;}}
    DrawMinimap();DrawHPBar();
}

// ═══════════════════ UPDATE ═══════════════════
void Update(float dt){
    if(isDead||isWin){
        if(keyState['R']){
            px=1.5;py=1.5;dirX=-1;dirY=0;planeX=0;planeY=0.66;
            isDead=isWin=false;playerHP=playerMaxHP;currentWeapon=WEAPON_PISTOL;
            for(int i=0;i<WEAPON_COUNT;i++)weaponUnlocked[i]=(i==WEAPON_PISTOL);
            fireCooldown=0;laserBeamActive=false;
            for(int i=0;i<HP_COUNT;i++)hpActive[i]=true;
            for(int i=0;i<WEAPON_COUNT_PICKUP;i++)weaponActive[i]=true;
            for(int i=0;i<ENEMY_COUNT;i++){enemyHP[i]=enemyMaxHPArr[i];eWPX[i]=eWPY[i]=-1;eLastPX[i]=eLastPY[i]=-1;}
            for(int i=0;i<MAX_BULLETS;i++)bullets[i].active=false;
            for(int i=0;i<30;i++)explosions[i].active=false;
        }
        return;
    }

    float moveSpd=2.5f*dt;
    if(keyState[VK_UP]||keyState['W']){double nx=px+dirX*moveSpd,ny=py+dirY*moveSpd;if(!IsBlock(nx,py))px=nx;if(!IsBlock(px,ny))py=ny;}
    if(keyState[VK_DOWN]||keyState['S']){double nx=px-dirX*moveSpd,ny=py-dirY*moveSpd;if(!IsBlock(nx,py))px=nx;if(!IsBlock(px,ny))py=ny;}
    if(keyState['A']){double nx=px-planeX*moveSpd,ny=py-planeY*moveSpd;if(!IsBlock(nx,py))px=nx;if(!IsBlock(px,ny))py=ny;}
    if(keyState['D']){double nx=px+planeX*moveSpd,ny=py+planeY*moveSpd;if(!IsBlock(nx,py))px=nx;if(!IsBlock(px,ny))py=ny;}

    // 鼠标(增量追踪)
    if(hasFocus){
        static POINT prevCP={-1,-1}; static bool cpInit=false;
        if(!cpInit){POINT ip;GetCursorPos(&ip);ScreenToClient(win,&ip);prevCP=ip;cpInit=true;}
        POINT cp;GetCursorPos(&cp);ScreenToClient(win,&cp);
        int mdx=cp.x-prevCP.x;prevCP=cp;
        if(abs(mdx)>0&&abs(mdx)<200){float mr=-mdx*MOUSE_SENS;
            double odX=dirX;dirX=dirX*cos(mr)-dirY*sin(mr);dirY=odX*sin(mr)+dirY*cos(mr);
            double opX=planeX;planeX=planeX*cos(mr)-planeY*sin(mr);planeY=opX*sin(mr)+planeY*cos(mr);}
        if(cp.x<40||cp.x>WIDTH-40||cp.y<40||cp.y>HEIGHT-40){
            POINT cc;cc.x=WIDTH/2;cc.y=HEIGHT/2;ClientToScreen(win,&cc);SetCursorPos(cc.x,cc.y);
            prevCP.x=WIDTH/2;prevCP.y=HEIGHT/2;
        }
    }

    // 武器切换 1-7
    if(keyState['1'])currentWeapon=WEAPON_PISTOL;
    if(keyState['2']&&weaponUnlocked[WEAPON_MACHINEGUN])currentWeapon=WEAPON_MACHINEGUN;
    if(keyState['3']&&weaponUnlocked[WEAPON_SHOTGUN])currentWeapon=WEAPON_SHOTGUN;
    if(keyState['4']&&weaponUnlocked[WEAPON_GATLING])currentWeapon=WEAPON_GATLING;
    if(keyState['5']&&weaponUnlocked[WEAPON_LASER])currentWeapon=WEAPON_LASER;
    if(keyState['6']&&weaponUnlocked[WEAPON_SNIPER])currentWeapon=WEAPON_SNIPER;
    if(keyState['7']&&weaponUnlocked[WEAPON_ROCKET])currentWeapon=WEAPON_ROCKET;

    // 计时
    if(laserBeamActive){laserTimer-=dt;if(laserTimer<=0)laserBeamActive=false;}
    for(int i=0;i<30;i++){if(explosions[i].active){explosions[i].timer-=dt;if(explosions[i].timer<=0)explosions[i].active=false;}}

    // 射击
    if(fireCooldown>0)fireCooldown-=dt;
    bool wantFire=mouseDown||keyState[VK_SPACE];
    if(wantFire&&fireCooldown<=0&&!isDead){
        WeaponInfo& wp=weaponStats[currentWeapon];
        fireCooldown=wp.fireRate;
        if(currentWeapon==WEAPON_LASER){
            double rx=px,ry=py,rdx=dirX,rdy=dirY;
            if(fabs(rdx)<1e-10)rdx=(rdx>=0)?1e-10:-1e-10;
            if(fabs(rdy)<1e-10)rdy=(rdy>=0)?1e-10:-1e-10;
            bool hitSth=false;
            for(int step=0;step<80&&!hitSth;step++){
                rx+=rdx*0.25;ry+=rdy*0.25;
                if(IsBlock(rx,ry)){laserEndX=rx;laserEndY=ry;hitSth=true;break;}
                for(int e=0;e<ENEMY_COUNT;e++){if(enemyHP[e]<=0)continue;
                    double edx=rx-sprites[e].x,edy=ry-sprites[e].y;
                    if(sqrt(edx*edx+edy*edy)<0.5){enemyHP[e]-=(float)wp.damage;if(enemyHP[e]<0)enemyHP[e]=0;
                        laserEndX=rx;laserEndY=ry;hitSth=true;break;}
                }
            }
            if(!hitSth){laserEndX=px+dirX*20;laserEndY=py+dirY*20;}
            laserBeamActive=true;laserTimer=0.08f;
        }else{
            for(int p=0;p<wp.pellets;p++){
                int slot=-1;for(int b=0;b<MAX_BULLETS;b++){if(!bullets[b].active){slot=b;break;}}if(slot==-1)break;
                float angle=((float)(rand()%1000)/1000.0f-0.5f)*2.0f*wp.spread;
                double bdx=dirX*cos(angle)-dirY*sin(angle),bdy=dirX*sin(angle)+dirY*cos(angle);
                float spd=wp.isRocket?ROCKET_SPEED:BULLET_SPEED;
                bullets[slot].x=px;bullets[slot].y=py;
                bullets[slot].dx=bdx*spd;bullets[slot].dy=bdy*spd;
                bullets[slot].damage=wp.damage;bullets[slot].life=3.0f;
                bullets[slot].isRocket=wp.isRocket;bullets[slot].blastR=wp.blastR;
                bullets[slot].active=true;
            }
        }
    }

    // 子弹移动 & 碰撞(先敌人后墙壁)
    for(int i=0;i<MAX_BULLETS;i++){
        if(!bullets[i].active)continue;
        bullets[i].x+=bullets[i].dx*dt;bullets[i].y+=bullets[i].dy*dt;bullets[i].life-=dt;
        bool hitEnemy=false;
        for(int e=0;e<ENEMY_COUNT;e++){if(enemyHP[e]<=0)continue;
            double edx=bullets[i].x-sprites[e].x,edy=bullets[i].y-sprites[e].y;
            if(sqrt(edx*edx+edy*edy)<0.45){
                if(bullets[i].isRocket){
                    // 范围爆炸
                    for(int e2=0;e2<ENEMY_COUNT;e2++){if(enemyHP[e2]<=0)continue;
                        double ed2x=bullets[i].x-sprites[e2].x,ed2y=bullets[i].y-sprites[e2].y;
                        double ed2=sqrt(ed2x*ed2x+ed2y*ed2y);
                        if(ed2<bullets[i].blastR){float dmg=bullets[i].damage*(1.0f-(float)(ed2/bullets[i].blastR));enemyHP[e2]-=dmg;if(enemyHP[e2]<0)enemyHP[e2]=0;}
                    }
                    double pd=sqrt((bullets[i].x-px)*(bullets[i].x-px)+(bullets[i].y-py)*(bullets[i].y-py));
                    if(pd<bullets[i].blastR){float dmg=bullets[i].damage*0.4f*(1.0f-(float)(pd/bullets[i].blastR));playerHP-=dmg;if(playerHP<=0){playerHP=0;isDead=true;}}
                    // 爆炸特效
                    for(int ex=0;ex<30;ex++){if(!explosions[ex].active){explosions[ex].x=bullets[i].x;explosions[ex].y=bullets[i].y;explosions[ex].timer=0.35f;explosions[ex].active=true;break;}}
                }else{
                    enemyHP[e]-=(float)bullets[i].damage;if(enemyHP[e]<0)enemyHP[e]=0;
                }
                bullets[i].active=false;hitEnemy=true;break;
            }
        }
        if(hitEnemy)continue;
        if(IsBlock(bullets[i].x,bullets[i].y)){
            if(bullets[i].isRocket){
                for(int e2=0;e2<ENEMY_COUNT;e2++){if(enemyHP[e2]<=0)continue;
                    double ed2x=bullets[i].x-sprites[e2].x,ed2y=bullets[i].y-sprites[e2].y;
                    double ed2=sqrt(ed2x*ed2x+ed2y*ed2y);
                    if(ed2<bullets[i].blastR){float dmg=bullets[i].damage*(1.0f-(float)(ed2/bullets[i].blastR));enemyHP[e2]-=dmg;if(enemyHP[e2]<0)enemyHP[e2]=0;}
                }
                double pd=sqrt((bullets[i].x-px)*(bullets[i].x-px)+(bullets[i].y-py)*(bullets[i].y-py));
                if(pd<bullets[i].blastR){float dmg=bullets[i].damage*0.4f*(1.0f-(float)(pd/bullets[i].blastR));playerHP-=dmg;if(playerHP<=0){playerHP=0;isDead=true;}}
                for(int ex=0;ex<30;ex++){if(!explosions[ex].active){explosions[ex].x=bullets[i].x;explosions[ex].y=bullets[i].y;explosions[ex].timer=0.35f;explosions[ex].active=true;break;}}
            }
            bullets[i].active=false;
        }
        if(bullets[i].life<=0)bullets[i].active=false;
    }

    // ── 敌人 AI (BFS 缓存优化) ──
    float aiSpd=1.75f*dt; int aliveEnemies=0;
    for(int i=0;i<ENEMY_COUNT;i++){
        if(enemyHP[i]<=0)continue; aliveEnemies++;
        double dx=px-sprites[i].x,dy=py-sprites[i].y,dist=sqrt(dx*dx+dy*dy);
        if(dist<DAMAGE_RANGE){playerHP-=enemyDamage[i]*dt;if(playerHP<=0){playerHP=0;isDead=true;break;}}
        if(dist<=0.25)continue;

        int eGX=(int)sprites[i].x,eGY=(int)sprites[i].y,pGX=(int)px,pGY=(int)py;
        // 判断是否需要重新 BFS:首次 / 玩家换格 / 到达路点
        bool needBFS=(eWPX[i]<0)||(eLastPX[i]!=pGX||eLastPY[i]!=pGY)||(eGX==eWPX[i]&&eGY==eWPY[i]);
        if(needBFS){
            bool visited[MAZE_H][MAZE_W]={};int prevX[MAZE_H][MAZE_W],prevY[MAZE_H][MAZE_W];
            int qx[MAZE_W*MAZE_H],qy[MAZE_W*MAZE_H];int head=0,tail=0;
            qx[tail]=eGX;qy[tail]=eGY;tail++;visited[eGY][eGX]=true;prevX[eGY][eGX]=-1;
            bool found=false;int ncx=eGX,ncy=eGY;
            const int dirs[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
            while(head<tail){
                int cx2=qx[head],cy2=qy[head];head++;
                if(cx2==pGX&&cy2==pGY){int tx=cx2,ty=cy2;
                    while(true){int px2=prevX[ty][tx],py2=prevY[ty][tx];if(px2==-1||(px2==eGX&&py2==eGY))break;tx=px2;ty=py2;}
                    ncx=tx;ncy=ty;found=true;break;}
                for(int d=0;d<4;d++){int nx2=cx2+dirs[d][0],ny2=cy2+dirs[d][1];
                    if(nx2>=0&&nx2<MAZE_W&&ny2>=0&&ny2<MAZE_H&&!visited[ny2][nx2]&&maze[ny2][nx2]==0)
                    {visited[ny2][nx2]=true;prevX[ny2][nx2]=cx2;prevY[ny2][nx2]=cy2;qx[tail]=nx2;qy[tail]=ny2;tail++;}}
            }
            if(found){eWPX[i]=ncx;eWPY[i]=ncy;}else{eWPX[i]=pGX;eWPY[i]=pGY;}
            eLastPX[i]=pGX;eLastPY[i]=pGY;
        }
        // 朝路点移动
        double ttx=eWPX[i]+0.5,tty=eWPY[i]+0.5;
        double tdx=ttx-sprites[i].x,tdy=tty-sprites[i].y;
        double td=sqrt(tdx*tdx+tdy*tdy);if(td<0.05)td=0.05;
        double mvX=(tdx/td)*aiSpd,mvY=(tdy/td)*aiSpd;
        if(!IsBlock(sprites[i].x+mvX,sprites[i].y+mvY)){sprites[i].x+=mvX;sprites[i].y+=mvY;}
        else if(!IsBlock(sprites[i].x+mvX,sprites[i].y))sprites[i].x+=mvX;
        else if(!IsBlock(sprites[i].x,sprites[i].y+mvY))sprites[i].y+=mvY;
    }
    if(aliveEnemies==0&&!isDead)isWin=true;

    // 血包
    if(!isDead&&!isWin)for(int i=0;i<HP_COUNT;i++){if(!hpActive[i])continue;
        double hx=px-sprites[ENEMY_COUNT+i].x,hy=py-sprites[ENEMY_COUNT+i].y;
        if(sqrt(hx*hx+hy*hy)<0.5){playerHP+=300;if(playerHP>playerMaxHP)playerHP=playerMaxHP;hpActive[i]=false;}
    }
    // 武器拾取
    if(!isDead&&!isWin)for(int i=0;i<WEAPON_COUNT_PICKUP;i++){if(!weaponActive[i])continue;
        double wx2=px-sprites[ENEMY_COUNT+HP_COUNT+i].x,wy2=py-sprites[ENEMY_COUNT+HP_COUNT+i].y;
        if(sqrt(wx2*wx2+wy2*wy2)<0.5){weaponUnlocked[weaponPickupType[i]]=true;weaponActive[i]=false;}
    }
}
原文