当前位置:   article > 正文

使用easyx图形库和c语言实现简单交互式游戏——快跑,我的球_easyx简单作品

easyx简单作品

利用这个小游戏可以巩固我们前面所学习的知识,使用easyx图形库可以使我们之前的终端输出变得更加有趣,我这里安装的是easyx大暑版(即最新版),大家可以到官网上下载,小游戏源代码就分享在这里了,大家有兴趣的话可以玩玩我的小游戏

  1. #include <graphics.h>
  2. #include<stdio.h>
  3. #include<conio.h>
  4. struct Ball
  5. {
  6. int x;
  7. int y;
  8. int r;
  9. int dx;
  10. int dy;
  11. };
  12. struct Ball ball1 = { 400,400,10,20,-15 };
  13. struct Ball ball2 = { 300,200,20,6,-20 };
  14. struct Ball ball3 = { 70,600,30,9,-20 };
  15. struct Ball myball = { 800, 650, 15, 20, 20 };//初始化所用到的球体参数
  16. void KeyDown()//实现键盘控制myball移动的函数
  17. {
  18. switch (_getch())
  19. {
  20. case 'W':
  21. case 'w':
  22. myball.y -= myball.dy;
  23. break;
  24. case 'S':
  25. case 's':
  26. myball.y += myball.dy;
  27. break;
  28. case 'A':
  29. case 'a':
  30. myball.x -= myball.dx;
  31. break;
  32. case 'D':
  33. case 'd':
  34. myball.x += myball.dx;
  35. break;
  36. }
  37. }
  38. void drawball(struct Ball ball1)//对障碍小球的画图操作
  39. {
  40. setfillcolor(WHITE);
  41. solidcircle(ball1.x, ball1.y, ball1.r);
  42. }
  43. void drawball2(struct Ball ball2)//对myball小球的画图操作
  44. {
  45. setfillcolor(RED);
  46. solidcircle(ball2.x, ball2.y, ball2.r);
  47. }
  48. void moveball1()//根据障碍小球不同的运动轨迹来实现障碍小球运动的函数,这是实现小球1运动的函数
  49. {
  50. if (ball1.x - ball1.r <= 0 || ball1.x + ball1.r >= 900||ball1.x>=200&&ball1.x<=300)
  51. {
  52. ball1.dx = -ball1.dx;
  53. }
  54. if (ball1.y - ball1.r <= 0 || ball1.y + ball1.r >= 700||ball1.y>=100&&ball1.y<=200)
  55. {
  56. ball1.dy = -ball1.dy;
  57. }
  58. ball1.x += ball1.dx;
  59. ball1.y += ball1.dy;
  60. }
  61. void moveball2()//实现障碍小球2运动的函数
  62. {
  63. if (ball2.x - ball2.r <= 0 || ball2.x + ball2.r >= 900 || ball2.x >= 100 && ball2.x <= 200)
  64. {
  65. ball2.dx = -ball2.dx;
  66. }
  67. if (ball2.y - ball2.r <= 0 || ball2.y + ball2.r >= 700 || ball2.y >= 600 && ball2.y <= 700)
  68. {
  69. ball2.dy = -ball2.dy;
  70. }
  71. ball2.x += ball2.dx;
  72. ball2.y += ball2.dy;
  73. }
  74. void moveball3()//实现障碍小球3运动的函数
  75. {
  76. if (ball3.x - ball3.r <= 0 || ball3.x + ball3.r >= 900 || ball3.x >= 600 && ball3.x <= 800)
  77. {
  78. ball3.dx = -ball3.dx;
  79. }
  80. if (ball3.y - ball3.r <= 0 || ball3.y + ball3.r >= 700 || ball3.y >= 200 && ball3.y <= 300)
  81. {
  82. ball3.dy = -ball3.dy;
  83. }
  84. ball3.x += ball3.dx;
  85. ball3.y += ball3.dy;
  86. }
  87. int main()
  88. {
  89. initgraph(1200, 700);//开启游戏起始界面的窗口
  90. IMAGE img1;
  91. ExMessage msg;
  92. loadimage(&img1, "./begin.jpg", 1200, 700);//使用此路径源文件要和图片在图一级目录下
  93. putimage(0, 0, &img1);
  94. while (1) {
  95. while (peekmessage(&msg))
  96. {
  97. if ((msg.message == WM_LBUTTONDOWN) && (msg.x <= 700 && msg.x >= 500) && (msg.y <= 700 && msg.y >= 630))//判断是否有鼠标点击开始游戏处
  98. {
  99. closegraph();
  100. initgraph(900, 700);//如果点击了开始游戏,那么开启游戏闯关界面
  101. while (1)//循环清屏和画图操作实现小球的动态移动效果
  102. {
  103. cleardevice();
  104. drawball(ball1);
  105. drawball(ball2);
  106. drawball(ball3);
  107. drawball2(myball);
  108. moveball1();
  109. moveball2();
  110. moveball3();
  111. if (_kbhit())//myball小球的控制
  112. {
  113. cleardevice();
  114. drawball(ball1);
  115. drawball(ball2);
  116. drawball(ball3);
  117. drawball2(myball);
  118. moveball1();
  119. moveball2();
  120. moveball3();
  121. KeyDown();
  122. }
  123. if (
  124. ((myball.x - ball1.x) * (myball.x - ball1.x) + (myball.y - ball1.y) * (myball.y - ball1.y)) <= (myball.r + ball1.r) * (myball.r + ball1.r)
  125. || ((myball.x - ball2.x) * (myball.x - ball2.x) + (myball.y - ball2.y) * (myball.y - ball2.y)) <= (myball.r + ball2.r) * (myball.r + ball2.r)
  126. || ((myball.x - ball3.x) * (myball.x - ball3.x) + (myball.y - ball3.y) * (myball.y - ball3.y)) <= (myball.r + ball3.r) * (myball.r + ball3.r)
  127. )//当myball和障碍小球发生触碰时,游戏失败
  128. {
  129. closegraph();
  130. initgraph(1200, 700);
  131. IMAGE img2;
  132. loadimage(&img2, "./over.jpg", 1200, 700);//使用该路径要使源文件和图片文件在同一级目录下
  133. putimage(0, 0, &img2);
  134. Sleep(5000);
  135. return 0;
  136. }
  137. if (myball.x < 15 && myball.y < 15)//当myball到达界面左上角时,闯关成功
  138. {
  139. closegraph();
  140. initgraph(1200, 700);
  141. IMAGE img3;
  142. loadimage(&img3, "./success.jpg", 1200, 700);
  143. putimage(0, 0, &img3);
  144. Sleep(5000);
  145. return 0;
  146. return 0;
  147. }
  148. Sleep(20);
  149. }
  150. closegraph();
  151. }
  152. }
  153. }
  154. return 0;
  155. }

这里给大家演示一下游戏画面:

其中红色小球是我们可以操作移动的小球,白色即为障碍小球 

成功界面:

失败界面:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/358886
推荐阅读
相关标签
  

闽ICP备14008679号