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

news/2025/1/15 4:58:53/

文章目录 

  • 实现原理
  • 代码实现(详见注释)

实现原理

  • 首先拿到所有桌面APP的路径。
  • 然后将它们都存进结构体,并存入指针数组(前面的指向后面的)。
  • 后面直接每次随机生成食物并且每次判断是否KO就行了。
  • 感兴趣也可以看看C++贪吃蛇无尽模式。

代码实现(详见注释)

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

 


http://www.ppmy.cn/news/1052323.html

相关文章

学习maven工具

文章目录 &#x1f412;个人主页&#x1f3c5;JavaEE系列专栏&#x1f4d6;前言&#xff1a;&#x1f3e8;maven工具产生的背景&#x1f993;maven简介&#x1fa80;pom.xml文件(project object Model 项目对象模型) &#x1fa82;maven工具安装步骤两个前提&#xff1a;下载 m…

前端开发中移动端开发需要注意什么? - 易智编译EaseEditing

在前端开发中&#xff0c;移动端开发具有一些独特的挑战和注意事项。以下是移动端开发时需要注意的一些重要点&#xff1a; 响应式设计&#xff1a; 移动设备的屏幕尺寸和分辨率多样&#xff0c;因此要采用响应式设计&#xff0c;确保你的网站或应用在各种设备上都能良好地呈…

fastjson 序列化问题:Comparison method violates its general contract

fastjson 序列化问题&#xff1a;Comparison method violates its general contract 问题重现 今天在测试接口的时候&#xff0c;调用了Mybatis Plus 分页查询的接口&#xff0c;然后将查询的结果转换成 Json字符串的形式&#xff0c;结果报了这个错误&#xff1a; java.lang.…

端口监听常见方案

端口监听常见方案 一、原生Java import java.io.*; import java.net.ServerSocket; import java.net.Socket;public class SimpleServer {public static void main(String[] args) {int portNumber 8080; // 监听的端口号try (ServerSocket serverSocket new ServerSocket(…

410随身WiFi刷完openwrt后作玩具nas/bt机

简单流程介绍&#xff1a;机子wifi模式改为客户端&#xff0c;添加ipv6&#xff0c;开放需要的端口&#xff0c;切换USB工作模式&#xff0c;挂载usb储存&#xff0c;安装qb/aria2之类软件 前提就是刷好openwrt了&#xff0c;教程话题里有 注意opemwrt的操作要及时保存并应用&a…

CMAKE项目配置交叉编译选项

新建cmake-aarch64-libreelec-linux-gnueabi.conf配置文件&#xff0c;指定交叉编译相关选项 SET(CMAKE_SYSTEM_NAME Linux) SET(CMAKE_SYSTEM_VERSION 1) SET(CMAKE_SYSTEM_PROCESSOR aarch64) SET(CMAKE_C_COMPILER /path/toolchain/bin/aarch64-libreelec-linux-gnueabi-…

【微信小程序】小程序之间的跳转方式总结

想要从该小程序跳转到其他小程序怎么做&#xff1f; 方式 小程序之间的跳转方法有&#xff1a; wx.navigateTo&#xff1a;保留当前页面&#xff0c;跳转到应用内的某个页面&#xff0c;然后从该页面返回上一页的时候使用wx.navigateBack返回。wx.switchTab&#xff1a;跳转…

PostgreSQL-研究学习-介绍与安装

PostgreSQL-预研 是个很厉害的数据库的样子 ψ(*&#xff40;ー)ψ 官方文档&#xff1a;http://www.postgres.cn/docs/12/ 总的结论和备注 PgSQL 支持对JSON的支持很强大&#xff0c;以及提供了很多数学几何相关的数据类型【例&#xff1a;点&#xff0c;线条&#xff0c;几何…