C++桌面贪吃蛇,能通关的都是大神(Windows可编译)

代码 代码 1533 人阅读 | 0 人回复

<
文章目次



运转结果

传收
完成道理



  • 起首拿到一切桌里APP的途径。
  • 然后将它们皆存进构造体,并存进指针数组(前里的指背后背的)。
  • 后背间接每次随机天生食品而且每次判定能否KO就好了。
  • 感爱好也能够看看一般的贪吃蛇
代码完成(详睹正文)

  1. #include <bits/stdc++.h>
  2. #include <windows.h>
  3. #include <commctrl.h>
  4. #include <shlobj.h>
  5. #define get GetAsyncKeyState
  6. #define send SendMessageA
  7. #define LVM LVM_SETITEMPOSITION
  8. #define msg MessageBox
  9. // 巨细取速率(速率越小越快)
  10. const int S = 100, V = 300;
  11. // 桌里
  12. HWND D;
  13. // 蛇的构造体
  14. typedef struct Snake {
  15.     // 地位
  16.         int x, y;
  17.     // 蛇的第几节
  18.         int idx;
  19.     // 下一节
  20.         struct Snake* nxt;
  21. } node;
  22. // 蛇头
  23. node* head;
  24. node* t;
  25. // 今朝食品的地位
  26. POINT food;
  27. // 今朝吃了几食品
  28. int cnt, idx;
  29. // 分辩率
  30. int w, h;
  31. // 共有几食品
  32. int sum;
  33. // 找途径
  34. char* Get() {
  35.         LPITEMIDLIST pidl;
  36.         LPMALLOC pShellMalloc;
  37.         char s[200];
  38.         if (SUCCEEDED(SHGetMalloc(&pShellMalloc))) {
  39.                 if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl)))        {
  40.                         SHGetPathFromIDListA(pidl, s);
  41.                         pShellMalloc -> Free(pidl);
  42.                 }
  43.                 pShellMalloc -> Release();
  44.         }
  45.         return s;
  46. }
  47. // 初初化
  48. void init() {
  49.         srand((unsigned int)time(0));
  50.         HWND grand = FindWindowA("Progman", "Program Manager"), fa = FindWindowExA(grand, NULL, "SHELLDLL_DefView", NULL);
  51.         D = FindWindowExA(fa, 0, "SysListView32", "FolderView");
  52.         sum = send(D, LVM_GETITEMCOUNT, 0, 0);
  53.         w = GetSystemMetrics(SM_CXSCREEN);
  54.         h = GetSystemMetrics(SM_CYSCREEN);
  55.         head = (node*) malloc (sizeof head);
  56.         head -> x = rand() % (w / S) * S;
  57.         head -> y = rand() % (h / S) * S;
  58.         head -> idx = 0;
  59.         head -> nxt = NULL;
  60.         for (int i = 0; i < sum; ++i) {
  61.                 send(D, LVM, i, (h << 16) + w);
  62.         }
  63. }
  64. // 游戏界里
  65. void Game() {
  66.         // 输出蛇头
  67.         send(D, LVM, head -> idx, (head -> y << 16) + head -> x);
  68. A:
  69.         food.x = rand() % (w / S) * S;
  70.         food.y = rand() % (h / S) * S;
  71.         // 假如重生成的食品地位战蛇头地位重开便更生成
  72.         if (head -> x == food.x && head -> y == food.y) {
  73.                 goto A;
  74.         }
  75.         // 输出食品
  76.         send(D, LVM, 1, (food.y << 16) + food.x);
  77.         // 挪动标的目的,最开端背左
  78.         node move;
  79.         move.x = 1;
  80.         move.y = 0;
  81.         // 皆吃了便胜利了
  82.         while (cnt < sum) {
  83.                 if (get(VK_UP)) {
  84.                         move.x = 0;
  85.                         move.y = -1;
  86.                 }
  87.                 if (get(VK_DOWN)) {
  88.                         move.x = 0;
  89.                         move.y = 1;
  90.                 }
  91.                 if (get(VK_LEFT)) {
  92.                         move.x = -1;
  93.                         move.y = 0;
  94.                 }
  95.                 if (get(VK_RIGHT)) {
  96.                         move.x = 1;
  97.                         move.y = 0;
  98.                 }
  99.                 if (get(VK_ESCAPE)) {
  100.                         msg(D, TEXT("再会,下次再去玩呦~"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
  101.                         exit(0);
  102.                 }
  103.                 if (get(VK_SPACE)) {
  104.                         while (true) {
  105.                                 Sleep(500);
  106.                                 if (get(VK_SPACE)) {
  107.                                         break;
  108.                                 }
  109.                         }
  110.                 }
  111.                 if (head -> x == food.x && head -> y == food.y) {
  112.                         ++idx;
  113.                         ++cnt;
  114.                         node* T;
  115.                         T = (node*) malloc (sizeof(node));
  116.                         T -> x = food.x;
  117.                         T -> y = food.y;
  118.                         T -> idx = idx;
  119.                         T -> nxt = NULL;
  120.                         t = head;
  121.                         while (t -> nxt != NULL) {
  122.                                 t = t -> nxt;
  123.                         }
  124.                         t -> nxt = T;
  125.                         t = head;
  126.                         t -> x += move.x * S;
  127.                         t -> y += move.y * S;
  128.                         while (t != NULL) {
  129.                                 send(D, LVM, t -> idx, (t -> y << 16) + t -> x);
  130.                                 t = t -> nxt;
  131.                         }
  132.                 B:
  133.                         food.x = rand() % (w / S) * S;
  134.                         food.y = rand() % (h / S) * S;
  135.                         if (head -> x == food.x && head -> y == food.y) {
  136.                                 goto B;
  137.                         }
  138.                         send(D, LVM, idx + 1, (food.y << 16) + food.x);
  139.                 } else {
  140.                         node t1, t2;
  141.                         t = head;
  142.                         t1.x = t -> x;
  143.                         t1.y = t -> y;
  144.                         t -> x += move.x * S;
  145.                         t -> y += move.y * S;
  146.                         send(D, LVM, t -> idx, (t -> y << 16) + t -> x);
  147.                         t = head -> nxt;
  148.                         while (t != NULL) {
  149.                                 t2.x = t -> x;
  150.                                 t2.y = t -> y;
  151.                                 t -> x = t1.x;
  152.                                 t -> y = t1.y;
  153.                                 send(D, LVM, t -> idx, (t -> y << 16) + t -> x);
  154.                                 t1.x = t2.x;
  155.                                 t1.y = t2.y;
  156.                                 t = t -> nxt;
  157.                         }
  158.                         if (head -> x > w || head -> x < 0 || head -> y > h || head -> y < 0) {
  159.                                 msg(D, TEXT("孩纸您碰到墙了,重去吧"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
  160.                                 exit(0);
  161.                         }
  162.                         t = head -> nxt;
  163.                         while (t != NULL) {
  164.                                 if (t -> x == head -> x && t -> y == head -> y) {
  165.                                         msg(D, TEXT("孩纸您碰到本人了,重去吧"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
  166.                                         exit(0);
  167.                                 }
  168.                                 t = t -> nxt;
  169.                         }
  170.                 }
  171.                 Sleep(V);
  172.         }
  173. }
  174. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
  175.         init();
  176.         msg(D, TEXT("游戏划定规矩:↑↓←→掌握标的目的,空格停息,再按一次空格持续,Esc退出。要好好记着哦~"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
  177.         msg(D, TEXT("筹办开端游戏..."), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
  178.         Game();
  179.         return 0;
  180. }
复造代码
免责声明:假如进犯了您的权益,请联络站少,我们会实时删除侵权内乱容,感谢协作!
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
回复 关闭延时

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则