当前位置:   article > 正文

贪吃蛇代码实现

贪吃蛇代码

一、代码实现

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<graphics.h>
  4. #include<stdlib.h>
  5. using namespace std;
  6. #define SNAKE_NUM 100
  7. #define HEIGHT 480
  8. #define WIDTH 640
  9. //枚举
  10. enum dir
  11. {
  12. UP,
  13. DOWN,
  14. LEFT,
  15. RIGHT
  16. };
  17. //蛇
  18. int score;
  19. struct Snake {
  20. int size;//
  21. int dir;
  22. int speed;
  23. POINT coor[SNAKE_NUM];
  24. }snake;
  25. //食物
  26. struct Food {
  27. int x;
  28. int y;
  29. bool flag;
  30. int size;
  31. DWORD color;
  32. }food;
  33. void GameInit()
  34. {
  35. //init 初始化 graph图形窗口
  36. initgraph(WIDTH, HEIGHT);
  37. //初始化蛇 一开始有三节
  38. //随机数种子 GetTickCount()获取系统开机到现在经过的毫秒数
  39. srand(GetTickCount());
  40. snake.size = 3;
  41. snake.speed = 10;
  42. snake.dir = RIGHT;
  43. for (int i =0; i <snake.size; i++) {
  44. snake.coor[i].x = snake.size*10-(i)*10;
  45. snake.coor[i].y = 10;
  46. printf("%d %d", snake.coor[i].x, snake.coor[i].y);
  47. }
  48. //随机生成食物
  49. food.x = rand() % WIDTH + 1;
  50. food.y = rand() % HEIGHT + 1;
  51. food.size = snake.speed / 2;
  52. food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
  53. food.flag = true;
  54. }
  55. void GameDraw() {
  56. setbkcolor(WHITE);
  57. cleardevice();
  58. setfillcolor(BLACK);
  59. //双缓冲绘图
  60. BeginBatchDraw();
  61. for (int i = 0; i < snake.size; i++) {
  62. solidcircle(snake.coor[i].x, snake.coor[i].y, snake.speed/2);
  63. }
  64. if (food.flag) {
  65. solidcircle(food.x, food.y, food.size);
  66. }
  67. EndBatchDraw();
  68. }
  69. void snakeMove() {
  70. for (int i = snake.size - 1; i > 0; i--) {
  71. snake.coor[i] = snake.coor[i - 1];
  72. }
  73. switch (snake.dir) {
  74. case UP:
  75. if (snake.coor[0].y - (snake.speed) / 2 <= 0) {
  76. snake.coor[0].y = HEIGHT;
  77. }
  78. snake.coor[0].y-=snake.speed;
  79. break;
  80. case DOWN:
  81. if (snake.coor[0].y + (snake.speed) / 2 >=640) {
  82. snake.coor[0].y = 0;
  83. }
  84. snake.coor[0].y += snake.speed;
  85. break;
  86. case LEFT:
  87. if (snake.coor[0].x - (snake.speed) / 2 <= 0) {
  88. snake.coor[0].x = WIDTH;
  89. }
  90. snake.coor[0].x-=snake.speed;
  91. break;
  92. case RIGHT:
  93. if (snake.coor[0].x + (snake.speed) / 2 >= 480) {
  94. snake.coor[0].x = 0;
  95. }
  96. snake.coor[0].x+=snake.speed;
  97. break;
  98. }
  99. }
  100. void keyControl() {
  101. //72 80 75 77 上下左右
  102. //kbhit
  103. if (_kbhit()) {
  104. switch (_getch()) {
  105. case 'w':
  106. case 72:
  107. if (snake.dir != DOWN) {
  108. snake.dir = UP;
  109. }
  110. break;
  111. case 's':
  112. case 80:
  113. if (snake.dir != UP) {
  114. snake.dir = DOWN;
  115. }
  116. break;
  117. case'a':
  118. case 75:
  119. if (snake.dir != RIGHT) {
  120. snake.dir = LEFT;
  121. }
  122. break;
  123. case'd':
  124. case 77:
  125. if (snake.dir != LEFT) {
  126. snake.dir = RIGHT;
  127. }
  128. break;
  129. case ' '://当按下空格时游戏暂停
  130. if (_getch() == ' ') {
  131. return;
  132. }
  133. break;
  134. }
  135. }
  136. }
  137. void eatFood() {
  138. if (food.x - snake.coor[0].x <= food.size && snake.coor[0].x - food.x <= food.size && food.y - snake.coor[0].y <= food.size && snake.coor[0].y - food.y <= food.size) {
  139. food.flag = false;
  140. snake.size++;
  141. score++;
  142. }
  143. if (!food.flag) {
  144. food.x = rand() % WIDTH + 1;
  145. food.y = rand() % HEIGHT + 1;
  146. food.size = snake.speed / 2;
  147. food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
  148. food.flag = true;
  149. }
  150. }
  151. int main() {
  152. GameInit();
  153. while (1) {
  154. GameDraw();
  155. snakeMove();
  156. keyControl();
  157. eatFood();
  158. Sleep(200);
  159. }
  160. return 0;
  161. }

二、easyx说明

eayx是针对c++的图形库,s可以帮助初学者上手游戏和图形化编程

 下载easyx库

easyx绘制图形坐标轴的原点位于界面的左上角,x轴向右为正,y轴向下为正,单位是像素点

Initgraph函数

参数

    width:绘图窗口的宽度。

    height:绘图窗口的高度。

    flag:绘图窗口的样式,默认为 NULL。可为以下值:

    值    含义

    EW_DBLCLKS    在绘图窗口中支持鼠标双击事件

    EW_NOCLOSE    禁用绘图窗口的关闭按钮。

    EW_NOMINIMIZE    禁用绘图窗口的最小化按钮。

    EW_SHOWCONSOLE    显示控制台窗口。

    ps:也可以不写前面的EW_


cleardevice函数

使用当前背景色清空窗口

setbkcolor

设置背景颜色

setfillcolor

设置填充颜色

绘制圆

  1. #include<stdio.h>
  2. //1.包含图形库头文件,就能使用提供给我的函数
  3. #include<graphics.h>
  4. int main()
  5. {
  6. //2.创建一个窗口,确定窗口大小,show_console(显现控制台)
  7. initgraph(640, 480, SHOWCONSOLE);
  8. //设置背景颜色,one,two两步才能设置背景颜色,位置不能颠倒
  9. setbkcolor(RGB(255,128,192));//one,设置背景颜色
  10. cleardevice();//two,清屏,初始化,清楚原来背景的黑色
  11. //3.画粑粑,圆
  12. setfillcolor(BLUE);//填充颜色为蓝色
  13. setlinecolor(YELLOW);//线的颜色为黄色
  14. circle(50, 50, 50);
  15. solidcircle(50, 250, 50);
  16. /*
  17. setbkcolor(GREEN);//重新设个背景色,更显著
  18. clearcircle(100, 150, 50);
  19. clearcircle(100, 250, 50);
  20. */
  21. getchar();
  22. //2.1关闭窗口
  23. closegraph();
  24. return 0;
  25. }

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

闽ICP备14008679号