|
<
文章目次
运转结果
传收
完成道理
- 起首拿到一切桌里APP的途径。
- 然后将它们皆存进构造体,并存进指针数组(前里的指背后背的)。
- 后背间接每次随机天生食品而且每次判定能否KO就好了。
- 感爱好也能够看看一般的贪吃蛇。
代码完成(详睹正文)
- #include <bits/stdc++.h>
- #include <windows.h>
- #include <commctrl.h>
- #include <shlobj.h>
- #define get GetAsyncKeyState
- #define send SendMessageA
- #define LVM LVM_SETITEMPOSITION
- #define msg MessageBox
- // 巨细取速率(速率越小越快)
- const int S = 100, V = 300;
- // 桌里
- HWND D;
- // 蛇的构造体
- typedef struct Snake {
- // 地位
- int x, y;
- // 蛇的第几节
- int idx;
- // 下一节
- struct Snake* nxt;
- } node;
- // 蛇头
- node* head;
- node* t;
- // 今朝食品的地位
- POINT food;
- // 今朝吃了几食品
- int cnt, idx;
- // 分辩率
- int w, h;
- // 共有几食品
- int sum;
- // 找途径
- char* Get() {
- LPITEMIDLIST pidl;
- LPMALLOC pShellMalloc;
- char s[200];
- if (SUCCEEDED(SHGetMalloc(&pShellMalloc))) {
- if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl))) {
- SHGetPathFromIDListA(pidl, s);
- pShellMalloc -> Free(pidl);
- }
- pShellMalloc -> Release();
- }
- return s;
- }
- // 初初化
- void init() {
- srand((unsigned int)time(0));
- HWND grand = FindWindowA("Progman", "Program Manager"), fa = FindWindowExA(grand, NULL, "SHELLDLL_DefView", NULL);
- D = FindWindowExA(fa, 0, "SysListView32", "FolderView");
- sum = send(D, LVM_GETITEMCOUNT, 0, 0);
- w = GetSystemMetrics(SM_CXSCREEN);
- h = GetSystemMetrics(SM_CYSCREEN);
- head = (node*) malloc (sizeof head);
- head -> x = rand() % (w / S) * S;
- head -> y = rand() % (h / S) * S;
- head -> idx = 0;
- head -> nxt = NULL;
- for (int i = 0; i < sum; ++i) {
- send(D, LVM, i, (h << 16) + w);
- }
- }
- // 游戏界里
- void Game() {
- // 输出蛇头
- send(D, LVM, head -> idx, (head -> y << 16) + head -> x);
- A:
- food.x = rand() % (w / S) * S;
- food.y = rand() % (h / S) * S;
- // 假如重生成的食品地位战蛇头地位重开便更生成
- if (head -> x == food.x && head -> y == food.y) {
- goto A;
- }
- // 输出食品
- send(D, LVM, 1, (food.y << 16) + food.x);
- // 挪动标的目的,最开端背左
- node move;
- move.x = 1;
- move.y = 0;
- // 皆吃了便胜利了
- while (cnt < sum) {
- if (get(VK_UP)) {
- move.x = 0;
- move.y = -1;
- }
- if (get(VK_DOWN)) {
- move.x = 0;
- move.y = 1;
- }
- if (get(VK_LEFT)) {
- move.x = -1;
- move.y = 0;
- }
- if (get(VK_RIGHT)) {
- move.x = 1;
- move.y = 0;
- }
- if (get(VK_ESCAPE)) {
- msg(D, TEXT("再会,下次再去玩呦~"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
- exit(0);
- }
- if (get(VK_SPACE)) {
- while (true) {
- Sleep(500);
- if (get(VK_SPACE)) {
- break;
- }
- }
- }
- if (head -> x == food.x && head -> y == food.y) {
- ++idx;
- ++cnt;
- node* T;
- T = (node*) malloc (sizeof(node));
- T -> x = food.x;
- T -> y = food.y;
- T -> idx = idx;
- T -> nxt = NULL;
- t = head;
- while (t -> nxt != NULL) {
- t = t -> nxt;
- }
- t -> nxt = T;
- t = head;
- t -> x += move.x * S;
- t -> y += move.y * S;
- while (t != NULL) {
- send(D, LVM, t -> idx, (t -> y << 16) + t -> x);
- t = t -> nxt;
- }
- B:
- food.x = rand() % (w / S) * S;
- food.y = rand() % (h / S) * S;
- if (head -> x == food.x && head -> y == food.y) {
- goto B;
- }
- send(D, LVM, idx + 1, (food.y << 16) + food.x);
- } else {
- node t1, t2;
- t = head;
- t1.x = t -> x;
- t1.y = t -> y;
- t -> x += move.x * S;
- t -> y += move.y * S;
- send(D, LVM, t -> idx, (t -> y << 16) + t -> x);
- t = head -> nxt;
- while (t != NULL) {
- t2.x = t -> x;
- t2.y = t -> y;
- t -> x = t1.x;
- t -> y = t1.y;
- send(D, LVM, t -> idx, (t -> y << 16) + t -> x);
- t1.x = t2.x;
- t1.y = t2.y;
- t = t -> nxt;
- }
- if (head -> x > w || head -> x < 0 || head -> y > h || head -> y < 0) {
- msg(D, TEXT("孩纸您碰到墙了,重去吧"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
- exit(0);
- }
- t = head -> nxt;
- while (t != NULL) {
- if (t -> x == head -> x && t -> y == head -> y) {
- msg(D, TEXT("孩纸您碰到本人了,重去吧"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
- exit(0);
- }
- t = t -> nxt;
- }
- }
- Sleep(V);
- }
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
- init();
- msg(D, TEXT("游戏划定规矩:↑↓←→掌握标的目的,空格停息,再按一次空格持续,Esc退出。要好好记着哦~"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
- msg(D, TEXT("筹办开端游戏..."), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
- Game();
- return 0;
- }
复造代码 免责声明:假如进犯了您的权益,请联络站少,我们会实时删除侵权内乱容,感谢协作! |
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
|