当前位置:   article > 正文

球球大作战简易版代码(含简单人机)_球球大作战gg脚本代码

球球大作战gg脚本代码

不废话,直接上代码:

  1. #include<stdio.h>
  2. #include<easyx.h>
  3. #include<stdlib.h>
  4. #include<time.h>
  5. #include<math.h>
  6. /*
  7. 项目内容:球球大作战
  8. 开发环境:vs2019+easyx
  9. */
  10. #define SCREEN_TICK_PER_FRAME (1000/60.0) //每一帧需要的毫秒数
  11. #define FOODNUM 200 //食物数量
  12. #define AINUM 10 //人机数量
  13. #define distance(spr1,spr2) sqrt(((spr1)->x-(spr2)->x)*((spr1)->x-(spr2)->x)+((spr1)->y-(spr2)->y)*((spr1)->y-(spr2)->y))
  14. //创建球球结构体
  15. struct Sprite//精灵
  16. {
  17. double x;
  18. double y;
  19. double r;
  20. double speed;
  21. bool isdie=false;//是否godie了
  22. COLORREF color;//颜色
  23. int index;
  24. };
  25. //初始化精灵
  26. void spr_init(Sprite* spr, int x, int y, int r, int speed)
  27. {
  28. spr->x = x;
  29. spr->y = y;
  30. spr->r = r;
  31. spr->speed = speed;
  32. spr->color = RGB(rand()%256,rand()%256,rand()%256); //随机生成颜色
  33. spr->isdie = false;
  34. spr->index = -1; //人机食物坐标
  35. }
  36. void spr_draw(Sprite* spr)
  37. {
  38. if (!spr->isdie)
  39. {
  40. //设置填充颜色
  41. setfillcolor(spr->color);
  42. //绘制一个圆
  43. solidcircle(spr->x, spr->y, spr->r);
  44. }
  45. }
  46. void gamer_move(Sprite* spr)
  47. {
  48. //获取键盘按键
  49. if (GetAsyncKeyState(VK_UP)) //GetAsyncKeyState获取异步按键的状态(检测某个健是否按下)
  50. {
  51. spr->y -= spr->speed;
  52. }
  53. if (GetAsyncKeyState(VK_DOWN)) //GetAsyncKeyState获取异步按键的状态(检测某个健是否按下)
  54. {
  55. spr->y += spr->speed;
  56. }
  57. if (GetAsyncKeyState(VK_LEFT)) //GetAsyncKeyState获取异步按键的状态(检测某个健是否按下)
  58. {
  59. spr->x -= spr->speed;
  60. }
  61. if (GetAsyncKeyState(VK_RIGHT)) //GetAsyncKeyState获取异步按键的状态(检测某个健是否按下)
  62. {
  63. spr->x += spr->speed;
  64. }
  65. }
  66. void gamer_eatFood(Sprite* gamer, Sprite* foods)
  67. {
  68. //遍历食物数组
  69. for (int i = 0; i < FOODNUM; i++)
  70. {
  71. //圆心距小于玩家的半径
  72. if (!foods[i].isdie&&distance(gamer, foods+i) < gamer->r)
  73. {
  74. //玩家变大
  75. gamer->r += foods[i].r/8;
  76. //食物消失
  77. foods[i].isdie = true;
  78. }
  79. }
  80. }
  81. void ai_eatFood(Sprite* ais, Sprite* foods)
  82. {
  83. for (int i = 0; i < AINUM; i++)
  84. {
  85. if (ais[i].isdie)
  86. continue;
  87. for (int k = 0; k < FOODNUM; k++)
  88. {
  89. if (!foods[k].isdie && distance(ais + i, foods + k) < ais[i].r)
  90. {
  91. ais[i].r += foods[ais[i].index].r / 8;
  92. foods[k].isdie = true;
  93. }
  94. }
  95. }
  96. }
  97. //定义全局变量
  98. Sprite gamer;//玩家
  99. Sprite food[FOODNUM]; //食物的数组
  100. Sprite ai[AINUM];//人工智障 联机
  101. //追击方法
  102. void chase(Sprite* chase, Sprite* run)
  103. {
  104. if (chase->x > run->x)
  105. {
  106. chase->x--;
  107. }
  108. else
  109. {
  110. chase->x++;
  111. }
  112. }
  113. void ai_move(Sprite* ais,Sprite* foods)
  114. {
  115. int minDis = getwidth();
  116. //吃最近的食物
  117. int k;
  118. for (int i = 0; i < FOODNUM; i++) //食物
  119. {
  120. if (food[i].isdie)
  121. continue;
  122. for (k = 0; k < AINUM; k++)
  123. {
  124. if (ais[k].isdie)
  125. continue;
  126. //开始判断
  127. double dis = distance(foods + i, ais + k);
  128. if (dis < minDis)
  129. {
  130. minDis = dis;
  131. ais[k].index = k;
  132. }
  133. }
  134. }
  135. for (int i = 0; i < AINUM; i++)
  136. {
  137. if (!foods[ais[i].index].isdie)
  138. {
  139. //开始追击
  140. chase(ais + i, foods + ais[i].index);
  141. 能吃了,圆心距小于我的半径
  142. //if (distance(ais + i, foods + ais[i].index) < ais[i].r)
  143. //{
  144. // ais[i].r = foods[ais[i].index].r / 8;
  145. // foods[ais[i].index].isdie = true;
  146. //}
  147. }
  148. }
  149. }
  150. void draw()
  151. {
  152. spr_draw(&gamer);
  153. //绘制所有食物
  154. for (int i = 0; i < FOODNUM; i++)
  155. {
  156. spr_draw(food + i);
  157. }
  158. for (int i = 0; i < AINUM; i++)
  159. {
  160. spr_draw(ai + i);
  161. }
  162. }
  163. //初始化所有数据
  164. void init()
  165. {
  166. srand(time(NULL));
  167. spr_init(&gamer, 1024 / 2, 640 / 2, 10, 3);
  168. //初始化所有的食物
  169. for (int i = 0; i < FOODNUM; i++)
  170. {
  171. int r = rand() % 5 + 2;
  172. int x = rand() % (getwidth());
  173. int y = rand() % (getheight());
  174. spr_init(food + i, x, y, r, 0);
  175. }
  176. //初始化人机
  177. for (int i = 0; i < AINUM; i++)
  178. {
  179. int r = rand() % 10 + 10;
  180. int x = rand() % (getwidth()-r);
  181. int y = rand() % (getheight()-r);
  182. spr_init(ai + i, x, y, r,3);
  183. }
  184. }
  185. int main()
  186. {
  187. //创建窗口
  188. initgraph(1024, 640);
  189. //设置背景颜色
  190. setbkcolor(WHITE);
  191. cleardevice();
  192. init();
  193. while (true) {
  194. //获取一下时间
  195. int startTime = clock();
  196. //绘制太快,未完成的步骤都已经被看到,所以会闪屏
  197. BeginBatchDraw(); //开启双缓冲
  198. //在绘制前先清屏
  199. cleardevice();
  200. draw();
  201. gamer_move(&gamer);
  202. ai_move(ai,food);
  203. gamer_eatFood(&gamer, food);
  204. ai_eatFood(ai, food);
  205. EndBatchDraw();//结束双缓冲
  206. //控制游戏帧率
  207. int frameTime = clock() - startTime; //计算一帧用了多久
  208. //提前就执行完了当前帧了 frametime 10 16.6666
  209. if (frameTime < SCREEN_TICK_PER_FRAME)
  210. {
  211. Sleep(SCREEN_TICK_PER_FRAME - frameTime);
  212. }
  213. }
  214. getchar();//防止闪退
  215. return 0;
  216. }

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

闽ICP备14008679号