当前位置:   article > 正文

基于easyx库的C/C++游戏编程实例-贪吃蛇|

基于easyx库的C/C++游戏编程实例-贪吃蛇|

贪吃蛇|双人贪吃蛇游戏设计

1.设计物体结构

蛇结构:

  1. struct SnakePlayer
  2. {
  3. int size;
  4. int r;
  5. int dir;
  6. int speed;
  7. POINT pos[SNAKE_SIZE];
  8. bool live;
  9. int score;
  10. }snake;

食物结构:

  1. struct Food
  2. {
  3. int x;
  4. int y;
  5. int r;
  6. bool flag;
  7. DWORD color;
  8. }food,foodtest[FOOD_SIZE];
2.游戏初始化
  1. void GameInit()
  2. {
  3. //播放背景音乐
  4. mciSendString(_T("open ./res/Snake_BGM.mp3 alias BGM"), 0, 0, 0);
  5. mciSendString(_T("play BGM repeat"), 0, 0, 0);
  6. //创建一个窗口,控制窗口台是自动创建的,图形窗口是需要自己手动创建的(后加 SHOWCONSOLE)
  7. initgraph(WIN_WIDTH, WIN_HEIGHT);
  8. //加载开始界面
  9. loadimage(&bg, _T("./res/Snake_Background.jpg"));
  10. putimage(0, 0, &bg);
  11. //让随机数每次进入时都不同,只调用一次!!!
  12. srand(GetTickCount());
  13. //开始界面
  14. setbkmode(TRANSPARENT);//设置透明背景
  15. settextcolor(GREEN);
  16. settextstyle(60, 0, _T("黑体"));
  17. outtextxy(WIDTH / 2, HEIGHT / 2 - 70,_T("贪吃蛇" ));
  18. settextstyle(20, 0, _T("黑体"));
  19. outtextxy(WIDTH / 2 + 30, HEIGHT / 2 + 20, _T("按'C'开始游戏" ));
  20. while (!GetAsyncKeyState('C'));
  21. //蛇初始状态
  22. snake.size = SIZE_BEGIN;
  23. snake.r = R_BEGIN;
  24. snake.dir = RIGHT;
  25. snake.speed=SPEED_MIN;
  26. snake.live = true;
  27. snake.score = 0;
  28. for (int i = 0; i < snake.size; i++)
  29. {
  30. snake.pos[i].x = X_BEGIN - 10 * i;
  31. snake.pos[i].y = Y_BEGIN;
  32. }
  33. //产生食物
  34. ProduceFood();
  35. }
3.游戏界面加载
  1. void GameDraw()
  2. {
  3. BeginBatchDraw();
  4. setbkcolor(RGB(28, 115, 119));
  5. cleardevice();//是用当前背景色清空屏幕,并将当前点移至 (0, 0),EASYX自带
  6. //游戏外界面
  7. settextcolor(GREEN);
  8. settextstyle(50, 0, _T("黑体"));
  9. outtextxy(WIDTH + 10, 20, _T("贪吃蛇" ));
  10. settextstyle(20, 0, _T("黑体"));
  11. outtextxy(WIDTH + 10, 120, _T("开始/继续游戏——C"));
  12. outtextxy(WIDTH + 10, 150, _T("重新开始——M"));
  13. outtextxy(WIDTH + 10, 180, _T("暂停游戏——空格"));
  14. outtextxy(WIDTH + 10, 210,_T("上——W或↑"));
  15. outtextxy(WIDTH + 10, 240,_T("下——S或↓"));
  16. outtextxy(WIDTH + 10, 270, _T("左——A或←"));
  17. outtextxy(WIDTH + 10, 300,_T("右——D或→"));
  18. outtextxy(WIDTH + 10, 330, _T("加速——B"));
  19. //line(0, HEIGHT, WIDTH, HEIGHT);//画直线
  20. rectangle(0, 0, WIDTH, HEIGHT);//画矩形边框
  21. settextcolor(BLACK);
  22. settextstyle(20, 0, _T("黑体"));
  23. outtextxy(10, HEIGHT + 5,_T("当前分数:"));
  24. char t[SNAKE_SIZE];
  25. sprintf_s(t, "%d", snake.score);
  26. outtextxy(140, HEIGHT + 5, *t);
  27. for (int i = 0; i < snake.size; i++)
  28. {
  29. setfillcolor(RED);
  30. solidcircle(snake.pos[i].x, snake.pos[i].y, snake.r);
  31. }
  32. if (food.flag)
  33. {
  34. setfillcolor(food.color);
  35. solidcircle(food.x, food.y, food.r);
  36. }
  37. /*//测试用
  38. for (int j = 0; j < FOOD_SIZE; j++)
  39. {
  40. if (foodtest[j].flag)
  41. {
  42. setfillcolor(foodtest[j].color);
  43. solidcircle(foodtest[j].x, foodtest[j].y, foodtest[j].r);
  44. }
  45. }*/
  46. EndBatchDraw();
  47. }
