VS2017 简单推箱子游戏
- 先为迷宫布局
int migo[11][11] = {
{
1,1,1,1,1,1,1,1,1,1,1},
{
1,0,0,0,0,0,0,0,0,0,1},
{
1,0,1,0,1,0,1,0,1,0,1},
{
1,0,0,0,0,0,0,0,0,0,1},
{
1,0,1,0,1,0,1,0,1,0,1},
{
1,0,0,0,0,0,0,0,0,0,1},
{
1,0,1,0,1,0,1,0,1,0,1},
{
1,0,0,0,0,0,0,0,0,0,1},
{
1,0,1,0,1,0,1,0,1,0,1},
{
1,0,0,0,0,0,0,0,0,0,1},
{
1,1,1,1,1,1,1,1,1,1,1}
};
- 在WM_PAINT里构建迷宫
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
HBRUSH hBrush;
hBrush = CreateSolidBrush(RGB(180, 220, 230));
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (migo[i][j] == 1) {
RECT rect;
rect.bottom = (i + 1) * 50;
rect.left = j * 50;
rect.right = (j + 1) * 50;
rect.top = i * 50;
FillRect(hdc, &rect, hBrush);
}
}
}
- 为键盘上下左右方向设置事件
case WM_KEYDOWN:
{
switch (wParam) {
case VK_LEFT:
{
if (persony > 0) {
if (boxx == personx && boxy == persony - 1) {
if (migo[boxx][boxy - 1] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
boxy--;
persony--;
steps++;
}
}
else if (migo[personx][persony - 1] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
persony--;
steps++;
}
}
}
break;
case VK_RIGHT:
{
if (persony < 10) {
if (boxx == personx && boxy == persony + 1 ) {
if (migo[boxx][boxy + 1] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
boxy++;
persony++;
steps++;
}
}else if (migo[personx][persony + 1] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
persony++;
steps++;
}
}
}
break;
case VK_UP:
{
if (persony > 0) {
if (boxx == personx - 1 && boxy == persony) {
if (migo[boxx - 1][boxy] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
boxx--;
personx--;
steps++;
}
}
else if (migo[personx - 1][persony] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
personx--;
steps++;
}
}
}
break;
case VK_DOWN:
{
if (personx < 10) {
if (boxx == personx + 1 && boxy == persony) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
if (migo[boxx + 1][boxy] == 0) {
boxx++;
personx++;
steps++;
}
}
else if (migo[personx + 1][persony] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
personx++;
steps++;
}
}
}
break;
}
}
- 设置箱子,任务,终点坐标初始值
//人
int personx = 1;
int persony = 1;
//箱子
int boxx = 3;
int boxy = 3;
//终点
int endx = 9;
int endy = 9;
//步数
int steps = 0;
- 在资源视图里进行布局设置
cpp完整代码:
#include "framework.h"
#include "week9.h"
#include "atlstr.h"
#define MAX_LOADSTRING 100
// 全局变量:
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);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK Person(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK Box(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK End(HWND, UINT, WPARAM, LPARAM);
void init(HWND hWnd);
//人
int personx = 1;
int persony = 1;
//箱子
int boxx = 3;
int boxy = 3;
//终点
int endx = 9;
int endy = 9;
//步数
int steps = 0;
int migo[11][11] = {
{
1,1,1,1,1,1,1,1,1,1,1},
{
1,0,0,0,0,0,0,0,0,0,1},
{
1,0,1,0,1,0,1,0,1,0,1},
{
1,0,0,0,0,0,0,0,0,0,1},
{
1,0,1,0,1,0,1,0,1,0,1},
{
1,0,0,0,0,0,0,0,0,0,1},
{
1,0,1,0,1,0,1,0,1,0,1},
{
1,0,0,0,0,0,0,0,0,0,1},
{
1,0,1,0,1,0,1,0,1,0,1},
{
1,0,0,0,0,0,0,0,0,0,1},
{
1,1,1,1,1,1,1,1,1,1,1}
};
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此处放置代码。
// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WEEK9, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WEEK9));
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;
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_WEEK9));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WEEK9);
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,
500, 200, 565, 605, 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)
{
switch (message)
{
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;
case PersonEdit:
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, Person);
InvalidateRect(hWnd, NULL, TRUE); //重绘
break;
case BoxEdit:
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG2), hWnd, Box);
InvalidateRect(hWnd, NULL, TRUE); //重绘
break;
case EndEdit:
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG3), hWnd, End);
InvalidateRect(hWnd, NULL, TRUE); //重绘
break;
case again :
init(hWnd);
break;
case victory:
boxx = endx;
boxy = endy;
InvalidateRect(hWnd, NULL, TRUE); //重绘
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: 在此处添加使用 hdc 的任何绘图代码...
HBRUSH hBrush;
hBrush = CreateSolidBrush(RGB(180, 220, 230));
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (migo[i][j] == 1) {
RECT rect;
rect.bottom = (i + 1) * 50;
rect.left = j * 50;
rect.right = (j + 1) * 50;
rect.top = i * 50;
FillRect(hdc, &rect, hBrush);
}
}
}
RECT rect;
//画圆
HRGN hrgn;
//终点
hBrush = CreateSolidBrush(RGB(150, 150, 150));
rect.bottom = (endx + 1) * 50;
rect.left = endy * 50;
rect.right = (endy + 1) * 50;
rect.top = endx * 50;
FillRect(hdc, &rect, hBrush);
//箱子
hBrush = CreateSolidBrush(RGB(50, 100, 120));
hrgn = CreateEllipticRgn(boxy * 50, boxx * 50, (boxy + 1) * 50, (boxx + 1) * 50);
FillRgn(hdc, hrgn, hBrush);
//人
hBrush = CreateSolidBrush(RGB(100, 0, 100));
hrgn = CreateEllipticRgn(persony * 50, personx * 50, (persony + 1) * 50, (personx + 1) * 50);
FillRgn(hdc, hrgn, hBrush);
if (boxx == endx && boxy == endy) {
CString str;
str.Format(_T("You Win with %d steps!\n---------------------\n"), steps);
MessageBox(hWnd, str, TEXT("message"), 0);
init(hWnd);
}
EndPaint(hWnd, &ps);
}
break;
case WM_KEYDOWN:
{
switch (wParam) {
case VK_LEFT:
{
if (persony > 0) {
if (boxx == personx && boxy == persony - 1) {
if (migo[boxx][boxy - 1] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
boxy--;
persony--;
steps++;
}
}
else if (migo[personx][persony - 1] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
persony--;
steps++;
}
}
}
break;
case VK_RIGHT:
{
if (persony < 10) {
if (boxx == personx && boxy == persony + 1 ) {
if (migo[boxx][boxy + 1] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
boxy++;
persony++;
steps++;
}
}else if (migo[personx][persony + 1] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
persony++;
steps++;
}
}
}
break;
case VK_UP:
{
if (persony > 0) {
if (boxx == personx - 1 && boxy == persony) {
if (migo[boxx - 1][boxy] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
boxx--;
personx--;
steps++;
}
}
else if (migo[personx - 1][persony] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
personx--;
steps++;
}
}
}
break;
case VK_DOWN:
{
if (personx < 10) {
if (boxx == personx + 1 && boxy == persony) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
if (migo[boxx + 1][boxy] == 0) {
boxx++;
personx++;
steps++;
}
}
else if (migo[personx + 1][persony] == 0) {
InvalidateRect(hWnd, NULL, TRUE); //重绘
personx++;
steps++;
}
}
}
break;
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//初始化
void init(HWND hWnd) {
//初始化人
personx = 1;
persony = 1;
//初始化箱子
boxx = 3;
boxy = 3;
//初始化终点
endx = 9;
endy = 9;
//初始化步数
steps = 0;
InvalidateRect(hWnd, NULL, TRUE); //重绘
}
// “关于”框的消息处理程序。
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)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
INT_PTR CALLBACK Person(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) == PersonOK)
{
int x = GetDlgItemInt(hDlg, PersonX, NULL, NULL);
int y = GetDlgItemInt(hDlg, PersonY, NULL, NULL);
if (x>=0 && x<=10 && y>=0 && y<=10 && migo[x][y] != 1) {
personx = x;
persony = y;
}
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
else if (LOWORD(wParam) == PersonCancel) {
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
INT_PTR CALLBACK Box(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) == BoxOk)
{
int x = GetDlgItemInt(hDlg, BoxX, NULL, NULL);
int y = GetDlgItemInt(hDlg, BoxY, NULL, NULL);
if (x >= 0 && x <= 10 && y >= 0 && y <= 10 && migo[x][y] != 1) {
boxx = x;
boxy = y;
}
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
else if(LOWORD(wParam) == BoxCancle){
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
INT_PTR CALLBACK End(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) == EndOk)
{
int x = GetDlgItemInt(hDlg, EndX, NULL, NULL);
int y = GetDlgItemInt(hDlg, EndY, NULL, NULL);
if (x >= 0 && x <= 10 && y >= 0 && y <= 10 && migo[x][y] != 1) {
endx = x;
endy = y;
}
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
else if (LOWORD(wParam) == EndCancle) {
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
效果图: