版本比较
联机小游戏
正文
增加 461 行删除 123 行
37 行未改动3839404142424344454646474849485051525354555657585960616263646566673 行未改动717273746075767778798081828 行未改动91929394779596978098991001011029 行未改动112113114115116117118119120121122123124125126127991281291301311031041051061071081321331341351361374 行未改动1421431441451211221231241251261271281291301311321331461471481491501511521531541551562 行未改动15916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228339 行未改动32332432532619132732832933033133233333433533633733833934034134234334434534634734834935035120120235235335435535635735835936036136236336436536636736836937010 行未改动38138238338422122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225338538638738838939039139239310 行未改动40440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443547 行未改动4834844854863284874884894903324914924934943344954964974984995005015025035045055065075085095105115122 行未改动51551651751834551952052152252352452552629 行未改动5565575585595605615625635645655665675685695705713845725735745753883893903913923935765775785795805814 行未改动5865875885894064074084094104114124134144154164174185905915925935945955965975985996002 行未改动60360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174245 行未改动7887897907914827927937947957967977987998003 行未改动80480580680749480880981081181281381481581681781881982082182282382482582682782882983083150450583283383483583683783883984084184284384484584684784810 行未改动85986086186252452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555686386486586686786886987087110 行未改动88288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691712 行未改动37 行未改动
HANDLE hConsole;int mx,my,tx,ty,omx,omy,otx,oty,ldx,ldy,mhp=10,thp=10;bool fr,rp=true,mr=false,tr=false,go=false,hfa=false;DWORD fs,acd=0,ru=0,selfRu=0,hfs,qcd=0,detectCD=0;DWORD fs,acd=0,ru=0,selfRu=0,hfs;bool speedBoost=false,rayActive=false,normalRayActive=false,previewActive=false;DWORD rayEndTime=0,normalRayEndTime=0,previewEndTime=0;int raySx,raySy,rayDirX,rayDirY,normalRaySx,normalRaySy,normalRayDirX,normalRayDirY;int skillChoice=0,prevSkillType=0;int qSkill=0,eSkill=1;DWORD qCooldownUntil=0,eCooldownUntil=0;int prevSkillType=0;
bool sd(int x,int y){return x>=0&&y>=0&&x<W&&y<H&&MAP[y][x]==0;}// 临时障碍物(光幕)bool tempWall[H][W]={false};DWORD wallTime[H][W]={0};
struct Missile { bool active; int curX,curY,dirX,dirY; DWORD lastMoveTime;};Missile myMissile;
bool sd(int x,int y){ return x>=0&&y>=0&&x<W&&y<H && MAP[y][x]==0 && !tempWall[y][x];}bool sa(int x,int y){return x>=4&&x<=17&&y>=4&&y<=17;}bool rhe(int sx,int sy,int dx,int dy,int ex,int ey){ int x=sx+dx,y=sy+dy; while(sd(x,y)){if(x==ex&&y==ey)return true;x+=dx;y+=dy;}3 行未改动void dc(int x,int y,char c,int col){ SetConsoleCursorPosition(hConsole,{(SHORT)x,(SHORT)y}); SetConsoleTextAttribute(hConsole,col);cout<<c;}void dcell(int x,int y){dc(x,y,MAP[y][x]?'#':' ',7);}void dcell(int x,int y){ if(tempWall[y][x]) dc(x,y,'#',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY); else dc(x,y,MAP[y][x]?'#':' ',7);}
void drawRayLine(int sx,int sy,int dx,int dy,char ch,int col){ int x=sx+dx,y=sy+dy; while(sd(x,y)){dc(x,y,ch,col);x+=dx;y+=dy;}8 行未改动 if(ru&&GetTickCount()<=ru) dc(tx,ty,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); else dcell(tx,ty);}
void showPreview(int st){void showPreview(int skillType){ if(previewActive&&prevSkillType==1&&ldx!=0&&ldy!=0) clearRayLine(mx,my,ldx,ldy); previewActive=false; if(st==1&&ldx!=0&&ldy!=0){ if(skillType==1&&ldx!=0&&ldy!=0){ drawRayLine(mx,my,ldx,ldy,'*',FOREGROUND_GREEN|FOREGROUND_INTENSITY); previewActive=true; previewEndTime=GetTickCount()+500; prevSkillType=1; }}9 行未改动 cout<<string(80,' '); SetConsoleCursorPosition(hConsole,{0,(SHORT)y});}
string skillName(int id){ switch(id){ case 0: return "加速"; case 1: return "射线"; case 2: return "探测"; case 3: return "飞弹"; case 4: return "光幕"; default: return "未知"; }}
void srs(){ for(int i=0;i<5;i++) clsLine(i); for(int i=0;i<6;i++) clsLine(i); SetConsoleCursorPosition(hConsole,{0,0}); cout<<"===== 对战准备阶段 ====="<<endl; cout<<"我方: "<<(mr?"已准备":"未准备")<<" 对方: "<<(tr?"已准备":"未准备")<<endl; cout<<"按 空格 切换准备状态,按 Q 选择技能"<<endl; cout<<"当前技能: "; if(skillChoice==0) cout<<"加速 (向前冲刺3格, CD:4秒)"; else if(skillChoice==1) cout<<"射线 (向前发射射线造成3点伤害, 自身暴露2秒, CD:10秒)"; else cout<<"探测 (检测周围3格内的敌人并暴露3秒, CD:5秒)"; cout<<" "; cout<<"按 空格 切换准备状态,按 Q/E 切换对应技能"<<endl; cout<<"[Q]技能: "<<skillName(qSkill)<<" [E]技能: "<<skillName(eSkill)<<endl;}
void shp(){ if(go) return;4 行未改动 SetConsoleCursorPosition(hConsole,{W+2,1});SetConsoleTextAttribute(hConsole,tc); cout<<"对方 HP: "<<thp<<" "; SetConsoleCursorPosition(hConsole,{W+2,2});SetConsoleTextAttribute(hConsole,FOREGROUND_GREEN|FOREGROUND_INTENSITY); DWORD now=GetTickCount(); string sn; if(skillChoice==0) sn="加速"; else if(skillChoice==1) sn="射线"; else sn="探测"; if(skillChoice==2){ if(detectCD>now) cout<<"[Q]技能: "<<sn<<" ("<<((detectCD-now+999)/1000)<<"s) "; else cout<<"[Q]技能: "<<sn<<" "; }else{ if(qcd>now) cout<<"[Q]技能: "<<sn<<" ("<<((qcd-now+999)/1000)<<"s) "; else if(speedBoost) cout<<"[Q]技能: 加速中 "; else cout<<"[Q]技能: "<<sn<<" "; } string qn=skillName(qSkill), en=skillName(eSkill); if(qCooldownUntil>now) cout<<"[Q]"<<qn<<" ("<<((qCooldownUntil-now+999)/1000)<<"s) "; else if(speedBoost && qSkill==0) cout<<"[Q]加速中 "; else cout<<"[Q]"<<qn<<" "; if(eCooldownUntil>now) cout<<"[E]"<<en<<" ("<<((eCooldownUntil-now+999)/1000)<<"s) "; else if(speedBoost && eSkill==0) cout<<"[E]加速中 "; else cout<<"[E]"<<en<<" "; SetConsoleTextAttribute(hConsole,7);}
void sgo(string w){2 行未改动 cout<<"========== 游戏结束 ==========\n 获胜方: "<<w<<"\n 我方 HP: "<<mhp<<" 对方 HP: "<<thp<<"\n==============================\n"; SetConsoleTextAttribute(hConsole,7);}
bool checkMyMissileHit(SOCKET cli, DWORD now) { if (myMissile.curX == tx && myMissile.curY == ty) { myMissile.active = false; thp -= 4; char bf[256]; sprintf(bf,"[HP|%d|%d]\n",mhp,thp); send(cli,bf,strlen(bf),0); send(cli,"[MISSILE_HIT]\n",14,0); fr=true; fs=now; fsr(BACKGROUND_RED); hfa=true; hfs=now; if(thp<=0){go=true; send(cli,"[GAMEOVER|我方]\n",16,0); sgo("我方");} else shp(); return true; } return false;}
// 放置光幕辅助函数(服务端使用)void placeBarrier(SOCKET cli, DWORD now){ if(ldx==0 && ldy==0) return; int px = mx + 2*ldx; int py = my + 2*ldy; auto canPlace = [](int x,int y)->bool{ return x>=0 && x<W && y>=0 && y<H && MAP[y][x]==0; }; if(!canPlace(px,py)) return; int dx = -ldy, dy = ldx; int pts[3][2] = {{px, py}, {px+dx, py+dy}, {px-dx, py-dy}}; char bf[256]; for(int i=0;i<3;i++){ int x=pts[i][0], y=pts[i][1]; if(canPlace(x,y)){ tempWall[y][x] = true; wallTime[y][x] = now; dcell(x,y); sprintf(bf,"[BARRIER_PLACE|%d|%d]\n",x,y); send(cli,bf,strlen(bf),0); } }}
void removeBarrierAt(SOCKET cli, int x, int y){ if(tempWall[y][x]){ tempWall[y][x] = false; dcell(x,y); char bf[64]; sprintf(bf,"[BARRIER_REMOVE|%d|%d]\n",x,y); send(cli,bf,strlen(bf),0); }}
void useSkill(SOCKET cli, int slot, DWORD now){ int skill = (slot==0) ? qSkill : eSkill; DWORD &cooldown = (slot==0) ? qCooldownUntil : eCooldownUntil; if(now < cooldown) return; char bf[256]; switch(skill){ case 0: cooldown = now + 4000; speedBoost = true; send(cli,"[SPEED_ON]\n",11,0); shp(); break; case 1: if(ldx==0 && ldy==0) return; cooldown = now + 10000; selfRu = now + 2000; send(cli,"[SELF_REVEAL]\n",14,0); raySx=mx; raySy=my; rayDirX=ldx; rayDirY=ldy; drawRayLine(mx,my,ldx,ldy,'*',FOREGROUND_RED|FOREGROUND_INTENSITY); rayActive=true; rayEndTime=now+200; if(rhe(mx,my,ldx,ldy,tx,ty)){ send(cli,"[RAY_HIT]\n",10,0); thp -= 3; sprintf(bf,"[HP|%d|%d]\n",mhp,thp); send(cli,bf,strlen(bf),0); if(thp<=0){go=true; send(cli,"[GAMEOVER|我方]\n",16,0); sgo("我方");} else shp(); }else shp(); break; case 2: cooldown = now + 5000; { int dist = abs(mx-tx)+abs(my-ty); if(dist <= 3){ ru = now + 3000; dcell(otx,oty); dc(tx,ty,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); send(cli,"[DETECT]\n",9,0); } } shp(); break; case 3: if(ldx==0 && ldy==0) return; cooldown = now + 10000; selfRu = now + 800; send(cli,"[SELF_REVEAL]\n",14,0); myMissile.active = true; myMissile.curX = mx + ldx; myMissile.curY = my + ldy; myMissile.dirX = ldx; myMissile.dirY = ldy; myMissile.lastMoveTime = now; if(!sd(myMissile.curX,myMissile.curY)){ myMissile.active = false; }else{ dc(myMissile.curX,myMissile.curY,'*',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY); if(checkMyMissileHit(cli, now)) { shp(); break; } } shp(); break; case 4: if(ldx==0 && ldy==0) return; cooldown = now + 20000; selfRu = now + 1500; send(cli,"[SELF_REVEAL]\n",14,0); placeBarrier(cli, now); shp(); break; }}
int main(){ srand(time(NULL));hConsole=GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO ci={1,FALSE};SetConsoleCursorInfo(hConsole,&ci); WSADATA w;WSAStartup(MAKEWORD(2,2),&w);39 行未改动 }else if(msg=="[DETECT]"){ ru=GetTickCount()+3000; dcell(otx,oty);dc(tx,ty,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); }else if(msg=="[SELF_REVEAL]"){ // 对方(客户端)使用射线,暴露了自己2秒 -> 服务端能看到客户端的位置 ru=GetTickCount()+2000; dcell(otx,oty);dc(tx,ty,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); }else if(msg=="[MISSILE_HIT]"){ fr=true;fs=GetTickCount();fsr(BACKGROUND_RED);hfa=true;hfs=GetTickCount(); shp(); }else if(sscanf(msg.c_str(),"[BARRIER_PLACE|%d|%d]",&x,&y)==2){ // 客户端放置光幕,服务端同步 if(x>=0 && x<W && y>=0 && y<H && MAP[y][x]==0){ tempWall[y][x] = true; wallTime[y][x] = GetTickCount(); dcell(x,y); } }else if(sscanf(msg.c_str(),"[BARRIER_REMOVE|%d|%d]",&x,&y)==2){ // 客户端移除光幕,服务端同步 if(x>=0 && x<W && y>=0 && y<H && tempWall[y][x]){ tempWall[y][x] = false; dcell(x,y); } } } } } if(_kbhit()&&GetTickCount()-lm>120&&!go){ char k=_getch();DWORD nw=GetTickCount(); if(rp){ if(k==' '){mr=!mr;send(cli,mr?"[READY]\n":"[UNREADY]\n",mr?8:11,0);srs();if(mr&&tr){send(cli,"[START]\n",8,0);system("cls");for(int y=0;y<H;++y)for(int x=0;x<W;++x)dcell(x,y);dc(mx,my,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);dcell(tx,ty);omx=mx;omy=my;otx=tx;oty=ty;rp=false;mhp=10;thp=10;shp();}lm=nw;} else if(k=='q'||k=='Q'){skillChoice=(skillChoice+1)%3;srs();showPreview(skillChoice);lm=nw;} if(k==' '){ mr=!mr;send(cli,mr?"[READY]\n":"[UNREADY]\n",mr?8:11,0);srs(); if(mr&&tr){send(cli,"[START]\n",8,0);system("cls");for(int y=0;y<H;++y)for(int x=0;x<W;++x)dcell(x,y);dc(mx,my,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);dcell(tx,ty);omx=mx;omy=my;otx=tx;oty=ty;rp=false;mhp=10;thp=10;shp();} lm=nw; }else if(k=='q'||k=='Q'){ int next = (qSkill+1)%5; while(next == eSkill) next = (next+1)%5; qSkill = next; srs(); showPreview(qSkill); lm=nw; }else if(k=='e'||k=='E'){ int next = (eSkill+1)%5; while(next == qSkill) next = (next+1)%5; eSkill = next; srs(); showPreview(eSkill); lm=nw; } }else{ int nx=mx,ny=my,dx=0,dy=0; if(k=='w'||k=='W'){ny--;dy=-1;} else if(k=='s'||k=='S'){ny++;dy=1;}10 行未改动 if(rhe(mx,my,ldx,ldy,tx,ty)) send(cli,"[HIT]\n",7,0); } lm=nw;continue; }else if(k=='q'||k=='Q'){ if(skillChoice==2){ if(nw>=detectCD){ detectCD=nw+5000; int dist = abs(mx-tx)+abs(my-ty); if(dist <= 3){ ru=GetTickCount()+3000; dcell(otx,oty);dc(tx,ty,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); send(cli,"[DETECT]\n",9,0); } shp(); } }else{ DWORD cd=skillChoice==0?4000:10000; if(nw>=qcd){ qcd=nw+cd; if(skillChoice==0){speedBoost=true;send(cli,"[SPEED_ON]\n",11,0);shp();} else if(ldx!=0||ldy!=0){ // 使用射线:自身暴露2秒 selfRu=nw+2000; send(cli,"[SELF_REVEAL]\n",14,0); raySx=mx;raySy=my;rayDirX=ldx;rayDirY=ldy; drawRayLine(mx,my,ldx,ldy,'*',FOREGROUND_RED|FOREGROUND_INTENSITY); rayActive=true;rayEndTime=nw+200; if(rhe(mx,my,ldx,ldy,tx,ty)){ send(cli,"[RAY_HIT]\n",10,0);thp-=3; sprintf(bf,"[HP|%d|%d]\n",mhp,thp);send(cli,bf,strlen(bf),0); if(thp<=0){go=true;send(cli,"[GAMEOVER|我方]\n",16,0);sgo("我方");} else shp(); }else shp(); }else shp(); } } useSkill(cli,0,nw); lm=nw;continue; }else if(k=='e'||k=='E'){ useSkill(cli,1,nw); lm=nw;continue; }else continue;
if(speedBoost){ int sx=dx,sy=dy,ms=0;10 行未改动 } } if(!rp&&!go){ DWORD now=GetTickCount(); // 检查临时墙超时 for(int y=0;y<H;y++){ for(int x=0;x<W;x++){ if(tempWall[y][x] && now - wallTime[y][x] >= 20000){ removeBarrierAt(cli,x,y); } } } if(myMissile.active){ if(now - myMissile.lastMoveTime >= 180){ dcell(myMissile.curX,myMissile.curY); int newX = myMissile.curX + myMissile.dirX; int newY = myMissile.curY + myMissile.dirY; if(sd(newX,newY)){ myMissile.curX = newX; myMissile.curY = newY; if(checkMyMissileHit(cli, now)) continue; dc(myMissile.curX,myMissile.curY,'*',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY); myMissile.lastMoveTime = now; }else{ myMissile.active = false; } } } if(fr&&now-fs>200){fr=false;fsr(0);for(int y=0;y<H;++y)for(int x=0;x<W;++x)dcell(x,y);redrawPlayers();shp();} if(hfa&&now-hfs>300){hfa=false;shp();} if(ru&&now<=ru) dc(tx,ty,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); else if(ru&&now>ru){ru=0;dcell(tx,ty);}47 行未改动
HANDLE hConsole;int mx,my,ex,ey,omx,omy,oex,oey,ldx=0,ldy=0,mhp=10,ehp=10;bool fr,rp=true,mr=false,er=false,gs=false,go=false,hfa=false;DWORD fs,acd=0,ru=0,selfRu=0,hfs,qcd=0,detectCD=0;DWORD fs,acd=0,ru=0,selfRu=0,hfs;bool speedBoost=false,rayActive=false,normalRayActive=false,previewActive=false;DWORD rayEndTime=0,normalRayEndTime=0,previewEndTime=0;int raySx,raySy,rayDirX,rayDirY,normalRaySx,normalRaySy,normalRayDirX,normalRayDirY;int skillChoice=0,prevSkillType=0;int qSkill=0,eSkill=1;DWORD qCooldownUntil=0,eCooldownUntil=0;int prevSkillType=0;
bool sd(int x,int y){return x>=0&&y>=0&&x<W&&y<H&&MAP[y][x]==0;}// 临时障碍物(光幕)bool tempWall[H][W]={false};DWORD wallTime[H][W]={0};
struct Missile { bool active; int curX,curY,dirX,dirY; DWORD lastMoveTime;};Missile myMissile;
bool sd(int x,int y){ return x>=0&&y>=0&&x<W&&y<H && MAP[y][x]==0 && !tempWall[y][x];}bool rhe(int sx,int sy,int dx,int dy,int ex,int ey){ int x=sx+dx,y=sy+dy; while(sd(x,y)){if(x==ex&&y==ey)return true;x+=dx;y+=dy;} return false;2 行未改动void dc(int x,int y,char c,int col){ SetConsoleCursorPosition(hConsole,{(SHORT)x,(SHORT)y}); SetConsoleTextAttribute(hConsole,col);cout<<c;}void dcell(int x,int y){dc(x,y,MAP[y][x]?'#':' ',7);}void dcell(int x,int y){ if(tempWall[y][x]) dc(x,y,'#',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY); else dc(x,y,MAP[y][x]?'#':' ',7);}
void drawRayLine(int sx,int sy,int dx,int dy,char ch,int col){ int x=sx+dx,y=sy+dy; while(sd(x,y)){dc(x,y,ch,col);x+=dx;y+=dy;}29 行未改动 cout<<string(80,' '); SetConsoleCursorPosition(hConsole,{0,(SHORT)y});}
string skillName(int id){ switch(id){ case 0: return "加速"; case 1: return "射线"; case 2: return "探测"; case 3: return "飞弹"; case 4: return "光幕"; default: return "未知"; }}
void srs(){ clsLine(0); clsLine(1); clsLine(2); clsLine(3); clsLine(4); clsLine(0); clsLine(1); clsLine(2); clsLine(3); clsLine(4); clsLine(5); SetConsoleCursorPosition(hConsole,{0,0}); cout<<"===== 对战准备阶段 ====="<<endl; cout<<"我方: "<<(mr?"已准备":"未准备")<<" 对方: "<<(er?"已准备":"未准备")<<endl; cout<<"按 空格 切换准备状态,按 Q 切换技能"<<endl; cout<<"当前技能: "; if(skillChoice==0) cout<<"加速 (向前冲刺3格, CD:4秒)"; else if(skillChoice==1) cout<<"射线 (向前发射射线造成3点伤害, 自身暴露2秒, CD:10秒)"; else cout<<"探测 (检测周围3格内的敌人并暴露3秒, CD:5秒)"; cout<<" "; cout<<"按 空格 切换准备状态,按 Q/E 切换对应技能"<<endl; cout<<"[Q]技能: "<<skillName(qSkill)<<" [E]技能: "<<skillName(eSkill)<<endl;}
void shp(){ if(go) return;4 行未改动 SetConsoleCursorPosition(hConsole,{W+2,1});SetConsoleTextAttribute(hConsole,ec); cout<<"对方 HP: "<<ehp<<" "; SetConsoleCursorPosition(hConsole,{W+2,2});SetConsoleTextAttribute(hConsole,FOREGROUND_GREEN|FOREGROUND_INTENSITY); DWORD now=GetTickCount(); string sn; if(skillChoice==0) sn="加速"; else if(skillChoice==1) sn="射线"; else sn="探测"; if(skillChoice==2){ if(detectCD>now) cout<<"[Q]技能: "<<sn<<" ("<<((detectCD-now+999)/1000)<<"s) "; else cout<<"[Q]技能: "<<sn<<" "; }else{ if(qcd>now) cout<<"[Q]技能: "<<sn<<" ("<<((qcd-now+999)/1000)<<"s) "; else if(speedBoost) cout<<"[Q]技能: 加速中 "; else cout<<"[Q]技能: "<<sn<<" "; } string qn=skillName(qSkill), en=skillName(eSkill); if(qCooldownUntil>now) cout<<"[Q]"<<qn<<" ("<<((qCooldownUntil-now+999)/1000)<<"s) "; else if(speedBoost && qSkill==0) cout<<"[Q]加速中 "; else cout<<"[Q]"<<qn<<" "; if(eCooldownUntil>now) cout<<"[E]"<<en<<" ("<<((eCooldownUntil-now+999)/1000)<<"s) "; else if(speedBoost && eSkill==0) cout<<"[E]加速中 "; else cout<<"[E]"<<en<<" "; SetConsoleTextAttribute(hConsole,7);}
void sgo(string w){2 行未改动 cout<<"========== 游戏结束 ==========\n 获胜方: "<<w<<"\n 我方 HP: "<<mhp<<" 对方 HP: "<<ehp<<"\n==============================\n"; SetConsoleTextAttribute(hConsole,7);}
bool checkMyMissileHit(SOCKET sk, DWORD now) { if (myMissile.curX == ex && myMissile.curY == ey) { myMissile.active = false; ehp -= 4; char bf[256]; sprintf(bf,"[HP|%d|%d]\n",mhp,ehp); send(sk,bf,strlen(bf),0); send(sk,"[MISSILE_HIT]\n",14,0); fr=true; fs=now; fsr(BACKGROUND_RED); hfa=true; hfs=now; if(ehp<=0){go=true; send(sk,"[DEAD]\n",7,0); sgo("我方");} else shp(); return true; } return false;}
// 本地放置光幕(仅本地视觉,同时通知服务端)void placeBarrierLocal(DWORD now){ if(ldx==0 && ldy==0) return; int px = mx + 2*ldx; int py = my + 2*ldy; auto canPlace = [](int x,int y)->bool{ return x>=0 && x<W && y>=0 && y<H && MAP[y][x]==0; }; if(!canPlace(px,py)) return; int dx = -ldy, dy = ldx; int pts[3][2] = {{px, py}, {px+dx, py+dy}, {px-dx, py-dy}}; for(int i=0;i<3;i++){ int x=pts[i][0], y=pts[i][1]; if(canPlace(x,y)){ tempWall[y][x] = true; wallTime[y][x] = now; dcell(x,y); } }}
// 本地移除光幕void removeBarrierLocal(int x, int y){ if(tempWall[y][x]){ tempWall[y][x] = false; dcell(x,y); }}
void useSkill(SOCKET sk, int slot, DWORD now){ int skill = (slot==0) ? qSkill : eSkill; DWORD &cooldown = (slot==0) ? qCooldownUntil : eCooldownUntil; if(now < cooldown) return; char bf[256]; switch(skill){ case 0: cooldown = now + 4000; speedBoost = true; send(sk,"[SPEED_ON]\n",11,0); shp(); break; case 1: if(ldx==0 && ldy==0) return; cooldown = now + 10000; selfRu = now + 2000; send(sk,"[SELF_REVEAL]\n",14,0); raySx=mx; raySy=my; rayDirX=ldx; rayDirY=ldy; drawRayLine(mx,my,ldx,ldy,'*',FOREGROUND_RED|FOREGROUND_INTENSITY); rayActive=true; rayEndTime=now+200; if(rhe(mx,my,ldx,ldy,ex,ey)){ send(sk,"[RAY_HIT]\n",10,0); ehp -= 3; sprintf(bf,"[HP|%d|%d]\n",mhp,ehp); send(sk,bf,strlen(bf),0); if(ehp<=0){go=true; send(sk,"[DEAD]\n",7,0); sgo("我方");} else shp(); }else shp(); break; case 2: cooldown = now + 5000; { int dist = abs(mx-ex)+abs(my-ey); if(dist <= 3){ ru = now + 3000; dcell(oex,oey); dc(ex,ey,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); send(sk,"[DETECT]\n",9,0); } } shp(); break; case 3: if(ldx==0 && ldy==0) return; cooldown = now + 10000; selfRu = now + 800; send(sk,"[SELF_REVEAL]\n",14,0); myMissile.active = true; myMissile.curX = mx + ldx; myMissile.curY = my + ldy; myMissile.dirX = ldx; myMissile.dirY = ldy; myMissile.lastMoveTime = now; if(!sd(myMissile.curX,myMissile.curY)){ myMissile.active = false; }else{ dc(myMissile.curX,myMissile.curY,'*',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY); if(checkMyMissileHit(sk, now)) { shp(); break; } } shp(); break; case 4: if(ldx==0 && ldy==0) return; cooldown = now + 20000; selfRu = now + 1500; send(sk,"[SELF_REVEAL]\n",14,0); placeBarrierLocal(now); // 通知服务端放置的光幕点 { int px = mx + 2*ldx; int py = my + 2*ldy; auto canPlace = [](int x,int y)->bool{ return x>=0 && x<W && y>=0 && y<H && MAP[y][x]==0; }; if(canPlace(px,py)){ int dx = -ldy, dy = ldx; int pts[3][2] = {{px, py}, {px+dx, py+dy}, {px-dx, py-dy}}; for(int i=0;i<3;i++){ int x=pts[i][0], y=pts[i][1]; if(canPlace(x,y)){ char buf[64]; sprintf(buf,"[BARRIER_PLACE|%d|%d]\n",x,y); send(sk,buf,strlen(buf),0); } } } } shp(); break; }}
int main(){ hConsole=GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO ci={1,FALSE};SetConsoleCursorInfo(hConsole,&ci); WSADATA w;WSAStartup(MAKEWORD(2,2),&w);45 行未改动 }else if(sscanf(msg.c_str(),"[REVEAL|%d|%d]",&x,&y)==2){ dcell(oex,oey); ex=x; ey=y; oex=ex; oey=ey; ru=GetTickCount()+1000; dc(ex,ey,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); }else if(sscanf(msg.c_str(),"[GAMEOVER|%[^]]]",bf)==1){go=true; sgo(string(bf));} else if(sscanf(msg.c_str(),"[HP|%d|%d]",&x,&y)==2){ehp=x; mhp=y; shp();} else if(sscanf(msg.c_str(),"[HP|%d|%d]",&x,&y)==2){ ehp = x; mhp = y; shp(); } else if(msg=="[SPEED_ON]"){speedBoost=true; shp();} else if(msg=="[SPEED_OFF]"){speedBoost=false; shp();} else if(msg=="[RAY_HIT]"){ mhp-=3; fr=true; fs=GetTickCount(); fsr(BACKGROUND_RED); hfa=true; hfs=GetTickCount();3 行未改动 }else if(msg=="[DETECT]"){ ru=GetTickCount()+3000; dcell(oex,oey);dc(ex,ey,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); }else if(msg=="[SELF_REVEAL]"){ // 对方(服务端)使用射线,暴露了自己2秒 -> 客户端能看到服务端的位置 ru=GetTickCount()+2000; dcell(oex,oey);dc(ex,ey,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); }else if(msg=="[MISSILE_HIT]"){ fr=true; fs=GetTickCount(); fsr(BACKGROUND_RED); hfa=true; hfs=GetTickCount(); shp(); }else if(sscanf(msg.c_str(),"[BARRIER_PLACE|%d|%d]",&x,&y)==2){ // 服务端广播放置光幕 if(x>=0 && x<W && y>=0 && y<H && MAP[y][x]==0){ tempWall[y][x] = true; wallTime[y][x] = GetTickCount(); dcell(x,y); } }else if(sscanf(msg.c_str(),"[BARRIER_REMOVE|%d|%d]",&x,&y)==2){ // 服务端广播移除光幕 if(x>=0 && x<W && y>=0 && y<H){ removeBarrierLocal(x,y); } } } } } if(_kbhit()&&GetTickCount()-lm>120&&!go){ char k=_getch(); DWORD nw=GetTickCount(); if(rp){ if(k==' '){mr=!mr; send(sk,mr?"[READY]\n":"[UNREADY]\n",mr?8:11,0); srs(); lm=nw;} else if(k=='q'||k=='Q'){skillChoice=(skillChoice+1)%3; srs(); showPreview(skillChoice); lm=nw;} if(k==' '){ mr=!mr; send(sk,mr?"[READY]\n":"[UNREADY]\n",mr?8:11,0); srs(); lm=nw; }else if(k=='q'||k=='Q'){ int next = (qSkill+1)%5; while(next == eSkill) next = (next+1)%5; qSkill = next; srs(); showPreview(qSkill); lm=nw; }else if(k=='e'||k=='E'){ int next = (eSkill+1)%5; while(next == qSkill) next = (next+1)%5; eSkill = next; srs(); showPreview(eSkill); lm=nw; } }else{ int nx=mx,ny=my,dx=0,dy=0; if(k=='w'||k=='W'){ny--; dy=-1;} else if(k=='s'||k=='S'){ny++; dy=1;}10 行未改动 if(rhe(mx,my,ldx,ldy,ex,ey)) send(sk,"[HIT]\n",7,0); } lm=nw; continue; }else if(k=='q'||k=='Q'){ if(skillChoice==2){ if(nw>=detectCD){ detectCD=nw+5000; int dist = abs(mx-ex)+abs(my-ey); if(dist <= 3){ ru=GetTickCount()+3000; dcell(oex,oey);dc(ex,ey,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); send(sk,"[DETECT]\n",9,0); } shp(); } }else{ DWORD cd=skillChoice==0?4000:10000; if(nw>=qcd){ qcd=nw+cd; if(skillChoice==0){speedBoost=true; send(sk,"[SPEED_ON]\n",11,0); shp();} else if(ldx!=0||ldy!=0){ // 使用射线:自身暴露2秒 selfRu=nw+2000; send(sk,"[SELF_REVEAL]\n",14,0); raySx=mx; raySy=my; rayDirX=ldx; rayDirY=ldy; drawRayLine(mx,my,ldx,ldy,'*',FOREGROUND_RED|FOREGROUND_INTENSITY); rayActive=true; rayEndTime=nw+200; if(rhe(mx,my,ldx,ldy,ex,ey)){ send(sk,"[RAY_HIT]\n",10,0); ehp-=3; sprintf(bf,"[HP|%d|%d]\n",mhp,ehp); send(sk,bf,strlen(bf),0); if(ehp<=0){go=true; send(sk,"[DEAD]\n",7,0); sgo("我方");} else shp(); }else shp(); }else shp(); } } useSkill(sk,0,nw); lm=nw; continue; }else if(k=='e'||k=='E'){ useSkill(sk,1,nw); lm=nw; continue; }else continue;
if(speedBoost){ int sx=dx,sy=dy,ms=0;10 行未改动 } } if(gs&&!go){ DWORD now=GetTickCount(); // 本地检查临时墙超时,并通知服务端 for(int y=0;y<H;y++){ for(int x=0;x<W;x++){ if(tempWall[y][x] && now - wallTime[y][x] >= 20000){ removeBarrierLocal(x,y); // 通知服务端移除 char buf[64]; sprintf(buf,"[BARRIER_REMOVE|%d|%d]\n",x,y); send(sk,buf,strlen(buf),0); } } } if(myMissile.active){ if(now - myMissile.lastMoveTime >= 180){ dcell(myMissile.curX,myMissile.curY); int newX = myMissile.curX + myMissile.dirX; int newY = myMissile.curY + myMissile.dirY; if(sd(newX,newY)){ myMissile.curX = newX; myMissile.curY = newY; if(checkMyMissileHit(sk, now)) continue; dc(myMissile.curX,myMissile.curY,'*',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY); myMissile.lastMoveTime = now; }else{ myMissile.active = false; } } } if(fr&&now-fs>200){fr=false; fsr(0); for(int y=0;y<H;++y) for(int x=0;x<W;++x) dcell(x,y); redrawPlayers(); shp();} if(hfa&&now-hfs>300){hfa=false; shp();} if(ru&&now<=ru) dc(ex,ey,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); else if(ru&&now>ru){ru=0; dcell(ex,ey);}12 行未改动