news 2026/5/23 20:56:20

easyx按键游戏

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
easyx按键游戏

好久没编个像样的程序,手操编了一下,再不练一下脑袋就生锈了

为什么选awds这几个按键(玩游戏的都懂)

#include <graphics.h>//引用图形库头文件 #include <conio.h> #include <Windows.h> #include <mmsystem.h> #include <exception>//异常处理头文件 #include <fstream>//文件操作 #include <string.h>//字符串处理 #include <iostream> #include <ctime> #include <stdlib.h> #pragma comment(lib,"winmm.lib") // 添加互斥锁句柄 HANDLE hMutex; bool bgmEnabled=true;//bgm默认开启 int difficulty=1;//默认简单模式 double speed=1;//默认速度 int score=0;//得分 bool keyA=false,keyW=false,keyD=false,keyS=false;//按键状态 int left,top,right,bottom;//按键位置 DWORD timeA=0,timeW=0,timeD=0,timeS=0;//按键生成时间记录 bool gameRunning=true;//游戏是否继续 char howToEnd[20];//失败形式 char whichKeyFail;//哪一个按键导致的失败 bool gameEnd=false;//判断游戏是否结束 struct KeyRect{//存储每个按键位置的矩形区域 int left, top, right, bottom; }rectA,rectW,rectD,rectS; void gameOver(char howToEnd[],char whichKeyFail){//游戏结束 if(gameEnd) return;//防止反复调用 gameEnd=true; setfillcolor(BLACK); setlinecolor(BLACK); fillrectangle(10,100,500,450); //清除剩余按键 if(whichKeyFail=='a'){ setfillcolor(RED); setlinecolor(BLACK); fillrectangle(100,250,150,300); outtextxy(100+15,250+8,whichKeyFail); } if(whichKeyFail=='w'){ setfillcolor(RED); setlinecolor(BLACK); fillrectangle(225,150,275,200); outtextxy(225+15,150+8,whichKeyFail); } if(whichKeyFail=='d'){ setfillcolor(RED); setlinecolor(BLACK); fillrectangle(350,250,400,300); outtextxy(350+15,250+8,whichKeyFail); } if(whichKeyFail=='s'){ setfillcolor(RED); setlinecolor(BLACK); fillrectangle(225,250,275,300); outtextxy(225+15,250+8,whichKeyFail); } if(strcmp(howToEnd,"timeout")==0){//游戏结束以时间耗尽结束 settextstyle(35,25,"微软雅黑"); settextcolor(RED); char failc[]="Time Out! You Fail!"; outtextxy(20,70,failc);//显示失败标签 } if(strcmp(howToEnd,"fatfinger")==0){//游戏结束以按错键结束 settextstyle(35,25,"微软雅黑"); settextcolor(RED); char failc[]="Fat Finger! You Fail!"; outtextxy(10,70,failc);//显示失败标签 } settextstyle(25,15,"微软雅黑"); char restartc[]="Press Any Key To Esc"; outtextxy(50,110,restartc);//显示退出标签 } void graphLoad(){ setfillcolor(BLACK); setlinecolor(BLACK); fillrectangle(0,10,200,50); //用黑色背景覆盖原score settextstyle(30,15,"微软雅黑");//设置字体格式 setfillcolor(WHITE); settextcolor(WHITE); char sc[]="Score:"; char scInt[50]; itoa(score,scInt,10);//将int转为char数组 outtextxy(45,10,sc);//score标签 outtextxy(145,10,scInt);//得分栏 char dc[]="Difficulty:"; char dInt[10]; itoa(difficulty,dInt,10); outtextxy(245,10,dc);//difficulty标签 outtextxy(395,10,dInt);//难度栏 } void gameConfig(){//游戏配置 FILE* file=fopen("config.txt","r");//打开配置文件 if(file==NULL){ return;//文件不存在,退出配置读取采用默认值 } int bgmValue,diffValue; fscanf(file,"%d",&bgmValue);//读取第一行bgm fscanf(file,"%d",&diffValue);//读取第二行difficulty bgmEnabled=(bgmValue==1); difficulty=diffValue; fclose(file); if(difficulty==2){ speed=0.7;//中等难度 } if(difficulty==3){ speed=0.5;//困难难度 } if(difficulty==4){ speed=0.1;//外挂难度 } } void loadMusic(){//载入音乐 if (!bgmEnabled){//如果bgmEnabled为false则不载入music return; } try{ mciSendString("open \"bgm.mp3\" type mpegvideo alias myMusic",NULL,0,NULL);//打开音频文件 mciSendString("play myMusic repeat",NULL,0,NULL);//设置循环播放 }catch(std::exception& e){//标准异常类要全小写 ; } } void generateOne(char one){ if (one=='a'){//a按键位置 left=100; top=250; right=150; bottom=300; rectA.left = left; rectA.top = top; rectA.right = right; rectA.bottom = bottom; //保存a位置 } if (one=='w'){//w按键位置 left=225; top=150; right=275; bottom=200; rectW.left = left; rectW.top = top; rectW.right = right; rectW.bottom = bottom; //保存w位置 } if (one=='d'){//d按键位置 left=350; top=250; right=400; bottom=300; rectD.left = left; rectD.top = top; rectD.right = right; rectD.bottom = bottom; //保存d位置 } if (one=='s'){//s按键位置 left=225; top=250; right=275; bottom=300; rectS.left = left; rectS.top = top; rectS.right = right; rectS.bottom = bottom; //保存a位置 } setfillcolor(WHITE);//填充颜色 setlinecolor(BLACK);//边框颜色 fillrectangle(left,top,right,bottom);//填充框架 setbkmode(TRANSPARENT); settextcolor(BLACK);//文本颜色 outtextxy(left+15,top+8,one);//按键字符 } DWORD WINAPI thinkGenerateKey(LPVOID lpParam){//判断创建awds按键函数 char awdsArr[]={'a','w','d','s'};//创建awds数组 char remainingKeys[4];//存储还未生成的按键 int remainingCount;//剩余按键数量 srand((unsigned)time(NULL));//设置随机种子 while(gameRunning){ Sleep(speed*1000);//每次刷新按键等待时间 if(!gameRunning){//再次检查游戏是否还在运行(防止最后生成一个按键) break; } WaitForSingleObject(hMutex, INFINITE); // 加锁 remainingCount=0; if(!keyA) remainingKeys[remainingCount++]='a'; if(!keyW) remainingKeys[remainingCount++]='w'; if(!keyD) remainingKeys[remainingCount++]='d'; if(!keyS) remainingKeys[remainingCount++]='s'; if(remainingCount==0){ ReleaseMutex(hMutex); continue;//如果所有按键都生成了,跳过,等待玩家按完其他键 } int randomIndex=rand()%remainingCount; char generatedKey=remainingKeys[randomIndex]; //从未生成的按键中随机选一个 switch(generatedKey){//生成按钮的时候记录时间 case 'a':keyA=true;generateOne('a');timeA=GetTickCount();break; case 'w':keyW=true;generateOne('w');timeW=GetTickCount();break; case 'd':keyD=true;generateOne('d');timeD=GetTickCount();break; case 's':keyS=true;generateOne('s');timeS=GetTickCount();break; } ReleaseMutex(hMutex); // 解锁 } return NULL;//退出线程 } void removeKey(char one){ WaitForSingleObject(hMutex, INFINITE); setfillcolor(BLACK); setlinecolor(BLACK); //用黑色背景覆盖按键 switch(one){ case 'a': fillrectangle(rectA.left,rectA.top,rectA.right,rectA.bottom); break; case 'w': fillrectangle(rectW.left,rectW.top,rectW.right,rectW.bottom); break; case 's': fillrectangle(rectS.left,rectS.top,rectS.right,rectS.bottom); break; case 'd': fillrectangle(rectD.left,rectD.top,rectD.right,rectD.bottom); break; } ReleaseMutex(hMutex); } DWORD WINAPI timeOutFail(LPVOID IpParam){//时间耗尽失败 while(gameRunning){ Sleep(50);//每50ms检查一次 WaitForSingleObject(hMutex, INFINITE); DWORD currentTime=GetTickCount();//记录现在时间(必须用DWORD,因为GetTickCount返回的是DWORD32位无符号整数) DWORD timeOut=(DWORD)(speed*3000);//超时时间=speedx3 if(keyA && timeA!=0 && (currentTime-timeA)>timeOut){ whichKeyFail='a'; gameRunning=false; ReleaseMutex(hMutex); break; } if(keyW && timeW!=0 && (currentTime-timeW)>timeOut){ whichKeyFail='w'; gameRunning=false; ReleaseMutex(hMutex); break; } if(keyD && timeD!=0 && (currentTime-timeD)>timeOut){ whichKeyFail='d'; gameRunning=false; ReleaseMutex(hMutex); break; } if(keyS && timeS!=0 && (currentTime-timeS)>timeOut){ whichKeyFail='s'; gameRunning=false; ReleaseMutex(hMutex); break; } ReleaseMutex(hMutex); //如果超时,退出游戏继续循环 } if(howToEnd[0]=='\0'){ strcpy(howToEnd,"timeout"); gameOver(howToEnd,whichKeyFail);//跳转到游戏结束画面 } return NULL; } DWORD WINAPI checkKeyPress(LPVOID lpParam){//检测按键按下状态 while(gameRunning){ if(_kbhit()){ char key=_getch();//获取键盘输入 WaitForSingleObject(hMutex, INFINITE); switch(key){ case 'a': case 'A': if(keyA){ keyA=false; removeKey('a');//删除a按键状态 score+=1;//加1分 graphLoad(); }else{//按了不存在的a键 whichKeyFail='a'; strcpy(howToEnd,"fatfinger"); gameRunning=false; ReleaseMutex(hMutex); gameOver(howToEnd,whichKeyFail); return NULL; } break; case 'w': case 'W': if(keyW){ keyW=false; removeKey('w');//删除w按键状态 score+=1;//加1分 graphLoad(); }else{//按了不存在的w键 whichKeyFail='w'; strcpy(howToEnd,"fatfinger"); gameRunning=false; ReleaseMutex(hMutex); gameOver(howToEnd,whichKeyFail); return NULL; } break; case 'd': case 'D': if(keyD){ keyD=false; removeKey('d');//删除d按键状态 score+=1;//加1分 graphLoad(); }else{//按了不存在的d键 whichKeyFail='d'; strcpy(howToEnd,"fatfinger"); gameRunning=false; ReleaseMutex(hMutex); gameOver(howToEnd,whichKeyFail); return NULL; } break; case 's': case 'S': if(keyS){ keyS=false; removeKey('s');//删除s按键状态 score+=1;//加1分 graphLoad(); }else{//按了不存在的s键 whichKeyFail='s'; strcpy(howToEnd,"fatfinger"); gameRunning=false; ReleaseMutex(hMutex); gameOver(howToEnd,whichKeyFail); return NULL; } break; case 27: gameRunning=false; ReleaseMutex(hMutex); return NULL; } ReleaseMutex(hMutex); } Sleep(10); } return NULL; } int main() { initgraph(500, 400,NULL);//创建绘图窗口,大小为500x400像素 // 创建互斥锁 hMutex = CreateMutex(NULL, FALSE, NULL); gameConfig();//调用gameConfig函数 loadMusic();//调用loadMusic函数 graphLoad();//调用graphLoad函数 // 创建Windows线程 HANDLE hThinkThread=CreateThread(NULL,0,thinkGenerateKey,NULL,0,NULL);//创建thinkGenerateKey的线程 HANDLE hTimeFailThread=CreateThread(NULL,0,timeOutFail,NULL,0,NULL);//创建timeOutFail的线程 HANDLE hCheckThread=CreateThread(NULL,0,checkKeyPress, NULL,0,NULL);//创建checkKeyPress的线程 // 等待线程结束 WaitForSingleObject(hThinkThread,INFINITE); WaitForSingleObject(hTimeFailThread,INFINITE); WaitForSingleObject(hCheckThread,INFINITE); // 关闭线程句柄 CloseHandle(hThinkThread); CloseHandle(hTimeFailThread); CloseHandle(hCheckThread); CloseHandle(hMutex); _getch();//按任意键继续 closegraph();//关闭绘图窗口 return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/23 20:54:57

