win.cpp代码如下
// win1.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "win1.h"
#define MAX_LOADSTRING 100
#define MAX_wide 840
#define MAX_long 600
// 全局变量:
HINSTANCE hInst; // 当前实例
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
hbrush1_SH = CreateSolidBrush(Color_hbrush_1_SH); //创建蛇体颜色画刷
hbrush2_QSSH= CreateSolidBrush(Color_hbrush_2_QCSH); //创建白色画刷
hbrush3_SW= CreateSolidBrush(Color_hbrush_3_SW); //创建食物颜色画刷
// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WIN1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN1));
MSG msg;
// 主消息循环:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
static TCHAR szAppName[] = TEXT("Checker3");
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN1));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WIN1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 将实例句柄存储在全局变量中
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 840, 600, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
RECT rect;
static POINT point;
HDC hdc;
static POINT pt[10000];
static int iCount; //蛇移动状态
switch (message)
{
case WM_CREATE: //第一个消息
SetTimer(hWnd, Time_1, Rate, NULL); //设置时钟,1000毫秒
point = { 300,300 };
break;
case WM_TIMER: //定时器
if (she_shiwu_ZT)
{
she_shiwu_ZT = shiwu_rand(hWnd);
}
working_she_move(hWnd); //蛇体的绘制
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_CHAR:
switch (wParam)
{
case ' ':
she_wait = ~she_wait;
break;
default:
break;
}
break;
case WM_KEYDOWN:
if (she_wait)
{
switch (wParam)
{
case VK_RIGHT:
she_head = she_right;
return 0;
case VK_LEFT:
she_head = she_left;
return 0;
case VK_UP:
she_head = she_up;
return 0;
case VK_DOWN:
she_head = she_down;
return 0;
default:
return 0;
}
}
break;
case WM_LBUTTONDOWN: //鼠标左键按下
return 0;
case WM_RBUTTONUP: //鼠标右键按起
restting(hWnd);
return 0;
case WM_MOUSEMOVE: //鼠标移动
return 0;
case WM_LBUTTONUP: //鼠标左键按起
return 0;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps); //获取设备环境句柄
//TODO: 在此处添加使用 hdc 的任何绘图代码...
GetClientRect(hWnd, &rect);
working(hWnd, hdc, rect); //绘制图形的具体代码
EndPaint(hWnd, &ps); //释放设备环境句柄
working_sheti_Initialize(hWnd); //初始化蛇体
}
break;
case WM_DESTROY:
KillTimer(hWnd, Time_1); //结束时钟
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
stdafx.h 代码如下
//
#include "win1.h"
#define MAX_wide 840
#define MAX_long 600
HINSTANCE hInst; // 当前实例
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
hbrush1_SH = CreateSolidBrush(Color_hbrush_1_SH); //创建蛇体颜色画刷
hbrush2_QSSH= CreateSolidBrush(Color_hbrush_2_QCSH); //创建白色画刷
hbrush3_SW= CreateSolidBrush(Color_hbrush_3_SW); //创建食物颜色画刷
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WIN1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
static TCHAR szAppName[] = TEXT("Checker3");
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN1));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WIN1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
}
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 将实例句柄存储在全局变量中
CW_USEDEFAULT, CW_USEDEFAULT, 840, 600, nullptr, nullptr, hInstance, nullptr);
{
return FALSE;
}
UpdateWindow(hWnd);
}
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
RECT rect;
static POINT point;
HDC hdc;
static POINT pt[10000];
static int iCount; //蛇移动状态
{
case WM_CREATE: //第一个消息
SetTimer(hWnd, Time_1, Rate, NULL); //设置时钟,1000毫秒
point = { 300,300 };
break;
if (she_shiwu_ZT)
{
she_shiwu_ZT = shiwu_rand(hWnd);
}
working_she_move(hWnd); //蛇体的绘制
break;
{
int wmId = LOWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
switch (wParam)
{
case ' ':
she_wait = ~she_wait;
break;
default:
break;
}
break;
case WM_KEYDOWN:
if (she_wait)
{
switch (wParam)
{
case VK_RIGHT:
she_head = she_right;
return 0;
case VK_LEFT:
she_head = she_left;
return 0;
case VK_UP:
she_head = she_up;
return 0;
case VK_DOWN:
she_head = she_down;
return 0;
default:
return 0;
}
}
break;
return 0;
restting(hWnd);
return 0;
return 0;
return 0;
{
hdc = BeginPaint(hWnd, &ps); //获取设备环境句柄
//TODO: 在此处添加使用 hdc 的任何绘图代码...
working(hWnd, hdc, rect); //绘制图形的具体代码
EndPaint(hWnd, &ps); //释放设备环境句柄
}
break;
case WM_DESTROY:
KillTimer(hWnd, Time_1); //结束时钟
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
win.h 代码如下
//初始化蛇体
void working_sheti_Initialize(HWND); //初始化蛇体
int working_she_move(HWND); //蛇体的移动 绘图---- 蛇体的绘制
void she_shiwu_eat(POINT); //蛇吃到食物的处理
int shiwu_rand(HWND); //食物的绘制与处理
void Free_she(void); //释放所有建立的空间
void restting(HWND); //重置所有,各类置初始
int she_fail(void); //蛇碰壁,游戏结束
//
#define Time_1 1 //定时器1
#define Rate 300 //初始速度1000毫秒
#define Color_hbrush_1_SH RGB(255,0,0) //画刷颜色1
#define Color_hbrush_2_QCSH RGB(255,255,255) //白色画刷
#define Color_hbrush_3_SW RGB(0,0,255)
#define she_left 1
#define she_right 2
#define she_up 3
#define she_down 4
//
static int she_wide,she_long; //蛇单体宽和高
static POINT Shiwu_point; //食物坐标
HBRUSH hbrush1_SH,hbrush2_QSSH,hbrush3_SW; //画刷句柄
int she_shiwu_ZT = 1; //1表示没有食物
unsigned int she_wait = 0; //蛇的运动状态 0表示暂停
unsigned int she_head = 2; //定义蛇移动的原始方向
unsigned int number_result=0; //得到的分数
//
//目的:绘制处理
//
{
TCHAR Tchar_working[100];
//赋值给Tchar_working
she_long = rect.bottom / 60;
MoveToEx(hdc, rect.right / 14 * 10, rect.top, NULL);
LineTo(hdc, rect.right / 14 * 10, rect.bottom); //右边积分区
TextOut(hdc, she_wide * 70, she_long * 10, Tchar_working,
wsprintf(Tchar_working, TEXT("得到的分数:")));
Rectangle(hdc, 0, 0, she_wide*60, she_long*60); //蛇正常活动区域
//定义链表来存蛇体
//
typedef struct SHE {
struct SHE *link;
POINT struct_point;
}she;
static she *head;
#define SHE_MALLOC (she *) malloc(sizeof(she)) //给链表分配空间
static int she_number = 1; //蛇的长度
//初始化蛇体
//初始化蛇体
void working_sheti_Initialize(HWND hWnd)
{
RECT rect;
she *p1;
HDC hdc;
head = SHE_MALLOC;
head->struct_point.x = rect.right/84*30;
head->struct_point.y = rect.bottom/60*30;
p1 = SHE_MALLOC;
head->link = p1;
p1->struct_point.x = head->struct_point.x-she_wide;
p1->struct_point.y = head->struct_point.y;
p1->link = NULL;
SelectObject(hdc, GetStockObject(NULL_PEN));
SelectObject(hdc, hbrush1_SH);
head->struct_point.x + she_wide, head->struct_point.y + she_long);
Rectangle(hdc, p1->struct_point.x, p1->struct_point.y,
p1->struct_point.x + she_wide, p1->struct_point.y + she_long);
ReleaseDC(hWnd, hdc);
}
//绘图---- 蛇体的绘制
//
//蛇体的移动
//
//
int working_she_move(HWND hWnd)
{
HDC hdc;
static POINT point;
static she *p1,*p2;
static unsigned move_PD ;
move_PD = 1;
while (move_PD)
{
move_PD = 0;
switch (she_head)
{
case she_left:
point.x = -she_wide;
if (head->struct_point.x + point.x == head->link->struct_point.x)
{
she_head = she_right;
move_PD = 1;
}
break;
case she_right:
point.x = she_wide;
if (head->struct_point.x + point.x == head->link->struct_point.x)
{
she_head = she_left;
move_PD = 1;
}
break;
case she_up:
point.y = -she_long;
if (head->struct_point.y + point.y == head->link->struct_point.y)
{
she_head = she_down;
move_PD = 1;
}
break;
case she_down:
point.y = she_long;
if (head->struct_point.y + point.y == head->link->struct_point.y)
{
she_head = she_up;
move_PD = 1;
}
break;
default:
break;
}
}
{
p1 = SHE_MALLOC;
p1->struct_point.x = head->struct_point.x+point.x;
p1->struct_point.y = head->struct_point.y + point.y;
p1->link = head;
head = p1;
{
restting(hWnd);
return 0;
}
SelectObject(hdc, GetStockObject(NULL_PEN));
SelectObject(hdc, hbrush1_SH);
Rectangle(hdc, head->struct_point.x, head->struct_point.y,
head->struct_point.x + she_wide, head->struct_point.y + she_long);
{
p2 = p1;
p1 = p1->link;
}
SelectObject(hdc, hbrush2_QSSH); //获取环境句柄
Rectangle(hdc, p1->struct_point.x, p1->struct_point.y,
p1->struct_point.x + she_wide, p1->struct_point.y + she_long);
p2->link = NULL;
free(p1);
if (head->struct_point.x == Shiwu_point.x*she_wide &&
head->struct_point.y == Shiwu_point.y*she_long ) //如果食物和蛇头重叠
{
she_shiwu_eat(point); //进入食物与蛇处理
TCHAR tchar[20];
TextOut(hdc, she_wide * 80, she_long * 10, tchar,
wsprintf(tchar, TEXT("%d"), number_result));
}
ReleaseDC(hWnd, hdc); //释放环境句柄
}
return 0;
}
//食物的绘制与产生
//
//
{
HDC hdc;
srand((unsigned)time(NULL)); // 使用系统时间作为随机种子
Shiwu_point.x = rand()%60; //产生一个0-61的随机数
Shiwu_point.y = rand() % 60;
SelectObject(hdc, GetStockObject(NULL_PEN));
SelectObject(hdc, hbrush3_SW);
Rectangle(hdc, Shiwu_point.x*she_wide, Shiwu_point.y*she_long,
(Shiwu_point.x +1) *she_wide, (Shiwu_point.y +1)* she_long);
ReleaseDC(hWnd, hdc); //释放环境句柄
}
//蛇吃到食物处理
//
void she_shiwu_eat(POINT point)
{
she *p1,*p2;
p1 = SHE_MALLOC;
p1->struct_point.x = point.x;
p1->struct_point.y = point.y;
p2 = head->link;
head->link = p1;;
p1->link = p2;
she_shiwu_ZT = 1; //置1食物,表示食物已经没有了
//Sleep(10000);
}
//
//
void Free_she(void)
{
she *p1;
while (head->link)
{
p1 = head;
head = p1->link;
free(p1);
}
free(head);
}
//重新开始 各类置初始
//重新开始 各类置初始
void restting(HWND hWnd)
{
InvalidateRect(hWnd, NULL, TRUE); //鼠标按起,清除绘图
Free_she(); //释放所有空间
working_sheti_Initialize(hWnd); //重新初始化蛇体
she_head = 1; //蛇的方向置右
she_shiwu_ZT = 1; //蛇食物状态置没有
she_wait = 0; //蛇移动状态置暂停
number_result = 0; //分数置零
}
//蛇碰壁,游戏结束
//
int she_fail(void)
{
she *p1;
if (head->struct_point.x <0 || head->struct_point.x>59 * she_wide ||
head->struct_point.y <0 || head->struct_point.y>59 * she_wide)
{
return 1;
}
p1 = head;
while (p1->link)
{
p1 = p1->link;
if (head->struct_point.x == p1->struct_point.x &&
head->struct_point.y == p1->struct_point.y)
{
return 1;
}
}
return 0;
}