4.随机生成一个食物
  1. void ProduceFood()
  2. {
  3. food.r = rand() % 3 + 7;
  4. //随机生成食物位置,不能在蛇的身体上
  5. food.x = rand() % (WIDTH - 30) + food.r;
  6. food.y = rand() % (HEIGHT - 30) + food.r;
  7. while(1)
  8. {
  9. int i = 0;
  10. for(; i < snake.size; i++)
  11. {
  12. if (snake.pos[i].x + snake.r >= food.x - food.r && snake.pos[i].x - snake.r <= food.x + food.r
  13. && snake.pos[i].y + snake.r >= food.y - food.r && snake.pos[i].y - snake.r <= food.y + food.r)
  14. {
  15. food.x = rand() % (WIDTH - 30) + food.r;
  16. food.y = rand() % (HEIGHT - 30) + food.r;
  17. break;
  18. }
  19. }
  20. if (i == snake.size)
  21. break;
  22. }
  23. //随机生成食物颜色,不能与背景相同
  24. food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
  25. while (food.color == RGB(28, 115, 119))
  26. {
  27. food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
  28. }
  29. food.flag = true;
  30. }
5.蛇吃食物机制
  1. void EatFood()
  2. {
  3. if (food.flag && snake.pos[0].x+snake.r > food.x - food.r && snake.pos[0].x - snake.r < food.x + food.r
  4. && snake.pos[0].y + snake.r > food.y - food.r && snake.pos[0].y - snake.r < food.y + food.r)
  5. {
  6. food.flag = false;
  7. snake.size++;
  8. snake.score++;
  9. }
  10. if (!food.flag)
  11. {
  12. ProduceFood();
  13. }
  14. }
6.控制蛇移动

蛇移动机制:

  1. void SnakeMove()
  2. {
  3. //蛇的身体移动
  4. for (int i = snake.size - 1; i > 0; i--)
  5. {
  6. //Sleep(1);
  7. snake.pos[i] = snake.pos[i - 1];
  8. }
  9. //碰墙死亡机制
  10. switch (snake.dir)
  11. {
  12. case UP:
  13. snake.pos[0].y -= snake.speed;
  14. break;
  15. case DOWN:
  16. snake.pos[0].y += snake.speed;
  17. break;
  18. case LEFT:
  19. snake.pos[0].x -= snake.speed;
  20. break;
  21. case RIGHT:
  22. snake.pos[0].x += snake.speed;
  23. break;
  24. }
  25. //穿墙所需机制
  26. /*
  27. switch (snake.dir)
  28. {
  29. case UP:
  30. snake.pos[0].y -= speed;
  31. if (snake.pos[0].y < 0)
  32. {
  33. snake.pos[0].y = HEIGHT;
  34. }
  35. break;
  36. case DOWN:
  37. snake.pos[0].y += speed;
  38. if (snake.pos[0].y > HEIGHT)
  39. {
  40. snake.pos[0].y = 0;
  41. }
  42. break;
  43. case LEFT:
  44. snake.pos[0].x -= speed;
  45. if (snake.pos[0].x < 0)
  46. {
  47. snake.pos[0].x = WIDTH;
  48. }
  49. break;
  50. case RIGHT:
  51. snake.pos[0].x += speed;
  52. if (snake.pos[0].x > WIDTH)
  53. {
  54. snake.pos[0].x = 0;
  55. }
  56. break;
  57. }*/
  58. }

按键控制:

  1. void KeyCtrl()
  2. {
  3. //Windows函数, 非阻塞
  4. //按键大写可检测到大小写,小写都检测不到
  5. //按键需要顺序+else if,这样目前检测连续按不能往回走
  6. if (GetAsyncKeyState(VK_UP) || GetAsyncKeyState('W'))
  7. {
  8. if (snake.dir != DOWN)
  9. {
  10. snake.dir = UP;
  11. }
  12. }
  13. else if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A'))
  14. {
  15. if (snake.dir != RIGHT)
  16. {
  17. snake.dir = LEFT;
  18. }
  19. }
  20. else if (GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState('S'))
  21. {
  22. if (snake.dir != UP)
  23. {
  24. snake.dir = DOWN;
  25. }
  26. }
  27. else if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D'))
  28. {
  29. if (snake.dir != LEFT)
  30. {
  31. snake.dir = RIGHT;
  32. }
  33. }
  34. if (GetAsyncKeyState('B'))
  35. {
  36. snake.speed += SPEED_GAP;
  37. if (snake.speed>= SPEED_MAX)
  38. {
  39. snake.speed = SPEED_MIN;
  40. }
  41. }
  42. if (GetAsyncKeyState(VK_SPACE))
  43. {
  44. while (!GetAsyncKeyState('C'));
  45. }
  46. }
7.蛇碰撞死亡机制
  1. bool Bump()
  2. {
  3. //碰身体死亡
  4. for (int i = 4; i < snake.size; i++) //最小情况为4
  5. {
  6. if (snake.pos[0].x == snake.pos[i].x && snake.pos[0].y == snake.pos[i].y)
  7. {
  8. //snake.live = false;
  9. return false;
  10. }
  11. }
  12. //碰墙死亡
  13. if (snake.dir == LEFT && snake.pos[0].x - snake.r<0 || snake.dir == RIGHT && snake.pos[0].x + snake.r>WIDTH
  14. || snake.dir == UP && snake.pos[0].y - snake.r<0 || snake.dir == DOWN && snake.pos[0].y + snake.r>HEIGHT)
  15. {
  16. //snake.live = false;
  17. return false;
  18. }
  19. return true; // 默认true,可不写
  20. }
8.执行
  1. int main()
  2. {
  3. while (1)
  4. {
  5. GameInit();
  6. while (1)
  7. {
  8. //Producefoodtest();
  9. GameDraw();
  10. EatFood();
  11. KeyCtrl();
  12. SnakeMove();
  13. FlushBatchDraw();
  14. Sleep(100); //可以让速度更慢
  15. if (!Bump() || GetAsyncKeyState('M'))
  16. break;
  17. }
  18. //播放死亡时的背景音乐
  19. mciSendString(_T("close BGM"), 0, 0, 0);
  20. mciSendString(_T("open ./res/Snake_Dead.mp3"), 0, 0, 0);
  21. mciSendString(_T("play ./res/Snake_Dead.mp3"), 0, 0, 0);
  22. settextcolor(RED);
  23. settextstyle(40, 0, _T("黑体"));
  24. outtextxy(WIDTH + 7, 400, _T("GAME OVER!"));
  25. settextstyle(20, 0, _T("黑体"));
  26. outtextxy(WIDTH + 10, 450, _T("获得总分:"));
  27. settextstyle(30, 0, _T("黑体"));
  28. char t[SNAKE_SIZE];
  29. sprintf_s(t, "%d", snake.score);
  30. outtextxy(WIDTH + 140, 445, *t);
  31. while (!GetAsyncKeyState('M'));
  32. //停住播放。不能和play相邻,它会立即关闭,因为程序执行太快!
  33. mciSendString(_T("close ./res/Snake_Dead.mp3"), 0, 0, 0);
  34. }
  35. return 0;
  36. }

结果演示视频:

可见,我还对游戏画面进行了优化,添加了文字描述,画面较为美观。

基于此设计,我们可以衍生出双人贪吃蛇的游戏设计,这只不过是在贪吃蛇设计上添加一个蛇,再修改一下碰撞机制,比如说双方碰撞检测:

  1. for (int k = 1; k < snake[i].size; k++)
  2. {
  3. if (i + 1 < SNAKE_NUM&&snake[i].live && snake[i+1].live)
  4. {
  5. if (snake[i].pos[0].x - snake[i].r < snake[i + 1].pos[k].x + snake[i].r && snake[i].pos[0].x + snake[i].r > snake[i + 1].pos[k].x - snake[i].r
  6. && snake[i].pos[0].y - snake[i].r < snake[i + 1].pos[k].y + snake[i].r && snake[i].pos[0].y + snake[i].r > snake[i + 1].pos[k].y - snake[i].r)
  7. {
  8. snake[i].live = false;
  9. DeadFood(i);
  10. }
  11. }
  12. if ((i - 1) >= 0&&snake[i].live && snake[i - 1].live)
  13. {
  14. if (snake[i].pos[0].x - snake[i].r < snake[i - 1].pos[k].x + snake[i].r && snake[i].pos[0].x + snake[i].r > snake[i - 1].pos[k].x - snake[i].r
  15. && snake[i].pos[0].y - snake[i].r < snake[i - 1].pos[k].y + snake[i].r && snake[i].pos[0].y + snake[i].r > snake[i - 1].pos[k].y - snake[i].r)
  16. {
  17. snake[i].live = false;
  18. DeadFood(i);
  19. }
  20. }
  21. }

还可以添加一些玩法,增加游戏的可玩性。此外,可以设计游戏初始画面和结束画面,添加鼠标控制等。以下是我的双人贪吃蛇设计:

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

闽ICP备14008679号