桌面贪吃蛇

· · 个人记录

游戏信息:这是一个整活游戏

游戏作者:RockyYue_

游戏团队:《CCF收钱员模拟器》开发组

支持系统:windows7、windows10

最新版本:1.0.0

#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;
}