MPC5604B/C 信号与引脚全解|硬件 / 底层必看

一、前言 本章主要说明每个引脚叫什么、干什么、上电默认状态、是什么电气类型、复用哪些功能。包含 封装引脚分布(64/100/144LQFP、208MAPBGA) 电源 / 地 / 复位 / 晶振 / JTAG 引脚 引脚电气类型(S/M/F/I/J/X) 复位期间引脚状态 所有 GPIO 的复用功能 AF0~AF3 引脚与外设…

作者头像 李华
网站建设 2026/5/23 20:54:10

医疗AI Agent落地真相(三甲医院内部白皮书首度流出):合规性卡点、NLP模型微调阈值与真实ROI测算模型

更多请点击&#xff1a; https://codechina.net 第一章&#xff1a;医疗AI Agent行业应用全景概览 医疗AI Agent正从单点辅助工具演进为具备感知、推理、决策与执行能力的临床协作者&#xff0c;深度嵌入诊疗全流程。其核心价值在于弥合信息孤岛、降低人为误差、提升资源调度效…

作者头像 李华
网站建设 2026/5/23 20:48:45

3步解决显卡驱动残留:Display Driver Uninstaller技术深度解析

3步解决显卡驱动残留&#xff1a;Display Driver Uninstaller技术深度解析 【免费下载链接】display-drivers-uninstaller Display Driver Uninstaller (DDU) a driver removal utility / cleaner utility 项目地址: https://gitcode.com/gh_mirrors/di/display-drivers-unin…

