win32项目——贪吃蛇

我的第一个win32项目----贪吃蛇

图片资源:
KUANGK
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
头文件resource.h代码:

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ 生成的包含文件。
// 供 6-14上午.rc 使用
//
#define IDB_BITMAP1                     101
#define IDB_BITMAP2                     102
#define IDB_BITMAP3                     103
#define IDB_BITMAP4                     104
#define IDB_BITMAP5                     105
#define IDB_BITMAP6                     106
#define IDB_BITMAP7                     107

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        108
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

资源.rc文件代码:

// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"

/
#undef APSTUDIO_READONLY_SYMBOLS

/
// 中文(简体,中国) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED

#ifdef APSTUDIO_INVOKED
/
//
// TEXTINCLUDE
//

1 TEXTINCLUDE 
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE 
BEGIN
    "#include ""winres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE 
BEGIN
    "\r\n"
    "\0"
END

#endif    // APSTUDIO_INVOKED


/
//
// Bitmap
//

IDB_BITMAP1             BITMAP                  "..\\she\\背景.bmp"
												 
IDB_BITMAP2             BITMAP                  "..\\she\\苹果.bmp"
												 
IDB_BITMAP3             BITMAP                  "..\\she\\蛇身.bmp"
												 
IDB_BITMAP4             BITMAP                  "..\\she\\蛇头0.bmp"
												 
IDB_BITMAP5             BITMAP                  "..\\she\\蛇头1.bmp"
												 
IDB_BITMAP6             BITMAP                  "..\\she\\蛇头2.bmp"
												 
IDB_BITMAP7             BITMAP                  "..\\she\\蛇头3.bmp"
												 
#endif    // 中文(简体,中国) resources
/



#ifndef APSTUDIO_INVOKED
/
//
// Generated from the TEXTINCLUDE 3 resource.
//


/
#endif    // not APSTUDIO_INVOKED


main.c代码:

#define _CRT_SECURE_NO_WARNINGS
#include<windows.h>
#include<stdio.h>
#include<time.h>
#include"resource.h"

typedef struct NODE
{
	int x; 
	int y;
	struct NODE *pLast;
	struct NODE *pNext;
}Snake,Apple;

HBITMAP hBitmap_Background;
HBITMAP hBitmap_Apple;
HBITMAP hBitmap_SnackBody;
HBITMAP hBitmap_SnackHead_Up;
HBITMAP hBitmap_SnackHead_Down;
HBITMAP hBitmap_SnackHead_Left;
HBITMAP hBitmap_SnackHead_Right;

enum Direct { Up, Down, Left, Right };
Snake *pHead = NULL;
Snake *pTail = NULL;
enum Direct dir = Up;
Apple apple = { 5,5,NULL,NULL};
BOOL flag = TRUE;

LRESULT CALLBACK MYWNDPROC(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void ShowBackground(HDC hdc);
void ShowSnake(HDC hdc);
void AddNode(int x, int y);
void SnakeMove();
void ShowApple(HDC hdc);
BOOL IsEatApple();
void NewAppple();
BOOL IsBumpWall();
BOOL IsEatItself();

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR IpCmdLline, int nCmdShow) {
	WNDCLASSEX ex;
	HWND hWnd;
	MSG Msg;

	//加载位图
	hBitmap_Background = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP1));
	hBitmap_Apple = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP2));
	hBitmap_SnackBody = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP3));
	hBitmap_SnackHead_Up = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP5));
	hBitmap_SnackHead_Down = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP7));
	hBitmap_SnackHead_Left = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP6));
	hBitmap_SnackHead_Right = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP4));

	AddNode(10, 15);
	AddNode(10, 16);
	AddNode(10, 17);


	// 1.设计窗口
	ex.style =  CS_HREDRAW | CS_VREDRAW;
	ex.cbSize = sizeof(ex);
	ex.cbClsExtra = 0;
	ex.cbWndExtra = 0;
	ex.hInstance = hInstance;
	ex.hIcon = NULL;
	ex.hCursor = NULL;
	ex.hbrBackground = CreateSolidBrush(RGB(0, 255, 0));
	ex.hIconSm = NULL;
	ex.lpfnWndProc = &MYWNDPROC;				//To Do
	ex.lpszMenuName = NULL;
	ex.lpszClassName = "aaa";

	// 2.注册
	RegisterClassEx(&ex);

	// 3.创建
	hWnd = CreateWindow(ex.lpszClassName, "贪吃蛇", WS_OVERLAPPEDWINDOW, 50, 50,615, 638, NULL, NULL, hInstance, NULL);

	// 4.显示
	ShowWindow(hWnd, SW_SHOW);

	// 设置定时器
	SetTimer(hWnd, 1, 200, NULL);

	srand((unsigned int)time(NULL));

	// 消息循环
	while (GetMessage(&Msg, NULL, 0, 0)) {
		// 翻译消息
		TranslateMessage(&Msg);
		// 分配消息
		DispatchMessage(&Msg);
	}



	return 0;
}

LRESULT CALLBACK MYWNDPROC(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	
	PAINTSTRUCT ps;
	HDC hdc;
	HDC hdcmen;
	switch (message)
	{

	case WM_CLOSE:
		PostQuitMessage(0);
		break;
	case WM_KEYUP:
		if (wParam == 65) {
			KillTimer(hWnd, 2);
		}
		break;
	case WM_KEYDOWN:
		 
		if (wParam == 65) {
			SetTimer(hWnd, 2,100,NULL);
		}

		if (flag == TRUE) {
			switch (wParam)
			{
			case VK_UP:
				if (dir != Down) {
					dir = Up;
				}
				break;
			case VK_DOWN:
				if (dir != Up) {
					dir = Down;
				}
				break;
			case VK_LEFT:
				if (dir != Right) {
					dir = Left;
				}
				break;
			case VK_RIGHT:
				if (dir != Left) {
					dir = Right;
				}
				break;
			default:
				break;
			}
			flag = FALSE;
		}
		hdc = GetDC(hWnd);
		//显示背景
		ShowBackground(hdc);
		//显示蛇
		ShowSnake(hdc);
		//显示苹果
		ShowApple(hdc);
		ReleaseDC(hWnd, hdc);
		break;

	case WM_PAINT:						//重绘消息

		hdc = BeginPaint(hWnd, &ps);
		//显示背景
		ShowBackground(hdc);
		//显示蛇
		ShowSnake(hdc);
		//显示苹果
		ShowApple(hdc);
		EndPaint(hWnd, &ps);
		break;
	case WM_TIMER:
		flag = TRUE;
		SnakeMove();
		//判断是否自咬或者撞墙
		if (IsBumpWall() || IsEatItself()) {
			//结束游戏
			KillTimer(hWnd, 1);
			MessageBox(hWnd, "游戏结束","提示",MB_OK);

		}
		//判断是否吃到苹果
		if (IsEatApple()) {     //吃到了苹果
			//长个
			AddNode(-10, -10);
			//新苹果
			NewAppple();
		}
		hdc = GetDC(hWnd);
		//显示背景
		ShowBackground(hdc);
		//显示蛇
		ShowSnake(hdc);
		//显示苹果
		ShowApple(hdc);
		ReleaseDC(hWnd, hdc);
		break;
	}


	return DefWindowProc(hWnd, message, wParam, lParam);
}

void ShowBackground(HDC hdc) {
	// 1.创建兼容性DC
	
	HDC hdcmen = CreateCompatibleDC(hdc);

	// 2.选择一张位图
	SelectObject(hdcmen, hBitmap_Background);

	// 3.传输
	BitBlt(hdc, 0, 0, 600, 600, hdcmen, 0, 0, SRCCOPY);

	// 4.删除兼容性DC
	DeleteDC(hdcmen);

}

void AddNode(int x, int y) {
	Snake *pTemp = (Snake*)malloc(sizeof(Snake));
	pTemp->x = x;
	pTemp->y = y;
	pTemp->pLast = NULL;
	pTemp->pNext = NULL;

	if (pHead == NULL) {
		pHead = pTemp;
	}
	else {
		pTail->pNext = pTemp;
		pTemp->pLast = pTail;
	}
	pTail = pTemp;
}

void ShowSnake(HDC hdc) {
	Snake *pMark = pHead;
	// 1.显示蛇头
	HDC hdcmen = CreateCompatibleDC(hdc);
	switch (dir)
	{
	case Up:
		SelectObject(hdcmen, hBitmap_SnackHead_Up);
		break;
	case Down:
		SelectObject(hdcmen, hBitmap_SnackHead_Down);
		break;
	case Left:
		SelectObject(hdcmen, hBitmap_SnackHead_Left);
		break;
	case Right:
		SelectObject(hdcmen, hBitmap_SnackHead_Right);
		break;
	default:
		break;
	}
	BitBlt(hdc, pMark->x * 30, pMark->y * 30, 30, 30, hdcmen, 0, 0, SRCCOPY);

	// 2.显示蛇身
	SelectObject(hdcmen, hBitmap_SnackBody);
	pMark = pMark->pNext;
	while (pMark != NULL) {
		BitBlt(hdc, pMark->x * 30, pMark->y * 30,30,30,hdcmen, 0, 0, SRCCOPY);
		pMark = pMark->pNext;
	}

	DeleteDC(hdcmen);

}

void SnakeMove() {

	Snake *pMark = pTail;
	// 1.先移动蛇身
	while (pMark != pHead) {
		pMark->x = pMark->pLast->x;
		pMark->y = pMark->pLast->y;
		pMark = pMark->pLast;
	}
	// 2.根据方向移动蛇头
	switch (dir)
	{
	case Up:
		pHead->y--;
		break;
	case Down:
		pHead->y++;
		break;
	case Left:
		pHead->x--;
		break;
	case Right:
		pHead->x++;
		break;
	}
}

void ShowApple(HDC hdc) {

	HDC hdcmen = CreateCompatibleDC(hdc);
	SelectObject(hdcmen, hBitmap_Apple);
	BitBlt(hdc, apple.x * 30, apple.y * 30, 30, 30, hdcmen, 0, 0, SRCCOPY);
	DeleteDC(hdcmen);

}

BOOL IsEatApple() {
	if (pHead->x == apple.x && pHead->y == apple.y) {

		return TRUE;
	}

	return FALSE;

}

void NewAppple() {

	int x, y;
	Snake *pMark = pHead;
	do {
		//1.随机生成两个坐标   范围0-18
		x = rand() % 18 + 1;
		y = rand() % 18 + 1;

		//2,判断是否在蛇的身上
		pMark = pHead;
		while (pMark != NULL)
		{
			if (pMark->x == x && pMark->y == y) {
				break;
			}
			pMark = pMark->pNext;
		}
	} while (pMark != NULL);
	//3.如果不在蛇的身上赋给苹果结构体
	apple.x = x;
	apple.y = y;

}

BOOL IsBumpWall() {
	if (pHead->x == 0 || pHead->x == 19 || pHead->y == 0 || pHead->y == 19) {
		return TRUE;
	}
	return FALSE;
}

BOOL IsEatItself() {
	Snake *pMark = pHead->pNext;
	while (pMark != NULL)
	{
		if (pMark->x == pHead->x && pMark->y == pHead->y) {
			return TRUE;
		}

		pMark = pMark->pNext;

	}
	return FALSE;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值