C++怎么利用easyx图形库实现天天酷跑小游戏

其他教程   发布日期:2023年08月13日   浏览次数:603

这篇文章主要介绍“C++怎么利用easyx图形库实现天天酷跑小游戏”,在日常操作中,相信很多人在C++怎么利用easyx图形库实现天天酷跑小游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++怎么利用easyx图形库实现天天酷跑小游戏”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

开发日志

1.创建项目

2.导入素材

3.创建游戏界面

从用户界面入手

选择图形库或者其他引擎,酷跑是基于“easyx”图形库的

1)创建游戏窗口

2)设计游戏背景

a.三重背景不同的速度移动

b.循环滚动背景的实现

3)实现游戏背景

a.加载背景资源

b.渲染(实现打印图片的效果)背景知识:坐标

遇到的问题:背景图片的png格式图片出现黑色

4.实现玩家奔跑

5.实现玩家跳跃

6.实现随机小乌龟

7.创建结构体结构类型

8.使用障碍物结构体后重新初始化

9.封装后多个障碍物的显示

10.实现玩家的下蹲技能

11.添加柱子障碍物

代码实现

  1. //#undef UNICODE
  2. //#undef _UNICODE
  3. #include <stdio.h>
  4. #include <graphics.h>
  5. #include <conio.h>
  6. #include <vector> //c++ 长度可变的数组
  7. #include "tools.h"
  8. using namespace std; //声明命名空间
  9. #define WIN_SCORE 10
  10. #define WIN_WIDTH 1012 //定义宏 便于后期维护与处理
  11. #define WIN_HEIGHT 396
  12. #define OBSTACLE_COUNT 10
  13. IMAGE imgBgs[3];//背景图片――创建图片数组
  14. int bgX[3];//背景图片的X坐标(不断变化)
  15. int bgSpeed[3] = { 1,2,4 };//控制3个背景不同速度
  16. IMAGE imgHeros[12];//人物不断奔跑的实现
  17. int heroX;//玩家的X坐标
  18. int heroY;//玩家的Y坐标
  19. int heroIndex;//玩家奔跑的图片帧序号
  20. bool heroJump;//表示玩家正在跳跃
  21. int jumpHeightMax;//跳跃的最大高度
  22. int heroJumpOff;//跳跃偏移量
  23. int update;//表示是否需要马上刷新画面
  24. //IMAGE imgTortoise; //小乌龟
  25. //int torToiseX; //小乌龟的水平坐标
  26. //int torToiseY; //小乌龟的竖直坐标
  27. //bool torToiseExist; //当前窗口是否有小乌龟
  28. int heroBlood; //定义玩家血量
  29. int score;
  30. typedef enum {
  31. TORTOISE, //乌龟 0
  32. LION, //狮子 1
  33. HOOK1,
  34. HOOK2,
  35. HOOK3,
  36. HOOK4,
  37. OBSTACLE_TYPE_COUNT //边界 6
  38. }obstacle_type;
  39. // 相当于 IMAGE obstacleImgs[3][5]
  40. vector<vector<IMAGE>>obstacleImgs; //二维数组 存放所有障碍物的各个图片
  41. typedef struct obstacle {
  42. int type; //障碍物的类型
  43. int imgIndex; //当前显示的图片的序号
  44. int x, y; //障碍物的坐标
  45. int speed;
  46. int power; //杀伤力
  47. bool exist;
  48. bool hited; //表示是否已经发生碰撞
  49. bool passed;//表示是否已经被通过
  50. }obstacle_t;
  51. obstacle_t obstacles[OBSTACLE_COUNT];
  52. int lastObsIndex;//解决障碍物bug(柱子与狮子在一起)
  53. IMAGE imgHeroDown[2];
  54. bool heroDown; //表示玩家是否处于下蹲状态
  55. IMAGE imgSZ[10];
  56. //游戏的初始化
  57. void init() {
  58. //创建游戏窗口
  59. initgraph(WIN_WIDTH, WIN_HEIGHT, EW_SHOWCONSOLE);
  60. //加载背景资源
  61. char name[64];
  62. for (int i = 0; i < 3; i++)
  63. {
  64. //路径 "res/bg001.png" "res/bg002.png" "res/bg003.png"
  65. sprintf(name, "res/bg%03d.png",i+1);//%03d占3位,不足3位自动补0
  66. //#undef _UNICODE
  67. loadimage(&imgBgs[i], name);//把3个图片加载到了图片数组的位置
  68. bgX[i] = 0;
  69. }
  70. //加载Hero奔跑的图片帧素材
  71. for (int i = 0; i < 12; i++) {
  72. sprintf(name, "res/hero%d.png", i + 1);
  73. loadimage(&imgHeros[i], name);
  74. }
  75. //设置玩家的初始位置
  76. heroX = WIN_WIDTH * 0.5 - imgHeros[0].getwidth() * 0.5;//X坐标:屏幕宽度的一半减去人物宽度的一半
  77. heroY = 345 - imgHeros[0].getheight();//Y坐标:脚底坐标减去人物高度
  78. heroIndex = 0;
  79. heroJump = false;
  80. jumpHeightMax = 345 - imgHeros[0].getheight() - 120;
  81. heroJumpOff = -4;
  82. update = true;
  83. 加载小乌龟素材
  84. //loadimage(&imgTortoise, "res/t1.png");
  85. //torToiseExist = false;
  86. //torToiseY = 345 - imgTortoise.getheight() + 5;
  87. IMAGE imgTort;
  88. loadimage(&imgTort, "res/t1.png");
  89. vector<IMAGE>imgTortArray;
  90. imgTortArray.push_back(imgTort);//添加图片
  91. obstacleImgs.push_back(imgTortArray);
  92. IMAGE imgLion;
  93. vector<IMAGE>imgLionArray;
  94. for (int i = 0; i < 6; i++) {
  95. sprintf(name, "res/p%d.png", i + 1);
  96. loadimage(&imgLion, name);
  97. imgLionArray.push_back(imgLion);
  98. }
  99. obstacleImgs.push_back(imgLionArray);
  100. //初始化障碍物池
  101. for (int i = 0; i < OBSTACLE_COUNT; i++) {
  102. obstacles[i].exist = false;
  103. }
  104. //加载下蹲素材
  105. loadimage(&imgHeroDown[0], "res/d1.png");
  106. loadimage(&imgHeroDown[1], "res/d2.png");
  107. heroDown = false;
  108. //加载柱子图片
  109. IMAGE imgH;
  110. for (int i = 0; i < 4; i++) {
  111. vector<IMAGE> imgHookArray;
  112. sprintf(name, "res/h%d.png", i + 1);
  113. loadimage(&imgH, name, 63, 250, true); // 图片进行缩化
  114. imgHookArray.push_back(imgH);
  115. obstacleImgs.push_back(imgHookArray);
  116. }
  117. heroBlood = 100;
  118. //预加载音效
  119. preLoadSound("res/hit.mp3");
  120. //背景音乐
  121. mciSendString("play res/bg.mp3 repeat", 0, 0, 0);
  122. lastObsIndex = -1;
  123. score = 0;
  124. //加载数字图片
  125. for (int i = 0; i < 10; i++) {
  126. sprintf(name, "res/sz/%d.png", i);
  127. loadimage(&imgSZ[i], name);
  128. }
  129. }
  130. //随机创建障碍物
  131. void creatObstacle() {
  132. int i;
  133. for (i = 0; i < OBSTACLE_COUNT; i++) {
  134. if (obstacles[i].exist == false) {
  135. break;
  136. }
  137. }
  138. if (i >= OBSTACLE_COUNT) {
  139. return;
  140. }
  141. obstacles[i].exist = true;
  142. obstacles[i].hited = false;
  143. obstacles[i].imgIndex = 0;
  144. //obstacles[i].type = (obstacle_type)(rand() % OBSTACLE_TYPE_COUNT);
  145. obstacles[i].type = (obstacle_type)(rand() % 3);
  146. //如果上一个障碍物是柱子,下一个是狮子,判断距离,若很近,则狮子换为乌龟
  147. if (lastObsIndex >= 0 && obstacles[lastObsIndex].type >= HOOK1 && obstacles[lastObsIndex].type <= HOOK4 && obstacles[i].type == LION && obstacles[lastObsIndex].x > WIN_WIDTH - 500)
  148. {
  149. obstacles[i].type == TORTOISE;
  150. }
  151. lastObsIndex = i;
  152. if (obstacles[i].type == HOOK1) { //降低柱子出现的频率
  153. obstacles[i].type += rand() % 4; //0-3
  154. }
  155. obstacles[i].x = WIN_WIDTH;
  156. obstacles[i].y = 345 + 5 - obstacleImgs[obstacles[i].type][0].getheight();
  157. if (obstacles[i].type == TORTOISE) {
  158. obstacles[i].speed = 0;
  159. obstacles[i].power = 5; //随意
  160. }
  161. else if (obstacles[i].type == LION) {
  162. obstacles[i].speed = 1;
  163. obstacles[i].power = 20;
  164. }
  165. else if (obstacles[i].type >= HOOK1 && obstacles[i].type <= HOOK4) {
  166. obstacles[i].speed = 0;
  167. obstacles[i].power = 20;
  168. obstacles[i].y = 0;
  169. }
  170. obstacles[i].passed = false;
  171. }
  172. void checkHit() {
  173. for(int i = 0; i < OBSTACLE_COUNT; i++) {
  174. if (obstacles[i].exist && obstacles[i].hited == false) {
  175. int a1x, a1y, a2x, a2y;
  176. int off = 30;
  177. if (!heroDown) { //非下蹲 奔跑 跳跃
  178. a1x = heroX + off;
  179. a1y = heroY + off;
  180. a2x = heroX + imgHeros[heroIndex].getwidth() - off;
  181. a2y = heroY + imgHeros[heroIndex].getheight();
  182. }
  183. else {
  184. a1x = heroX + off;
  185. a1y = 345 - imgHeroDown[heroIndex].getheight();
  186. a2x = heroX + imgHeroDown[heroIndex].getwidth() - off;
  187. a2y = 345;
  188. }
  189. IMAGE img = obstacleImgs[obstacles[i].type][obstacles[i].imgIndex]; //当前障碍物类型(的第几张图片)
  190. int b1x = obstacles[i].x + off;
  191. int b1y = obstacles[i].y + off;
  192. int b2x = obstacles[i].x + img.getwidth() - off;
  193. int b2y = obstacles[i].y + img.getheight() - 10;
  194. if (rectIntersect(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y)) {
  195. heroBlood -= obstacles[i].power;
  196. printf("血量剩余 %d
  197. ", heroBlood);
  198. playSound("res/hit.mp3");
  199. obstacles[i].hited = true;
  200. }
  201. }
  202. }
  203. }
  204. //让背景动起来
  205. void run() {
  206. for (int i = 0; i < 3; i++) {
  207. bgX[i] -= bgSpeed[i];//3个背景移动的速度不同
  208. if (bgX[i] < -WIN_WIDTH) {
  209. bgX[i] = 0;
  210. }
  211. }
  212. //实现跳跃
  213. if (heroJump)
  214. {
  215. if (heroY < jumpHeightMax) //达到最大跳跃高度 跳跃偏移量为正 向下跳跃
  216. {
  217. heroJumpOff = 4;
  218. }
  219. heroY += heroJumpOff;
  220. if (heroY > 345 - imgHeros[0].getheight()) //到达地面 跳跃结束
  221. {
  222. heroJump = false;
  223. heroJumpOff = -4; // 偏移量初始化
  224. }
  225. }
  226. else if (heroDown) { //人物下蹲
  227. static int count = 0;
  228. int delays[2] = { 8,30 }; //设置下蹲的时间不一样
  229. count++;
  230. if (count >= delays[heroIndex]) {
  231. count = 0;
  232. heroIndex++;
  233. if (heroIndex >= 2) {
  234. heroIndex = 0;
  235. heroDown = false;
  236. }
  237. }
  238. }
  239. else{ //不跳跃
  240. heroIndex = (heroIndex + 1) % 12; //12张图片循环播放完成一系列动作
  241. }
  242. //创建障碍物
  243. static int frameCount = 0;
  244. static int enemyFre = 50;
  245. frameCount++;
  246. if (frameCount > enemyFre){
  247. frameCount = 0;
  248. //if (!torToiseExist) { //避免屏幕同时出现多个小乌龟
  249. // torToiseExist = true;
  250. // torToiseX = WIN_WIDTH;
  251. // enemyFre=rand()%301+200; //每200-500帧随机出现一只龟
  252. //}
  253. enemyFre = rand() % 50 + 50;
  254. creatObstacle();
  255. }
  256. //if (torToiseExist) {
  257. // //更新小乌龟位置
  258. // torToiseX -= bgSpeed[2];
  259. // if (torToiseX < -imgTortoise.getwidth()) {
  260. // torToiseExist = false;
  261. // }
  262. //}
  263. //更新所有障碍物的坐标
  264. for (int i = 0; i < OBSTACLE_COUNT; i++) {
  265. if (obstacles[i].exist) {
  266. obstacles[i].x -= obstacles[i].speed + bgSpeed[2];
  267. if (obstacles[i].x < -obstacleImgs[obstacles[i].type][0].getwidth() * 2) {
  268. obstacles[i].exist = false;
  269. }
  270. int len = obstacleImgs[obstacles[i].type].size();
  271. obstacles[i].imgIndex = (obstacles[i].imgIndex + 1) % len;
  272. }
  273. }
  274. //玩家和障碍物的“碰撞检测”处理
  275. checkHit();
  276. }
  277. //渲染“游戏背景”
  278. void updateBg() {
  279. //调整背景图片位置
  280. putimagePNG2(bgX[0], 0, &imgBgs[0]);
  281. putimagePNG2(bgX[1], 119, &imgBgs[1]);
  282. putimagePNG2(bgX[2], 330, &imgBgs[2]);
  283. }
  284. //实现跳跃
  285. void jump() {
  286. heroJump = true;
  287. update = true; //未处于刷新时也能跳跃
  288. }
  289. void down() {
  290. update = true;
  291. heroDown = true;
  292. heroIndex = 0;
  293. }
  294. //处理用户按键的输入
  295. void keyEvent() {
  296. //char c;
  297. //scanf("%c", &c); 会直接阻塞程序的执行
  298. if (GetAsyncKeyState(VK_UP)){ //虚拟键
  299. jump();
  300. /*
  301. if(kbhit()) //kbhit()判断有无键盘输入。若有按键按下,则kbhit()返回 TRUE
  302. {
  303. char ch = _getch();//不需要按下回车即可直接读取
  304. if (ch == ' ') {//按下空格跳跃
  305. jump();
  306. }
  307. */
  308. }
  309. else if (GetAsyncKeyState(VK_DOWN)) {
  310. down();
  311. }
  312. }
  313. void updateEnemy() {
  314. //渲染小乌龟
  315. /*if (torToiseExist) {
  316. putimagePNG2(torToiseX, torToiseY, WIN_WIDTH, &imgTortoise);
  317. }*/
  318. for (int i = 0; i < OBSTACLE_COUNT; i++) {
  319. if (obstacles[i].exist) {
  320. putimagePNG2(obstacles[i].x, obstacles[i].y, WIN_WIDTH, &obstacleImgs[obstacles[i].type][obstacles[i].imgIndex]);
  321. }
  322. }
  323. }
  324. void updateHero() {
  325. if (!heroDown) { //不处于下蹲状态――奔跑跳跃
  326. putimagePNG2(heroX, heroY, &imgHeros[heroIndex]);
  327. }
  328. else {
  329. int y = 345 - imgHeroDown[heroIndex].getheight();
  330. putimagePNG2(heroX, y, &imgHeroDown[heroIndex]);
  331. }
  332. }
  333. void updateBloodBar()
  334. {
  335. drawBloodBar(10, 10, 200, 10, 2, BLUE, DARKGRAY, RED, heroBlood / 100.0);
  336. }
  337. void checkOver() {
  338. if (heroBlood <= 0) {
  339. loadimage(0, "res/over.png");
  340. FlushBatchDraw();//刷新
  341. mciSendString("stop res / bg.mp3", 0, 0, 0);//关掉背景音乐
  342. system("pause");
  343. //暂停之后,充币复活或者直接开始下一局
  344. heroBlood = 100;
  345. score = 0;
  346. mciSendString("play res / bg.mp3 repeat", 0, 0, 0);
  347. }
  348. }
  349. void checkScore() {
  350. for (int i = 0; i < OBSTACLE_COUNT; i++) {
  351. if (obstacles[i].exist && obstacles[i].passed == false &&
  352. obstacles[i].x + obstacleImgs[obstacles[i].type][0].getwidth() < heroX && obstacles[i].hited == false)
  353. {
  354. score++;
  355. obstacles[i].passed = true;
  356. printf("score:%d
  357. ", score);
  358. }
  359. }
  360. }
  361. void updateScore() {
  362. char str[8];
  363. sprintf(str, "%d", score);
  364. int x = 20;
  365. int y = 25;
  366. for (int i = 0; str[i]; i++) {
  367. int sz = str[i] - '0';
  368. putimagePNG(x, y, &imgSZ[sz]);
  369. x += imgSZ[sz].getwidth() + 5;
  370. }
  371. }
  372. void checkWin() {
  373. if (score >= WIN_SCORE) {
  374. FlushBatchDraw();
  375. mciSendString("play res/win.mp3", 0, 0, 0);
  376. Sleep(2000);
  377. loadimage(0, "res/win.png");
  378. FlushBatchDraw();
  379. mciSendString("stop res/bg.mp3", 0, 0, 0);
  380. system("pause");
  381. heroBlood = 100;
  382. score = 0;
  383. mciSendString("play res/bg.mp3 repeat", 0, 0, 0);
  384. }
  385. }
  386. int main(void)
  387. {
  388. init();
  389. //显示初始化面
  390. loadimage(0, "res/over.png");
  391. system("pause");
  392. int timer = 0;
  393. while (1) {
  394. keyEvent();
  395. timer += getDelay();//此函数返回距离上一次调用间隔的时间,第一次返回0
  396. if (timer > 30) { //30毫秒刷新时间
  397. timer = 0;
  398. update = true;
  399. }
  400. if (update) {
  401. update = false;
  402. BeginBatchDraw();//这个函数用于开始批量绘图。执行后,任何绘图操作都将暂时不输出到绘图窗口上,直到执行 FlushBatchDraw 或 EndBatchDraw 才将之前的绘图输出。
  403. updateBg();
  404. //putimagePNG2(heroX, heroY, &imgHeros[heroIndex]);
  405. updateHero();
  406. updateEnemy();
  407. updateBloodBar();
  408. updateScore();
  409. checkWin();
  410. EndBatchDraw();//这个函数用于结束批量绘制,并执行未完成的绘制任务。 这两个函数主要为了消除闪烁。
  411. checkOver();
  412. checkScore();
  413. run();
  414. }
  415. //Sleep(30); //休眠
  416. }
  417. system("pause");
  418. return 0;
  419. }
  420. ## tool头文件
  421. ```javascript
  422. #include <stdio.h>
  423. #include <Windows.h>
  424. #include "tools.h"
  425. #include <mmsystem.h>
  426. #pragma comment(lib, "winmm.lib")
  427. int getDelay() {
  428. static unsigned long long lastTime = 0;
  429. unsigned long long currentTime = GetTickCount();
  430. if (lastTime == 0) {
  431. lastTime = currentTime;
  432. return 0;
  433. }
  434. else {
  435. int ret = currentTime - lastTime;
  436. lastTime = currentTime;
  437. return ret;
  438. }
  439. }
  440. // 载入PNG图并去透明部分
  441. void putimagePNG(int picture_x, int picture_y, IMAGE* picture) //x为载入图片的X坐标,y为Y坐标
  442. {
  443. DWORD* dst = GetImageBuffer(); // GetImageBuffer()函数,用于获取绘图设备的显存指针,EASYX自带
  444. DWORD* draw = GetImageBuffer();
  445. DWORD* src = GetImageBuffer(picture); //获取picture的显存指针
  446. int picture_width = picture->getwidth(); //获取picture的宽度,EASYX自带
  447. int picture_height = picture->getheight(); //获取picture的高度,EASYX自带
  448. int graphWidth = getwidth(); //获取绘图区的宽度,EASYX自带
  449. int graphHeight = getheight(); //获取绘图区的高度,EASYX自带
  450. int dstX = 0; //在显存里像素的角标
  451. // 实现透明贴图 公式: Cp=αp*FP+(1-αp)*BP , 贝叶斯定理来进行点颜色的概率计算
  452. for (int iy = 0; iy < picture_height; iy++)
  453. {
  454. for (int ix = 0; ix < picture_width; ix++)
  455. {
  456. int srcX = ix + iy * picture_width; //在显存里像素的角标
  457. int sa = ((src[srcX] & 0xff000000) >> 24); //0xAArrggbb;AA是透明度
  458. int sr = ((src[srcX] & 0xff0000) >> 16); //获取RGB里的R
  459. int sg = ((src[srcX] & 0xff00) >> 8); //G
  460. int sb = src[srcX] & 0xff; //B
  461. if (ix >= 0 && ix <= graphWidth && iy >= 0 && iy <= graphHeight && dstX <= graphWidth * graphHeight)
  462. {
  463. dstX = (ix + picture_x) + (iy + picture_y) * graphWidth; //在显存里像素的角标
  464. int dr = ((dst[dstX] & 0xff0000) >> 16);
  465. int dg = ((dst[dstX] & 0xff00) >> 8);
  466. int db = dst[dstX] & 0xff;
  467. draw[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) << 16) //公式: Cp=αp*FP+(1-αp)*BP ; αp=sa/255 , FP=sr , BP=dr
  468. | ((sg * sa / 255 + dg * (255 - sa) / 255) << 8) //αp=sa/255 , FP=sg , BP=dg
  469. | (sb * sa / 255 + db * (255 - sa) / 255); //αp=sa/255 , FP=sb , BP=db
  470. }
  471. }
  472. }
  473. }
  474. // 适用于 y <0 以及x<0的任何情况
  475. void putimagePNG2(int x, int y, IMAGE* picture) {
  476. IMAGE imgTmp;
  477. if (y < 0) {
  478. SetWorkingImage(picture);
  479. getimage(&imgTmp, 0, -y,
  480. picture->getwidth(), picture->getheight() + y);
  481. SetWorkingImage();
  482. y = 0;
  483. picture = &imgTmp;
  484. }
  485. if (x < 0) {
  486. SetWorkingImage(picture);
  487. getimage(&imgTmp, -x, 0, picture->getwidth() + x, picture->getheight());
  488. SetWorkingImage();
  489. x = 0;
  490. picture = &imgTmp;
  491. }
  492. putimagePNG(x, y, picture);
  493. }
  494. // 适用于 y <0 以及y>0的任何情况
  495. void putimagePNG2(int x, int y, int winWidth, IMAGE* picture) {
  496. IMAGE imgTmp;
  497. if (y < 0) {
  498. SetWorkingImage(picture);
  499. getimage(&imgTmp, 0, -y,
  500. picture->getwidth(), picture->getheight() + y);
  501. SetWorkingImage();
  502. y = 0;
  503. picture = &imgTmp;
  504. }
  505. if (x < 0) {
  506. SetWorkingImage(picture);
  507. getimage(&imgTmp, -x, 0, picture->getwidth() + x, picture->getheight());
  508. SetWorkingImage();
  509. x = 0;
  510. picture = &imgTmp;
  511. }
  512. else if (x >= winWidth) {
  513. return;
  514. }
  515. else if (x > winWidth-picture->getwidth()) {
  516. SetWorkingImage(picture);
  517. getimage(&imgTmp, 0, 0, winWidth - x, picture->getheight());
  518. SetWorkingImage();
  519. picture = &imgTmp;
  520. }
  521. putimagePNG(x, y, picture);
  522. }
  523. //设A[x01,y01,x02,y02] B[x11,y11,x12,y12].
  524. bool rectIntersect(int x01, int y01, int x02, int y02,
  525. int x11, int y11, int x12, int y12)
  526. {
  527. int zx = abs(x01 + x02 - x11 - x12);
  528. int x = abs(x01 - x02) + abs(x11 - x12);
  529. int zy = abs(y01 + y02 - y11 - y12);
  530. int y = abs(y01 - y02) + abs(y11 - y12);
  531. return (zx <= x && zy <= y);
  532. }
  533. void preLoadSound(const char* name) {
  534. char cmd[512];
  535. sprintf_s(cmd, sizeof(cmd), "open %s alias %s-1", name, name);
  536. mciSendString(cmd, 0, 0, 0);
  537. sprintf_s(cmd, sizeof(cmd), "open %s alias %s-2", name, name);
  538. mciSendString(cmd, 0, 0, 0);
  539. }
  540. void playSound(const char* name) {
  541. static int index = 1;
  542. char cmd[512];
  543. if (index == 1) {
  544. sprintf_s(cmd, sizeof(cmd), "play %s-1", name);
  545. mciSendString(cmd, 0, 0, 0);
  546. sprintf_s(cmd, sizeof(cmd), "close %s-2", name);
  547. mciSendString(cmd, 0, 0, 0);
  548. sprintf_s(cmd, sizeof(cmd), "open %s alias %s-2", name, name);
  549. mciSendString(cmd, 0, 0, 0);
  550. index++;
  551. }
  552. else if (index == 2) {
  553. sprintf_s(cmd, sizeof(cmd), "play %s-2", name);
  554. mciSendString(cmd, 0, 0, 0);
  555. sprintf_s(cmd, sizeof(cmd), "close %s-1", name);
  556. mciSendString(cmd, 0, 0, 0);
  557. sprintf_s(cmd, sizeof(cmd), "open %s alias %s-1", name, name);
  558. mciSendString(cmd, 0, 0, 0);
  559. index = 1;
  560. }
  561. }
  562. void drawBloodBar(int x, int y, int width, int height, int lineWidth, int boardColor, int emptyColor, int fillColor, float percent) {
  563. LINESTYLE lineStyle;
  564. getlinestyle(&lineStyle);
  565. int lineColor = getlinecolor();
  566. int fileColor = getfillcolor();
  567. if (percent < 0) {
  568. percent = 0;
  569. }
  570. setlinecolor(BLUE);
  571. setlinestyle(PS_SOLID | PS_ENDCAP_ROUND, lineWidth);
  572. setfillcolor(emptyColor);
  573. fillrectangle(x, y, x + width, y + height);
  574. setlinestyle(PS_SOLID | PS_ENDCAP_FLAT, 0);
  575. setfillcolor(fillColor);
  576. setlinecolor(fillColor);
  577. if (percent > 0) {
  578. fillrectangle(x + 0.5 * lineWidth, y + lineWidth * 0.5, x + width * percent, y + height - 0.5 * lineWidth);
  579. }
  580. setlinecolor(lineColor);
  581. setfillcolor(fillColor);
  582. setlinestyle(&lineStyle);
  583. }

以上就是C++怎么利用easyx图形库实现天天酷跑小游戏的详细内容,更多关于C++怎么利用easyx图形库实现天天酷跑小游戏的资料请关注九品源码其它相关文章!