作者头像 李华
网站建设 2026/5/23 20:39:05

如何高效下载B站视频:Python开源工具bilibili-downloader完全指南

如何高效下载B站视频&#xff1a;Python开源工具bilibili-downloader完全指南 【免费下载链接】bilibili-downloader B站视频下载&#xff0c;支持下载大会员清晰度4K&#xff0c;持续更新中 项目地址: https://gitcode.com/gh_mirrors/bil/bilibili-downloader B站视频…

作者头像 李华
网站建设 2026/5/23 20:39:02

锂离子电池阻抗测试全解析:从静态EIS到动态DEIS实战指南

1. 锂离子电池阻抗测试&#xff1a;从静态到动态的深度解析在锂离子电池的研发、生产与状态评估中&#xff0c;仅仅依靠电压、电流和容量这些宏观参数&#xff0c;就像医生只给病人量体温和血压&#xff0c;很难诊断出内部器官的具体病灶。电池内部的电荷转移、离子扩散、界面反…

作者头像 李华
网站建设 2026/5/23 20:27:32

为什么很多企业买三维扫描设备之前问“多少钱”,用了一段时间后开始问“值不值”?

在工业设备采购过程中&#xff0c;价格通常是最先被讨论的问题之一。三维扫描项目也不例外。很多企业在前期接触时&#xff0c;最关心的是预算、配置和采购成本&#xff0c;希望快速判断投入范围。 但在项目真正运行几个月之后&#xff0c;现场关注的问题往往会发生变化。 讨论…

作者头像 李华