跳到正文

个人记录

关于制作动作类游戏

猎户约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月17日
篇幅
约 9 分钟

版本

  1. v1当前

关于这个呢,我想了好久了,最近才找到有用的代码


类型一:关于动作类,我初学时想都不敢想,因为我就知道cin和scanf,这两种语言输入都要回车,当时做出来的回合制游戏也是不堪回首,被多少人吐槽游戏体验不佳。


类型二:之后,我去找了网上的游戏,发现代码里有一个叫getch的东东,我拿出来用了一下,加上头文件,发现不用输回车,但会有延时,还不能长按。延时后来解决了,因为我一开始多打了一个getch,就像这样:


getch(); char j=getch();


后来解决了,可还是用不了(起码效果不好),也只能用于回合制。


类型三:为了向动作类游戏发展(当时也只能叫实时类), 我找到了一个系统函数——kbhit。这玩意儿可以判断你是否有输入,如果输入了,会返回true,否则——都懂得。可这配上getch还是不太好,延时又出现了,还没法解决,对于动作类游戏来说,它虽比之前的方法都好,但还有缺陷,如果长按,会出现以下情况:


如果没有输入返回0,输入返回3:


000003000030000300003000030000300003000003


这就会导致我们无法准确的判断上一次输入的时间和内容,这样的做法持续了很久,知道今天,我终于从一段代码中提取出了目前最好的处理方法,可以用GetAsyncKeyState(); 我打了一段代码,可以判断它的实用性:```cpp #include<bits/stdc++.h> #include<windows.h> #include<conio.h> using namespace std; bool jump=0; bool down=0; int gs[16]= {0,20}; int csgs;//出现在场地中的怪物数量 int J_cls[100005],J_x,J_y,lll; int cnt=0; int tg_x[16]; int fx=1; int gq_xz; int mouse; int ry=17,rx=4; int cnt_A=0,cnt_D=0,cnt_J=0; int num[16]= {0,1,2,2,3,3,3,3,5,5,5,7,7,9,9,10}; int mapp[30][1000][16]; struct node { //怪物初始化 string name; int x,y; int gx,gzl,gf; int color1; int v;//当v==0时,怪物移动(造成伤害) } gw[105][16]; struct bei { //装备初始化 int kind;//种类 string name; bool on_down;//是否装备 bool y_n;//是否拥有 int zl,xl,mn,fy;//加成 int price;//后期商店里的价格 int step;//等级(品质) } zhuangbei[1005]; struct play { //玩家初始化 string name; int xl,mn,zl,fy,money; int dj,jy;//等级,经验 bei yzb[15];//已装备在身上的 } player; POINT Windowpos() { POINT pt; GetCursorPos(&pt); HWND h=GetForegroundWindow(); ScreenToClient(h,&pt); pt.x/=8; pt.y/=16; return pt; } void color(int a) { HANDLE hou; hou=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hou,a); } void getoxy(int x,int y) { HANDLE hout; COORD pos= {x,y}; hout=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hout,pos); } void HideCursor() { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void wait(double t) { t*=30000; while(—t) { if(kbhit()!=0) getch(); } } void csh() { for(int j=1; j<=15; j++) for(int i=0; i<1000; i++) mapp[18][i][j]=1; for(int i=1; i<=15; i++) { mapp[17][80][i]=99; mapp[16][80][i]=99; tg_x[i]=80; } } void gw_csh() { for(int i=1; i<=20; i++) { gw[i][1].name=""; gw[i][1].color1=15; gw[i][1].gf=2; gw[i][1].gx=50; gw[i][1].gzl=5; gw[i][1].v=200; }

} void Background() { for(int i=0; i<50; i++) for(int j=20; j<30; j++) { color(6); getoxy(i2,j); cout<<”█”; } getoxy(0,22); for(int i=0; i<50; i++) for(int j=18; j<20; j++) { color(2); getoxy(i2,j); cout<<”█”; } } void Win() { system(“cls”); getoxy(35,12); cout<<“过关!!!”; wait(3); system(“cls”); } void GameStart(int c) { int t=0; while(1) { if(c2>csgs) for(int i=csgs+1; i<=c2; i++) { getoxy(80,15); color(gw[i][1].color1); cout<<”█”; } color(10); getoxy(rx,ry); cout<<”█”; if(gs<=0) { color(14); getoxy(tg_x[c],16); cout<<”█”; getoxy(tg_x[c],17); cout<<”█”; } if(jump==1 && mapp[ry-1][rx/2][c]==0 && t%100==0) { getoxy(rx,ry); cout<<” ”; ry—; cnt++; } if(cnt==4) { jump=0; down=1; cnt=0; t=0; } if(down==1 && mapp[ry+1][rx/2][c]==0 && t%100==0) { getoxy(rx,ry); cout<<” ”; ry++; } t++; if(mapp[ry+1][rx/2][c]==1) { t=0; down=0; } aa: if(kbhit()) { if (GetAsyncKeyState(‘K’) && mapp[ry+1][rx/2][c]!=0) jump=1; else if (GetAsyncKeyState(‘W’)) { if(tg_x[c]==rx && ry<=17 && ry>=16 && gs[c]<=0) { Win(); return; } } else if (cnt_D==150) { if(rx+2<=98) { getoxy(rx,ry); cout<<” ”; rx+=2; fx=1; } cnt_D=0; goto aa; } else if (cnt_A==150) { if(rx-2>=0) { getoxy(rx,ry); cout<<” ”; rx-=2; fx=-1; } cnt_A=0; goto aa; } else if (cnt_J==100 && GetAsyncKeyState(‘J’)) { color(10); getoxy(rx+fx4,ry); if(fx==1) cout<<”)”; else cout<<”(”; lll++; J_x=rx+fx4; J_y=ry; J_cls[lll]=80; cnt_J=0; } else if (GetAsyncKeyState(‘D’)) { cnt_D++; } else if (GetAsyncKeyState(‘A’)) { cnt_A++; } if(cnt_J<100) cnt_J++; for(int i=1; i<=lll; i++) { if(J_cls[i]>0) J_cls[i]—; if(J_cls[i]==0) { getoxy(J_x,J_y); cout<<” ”; lll—; } } } } } int main() { srand(time(0)); HideCursor(); gq_xz=1; system(“mode con cols=100 lines=30”); csh(); while(1) { rx=50; ry=17; lll=0; csgs=0; system(“cls”); Background(); GameStart(gq_xz); gq_xz++; } return 0; }

代码
------------

比之前都好多了,不仅可以判断是否长按,还能测时间,保存输入。不过,我还是运用不熟练,毕竟刚会,还得大佬们多指点
(吐槽一句:想要游戏的可以私聊我,但只可娱乐,不可商用,毕竟有一部分不是我原创的)
原文