当前位置:   article > 正文

C++实现童年游戏

C++实现童年游戏

 其中多处坐标的代码是经过运算移项推导出的

  1. #include<easyx.h>
  2. #include<conio.h>
  3. #include<time.h>
  4. #include<stdio.h>
  5. bool Timer(int id, int ms)//计时器,过了多少毫秒就会返回真
  6. {
  7. static long start[3] = { 0 };
  8. long last = clock();
  9. if (last - start[id] >= ms)
  10. {
  11. start[id] = last;
  12. return true;
  13. }
  14. else return false;
  15. }
  16. void fail()
  17. {
  18. settextstyle(600, 0, 0);
  19. settextcolor(RED);
  20. outtextxy(800, 500, "你死了!");
  21. EndBatchDraw();
  22. _getch();
  23. }
  24. class Car {
  25. public:
  26. short x, y;
  27. COLORREF color;
  28. void isHit(short objx, short objy)
  29. {
  30. if (!(x > objx + 65 || x + 65 < objx || 715 > objy || 885 < objy))
  31. fail();
  32. }
  33. void show()
  34. {
  35. setfillcolor(color);
  36. solidcircle(x + 10, y + 10, 10);
  37. solidcircle(x + 55, y + 10, 10);
  38. solidrectangle(x + 20, y + 20, x + 45, y + 65);
  39. solidcircle(x + 10, y + 75, 10);
  40. solidcircle(x + 55, y + 75, 10);//长度65,宽度85
  41. }
  42. };
  43. class Npc :public Car
  44. {
  45. public:
  46. bool isLive = false;
  47. static Npc* p;
  48. static int* a;
  49. bool overLap(int j)
  50. {
  51. for (int i = 0; i < 6; i++)
  52. {
  53. if (i == j || p[i].isLive == false)
  54. continue;
  55. if (!(x > p[i].x + 65 || x + 65 < p[i].x || 86 < p[i].y))
  56. return true;
  57. }
  58. return false;
  59. }
  60. static void loop(Car*player,int j) {
  61. for (int i = 0; i < 6; i++)
  62. {
  63. int c = 0;
  64. if (p[i].isLive == false && rand() % 100 == 0)
  65. {
  66. short n1 = a[j], n2 = a[j + 1 > 53 ? 0 : j + 1], n3 = a[j + 2 > 53 ? j - 52 : 2], length;
  67. short max = (n1 > n2 ? n1 : n2) > n3 ? (n1 > n2 ? n1 : n2) : n3;
  68. short min = (n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3;
  69. length = min + 835 - max;//max-min+900-65,算出车辆x坐标可出现的变动值
  70. do { p[i].x = rand() % length + 10 + max; }//随机偏移量加上最右边的道路方块起始坐标
  71. while (p[i].overLap(i) && c++ < 1000);
  72. p[i].isLive = true;
  73. p[i].y = 1;
  74. p[i].color = RGB(rand() % 200 + 56, rand() % 200 + 56, rand() % 200 + 56);
  75. }
  76. }
  77. for (int i = 0; i < 6; i++)
  78. {
  79. if (p[i].isLive)
  80. {
  81. if (p[i].y >= 1080)
  82. p[i].isLive = false;
  83. p[i].y += 20;
  84. p[i].show();
  85. player->isHit(p[i].x, p[i].y);//判断有没有撞击敌机
  86. }
  87. }
  88. }
  89. };
  90. Npc* Npc::p = 0;
  91. int* Npc::a = 0;
  92. int main()
  93. {
  94. short a[54] = { 0 };//道路两旁横坐标数组,为了生成蜿蜒曲折的路,用数组模拟双向循环链表
  95. short dire = 0, lastdire = 0, c = 0, t = 30, change = 0, behind = 0, j = 0, metre = 0;//c为道路绵延相同方向的次数
  96. ExMessage ex = { 0 }; bool mouseclick = 0; char str[128];
  97. Car player;
  98. player.x = 500, player.y = 800, player.color = RGB(0, 255, 255);
  99. Npc npc[6];
  100. Npc::p = npc;
  101. srand(time(0));
  102. //车路宽度为920
  103. initgraph(1920, 1080);
  104. setbkmode(TRANSPARENT);
  105. settextstyle(100, 0, 0);
  106. settextcolor(GREEN);
  107. _getch();
  108. while (1)
  109. {
  110. if (peekmessage(&ex, EX_MOUSE))//监听鼠标事件
  111. {
  112. switch (ex.message)
  113. {
  114. case WM_LBUTTONDOWN:
  115. if (ex.x > player.x && ex.x < player.x + 45)
  116. mouseclick = 1;
  117. break;
  118. case WM_LBUTTONUP:
  119. mouseclick = 0;
  120. }
  121. if (mouseclick)//只有拖拽不放才能移动,不能单击任意位置实现瞬移
  122. player.x = ex.x - 22;
  123. }
  124. if (Timer(1, 3000) && t > 5)
  125. t--;
  126. if (Timer(2, t * 4))
  127. {
  128. metre++;
  129. sprintf_s(str, "%d米", metre);
  130. }
  131. if (Timer(0, t))
  132. {
  133. BeginBatchDraw();
  134. cleardevice();
  135. int color = 255;
  136. for (int i = 0; i < 54; i++)
  137. {
  138. int index, x;
  139. if (j + i > 53)
  140. index = j + i - 54;
  141. else index = j + i;
  142. x = a[index];
  143. setfillcolor(RGB(color, color, 0));
  144. color -= 4;//道路颜色变淡
  145. solidrectangle(x, i * 20 + 1, x + 10, i * 20 + 21);
  146. x += 910;
  147. solidrectangle(x, i * 20 + 1, x + 10, i * 20 + 21);
  148. }
  149. player.show();
  150. Npc::loop(npc, j);
  151. for (int i = 3, y = 40 + j; i < 6; i++)//检测主角是否撞到道路
  152. {
  153. int index, x;
  154. if (y + i > 53)
  155. index = y + i - 54;
  156. else index = y + i;
  157. x = a[index];
  158. if (x + 10 > player.x || x + 865 < player.x)
  159. fail();
  160. }
  161. outtextxy(0, 0, str);
  162. if (j-- == -1)//j为道路最前面对应的数组下标,自减后是新一轮的下标
  163. {
  164. behind = 0;
  165. j = 53;
  166. }
  167. else behind = j + 1;
  168. if (c >= 13)
  169. dire = rand() % 3 - 1;
  170. if (dire == lastdire)
  171. c++;
  172. else c = 1;
  173. lastdire = dire;
  174. change = dire * 2 * (rand() % 4 + 1);
  175. int tmp = a[behind] + change;
  176. if (tmp > 0 && tmp <= 1000)
  177. a[j] = tmp;
  178. else a[j] = a[behind];
  179. //前面的路受后面影响
  180. EndBatchDraw();
  181. }
  182. }
  183. }

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

闽ICP备14008679号