任务点:
1、键盘左右键消息处理;
2、碰撞检测(与砖块、挡板、上、左、右);
3、控制转向;
解决思路:
1、左右键消息处理:
响应 WM_KEYDOWN 消息,移动挡板(如果能够的话,重新绘制窗口即可)
- case WM_KEYDOWN:
- {
- switch(wParam)
- {
- case VK_LEFT:
- if(bafflex>=5)
- {
- bafflex-=5;
- hdc=::GetDC(hWnd);
- Draw(hdc);
- ::ReleaseDC(hWnd,hdc);
- }
-
- break;
- case VK_RIGHT:
- if(bafflex+110<=445)
- {
- bafflex+=5;
- hdc=::GetDC(hWnd);
- Draw(hdc);
- ::ReleaseDC(hWnd,hdc);
- }
- break;
- default: //其他消息发给windows默认窗口处理函数
- return (DefWindowProc(hWnd,message,wParam,lParam));
- }
- }
2、检测球与砖块的碰撞,通过砖块数组下标,计算砖块位置 :
左上(j * 45,i * 20) 右下 ((j +1)* 45,( i + 1)* 20)
判断条件:
bally+5 >= i * 20 && bally+5 <= (i+1) *20 && ballx +5>= j * 45 && ballx+5 <= (j+1) *45;
另外,也可以反过来用 bally / 20 ballx / 45 计算出当前的(i ,j )要注意数组越界!
砖块碰撞后的处理:
消除砖块( 数组中 (i,j) 元素置 0 ) 球速水平、垂直 逆转
brick_array[i][j] = 0; cy = -cy; cx=-cx;
3、检测球与窗口边缘(左、右、上)以及挡板(是否接住)的碰撞
- bool isBlock()
- {
- if(bally <= 0) cy=-cy;
- if(ballx <= 0 || ballx >= 435) cx=-cx;
- if(bally + 5 >= baffley)
- {
- if(ballx < bafflex || ballx > bafflex +111)
- {
- return false;
- }
- cy =-cy;
- }
- return true;
- }
上、左、 右 分别逆转球的运动方向,最后判断是否接住: 先看球已经掉落到挡板位置,然后判断是否在挡板范围内
4、球没接住的后续处理:
::KillTimer(hWnd,1); // 停了时钟,然后后续的处理
5、简单粗暴完成版本代码如下:
主函数:SDK_3.cpp
- // SDK_3.cpp : 定义应用程序的入口点。
- //
-
- #include "stdafx.h"
- #include "SDK_3.h"
- #include "SDK3_API.h"
- #define MAX_LOADSTRING 100
-
- // 全局变量:
- HINSTANCE hInst; // 当前实例
- TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
- TCHAR 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);
-
- extern int bafflex; //外部引用全局变量
- int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPTSTR lpCmdLine,
- _In_ int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
-
- // TODO: 在此放置代码。
- MSG msg;
- HACCEL hAccelTable;
-
- // 初始化全局字符串
- LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_SDK_3, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
-
- // 执行应用程序初始化:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
-
- hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SDK_3));
-
- // 主消息循环:
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
-
- return (int) msg.wParam;
- }
- //
- // 函数: MyRegisterClass()
- //
- // 目的: 注册窗口类。
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX 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_SDK_3));
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SDK_3);
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
-
- return RegisterClassEx(&wcex);
- }
-
- //
- // 函数: InitInstance(HINSTANCE, int)
- //
- // 目的: 保存实例句柄并创建主窗口
- //
- // 注释:
- //
- // 在此函数中,我们在全局变量中保存实例句柄并
- // 创建和显示主程序窗口。
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
-
- hInst = hInstance; // 将实例句柄存储在全局变量中
-
- hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
-
- 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)
- {
- int wmId, wmEvent;
- PAINTSTRUCT ps;
- HDC hdc;
-
- switch (message)
- {
- case WM_CREATE:
- Init(hWnd);
- break;
- case WM_KEYDOWN:
- {
- switch(wParam)
- {
- case VK_LEFT:
- if(bafflex>=5)
- {
- bafflex-=5;
- hdc=::GetDC(hWnd);
- Draw(hdc);
- ::ReleaseDC(hWnd,hdc);
- }
-
- break;
- case VK_RIGHT:
- if(bafflex+110<=445)
- {
- bafflex+=5;
- hdc=::GetDC(hWnd);
- Draw(hdc);
- ::ReleaseDC(hWnd,hdc);
- }
- break;
- default: //其他消息发给windows默认窗口处理函数
- return (DefWindowProc(hWnd,message,wParam,lParam));
- }
- }
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- // 分析菜单选择:
- switch (wmId)
- {
- case IDC_START:
- ::SetTimer(hWnd,1,10,TimerProc);
- break;
- 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_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- // TODO: 在此添加任意绘图代码...
- Draw(hdc);
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY:
- 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;
- }
自定义的API头文件:SDK3_API.h
- #pragma once
- void Init(HWND hWnd);
- void Draw(HDC hdc );
- void WINAPI TimerProc(HWND, UINT, UINT, DWORD);
- bool isHit(int i,int j);
- bool isBlock();
- void Check();
- void MoveBaffle(int x);
SDK3_API.cpp
- #include "stdafx.h"
- #include "SDK_3.h"
- #include "resource.h"
- #include "SDK3_API.h"
- extern HINSTANCE hInst;
- BYTE brick_array[4][10]={
- {0,1,1,1,1,1,1,1,1,0},
- {0,0,1,1,1,1,1,1,0,0},
- {0,0,0,1,1,1,1,0,0,0},
- {0,0,0,0,1,1,0,0,0,0}
- };
- HBITMAP ball,brick,baffle,tempBitmap;
- HDC hMemDC,hTempDC;
- int ballx=225,bally=420,bafflex=170,baffley=430,cx=2,cy=-2;
- void Init(HWND hWnd)
- {
- baffle = ::LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP1));
- ball = ::LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP2));
- brick = ::LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP3));
-
- ::SetWindowPos(hWnd,
- NULL,
- (::GetSystemMetrics(SM_CXFULLSCREEN) - 450)/2,
- (::GetSystemMetrics(SM_CYFULLSCREEN) - 450)/2,
- 450,
- 500,
- SWP_NOZORDER);
- HDC hdc = ::GetDC(hWnd);
- hMemDC = ::CreateCompatibleDC(hdc);
- hTempDC=::CreateCompatibleDC(hdc);
- tempBitmap = ::CreateCompatibleBitmap(hdc,450,500);
- ::SelectObject(hTempDC,tempBitmap);
-
- ::ReleaseDC(hWnd,hdc);
- }
- void Draw(HDC hdc )
- {
- int i,j;
- ::PatBlt(hTempDC,0,0,450,500,WHITENESS);
- ::SelectObject(hMemDC,brick);
- for(i = 0; i < 4; i++)
- {
- for(j = 0; j < 10; j++)
- {
- if(brick_array[i][j] == 1)
- {
- ::BitBlt(hTempDC,j * 45,i * 20,45,20,hMemDC,0,0,SRCCOPY);
- }
- }
- }
- ::SelectObject(hMemDC,ball);
- ::BitBlt(hTempDC,ballx,bally,10,10,hMemDC,0,0,SRCCOPY);
- ::SelectObject(hMemDC,baffle);
- ::BitBlt(hTempDC,bafflex,baffley,111,10,hMemDC,0,0,SRCCOPY);
- ::BitBlt(hdc,0,0,450,500,hTempDC,0,0,SRCCOPY);
- }
- void WINAPI TimerProc(HWND hWnd, UINT nMsg,UINT nTimerid,DWORD dwTime)
- {
- if(nTimerid ==1)
- {
- ballx+=cx;
- bally+=cy;
- Check();
- if(!isBlock())
- {
- ::KillTimer(hWnd,1);
- return;
- }
- HDC hdc=::GetDC(hWnd);
- Draw(hdc);
- ::ReleaseDC(hWnd,hdc);
- }
- }
- bool isHit(int i,int j)
- {
- return bally+5 >= i * 20 && bally+5 <= (i+1) *20 && ballx +5>= j * 45 && ballx+5 <= (j+1) *45;
- }
- bool isBlock()
- {
- if(bally <= 0) cy=-cy;
- if(ballx <= 0 || ballx >= 435) cx=-cx;
- if(bally + 5 >= baffley)
- {
- if(ballx < bafflex || ballx > bafflex +111)
- {
- return false;
- }
- cy =-cy;
- }
- return true;
- }
- void Check()
- {
- int i,j;
- for(i = 0; i < 4; i++)
- {
- for(j = 0; j < 10; j++)
- {
- if(brick_array[i][j] == 1)
- {
- if(isHit(i,j))
- {
- brick_array[i][j] = 0;
- cy = -cy;
- cx=-cx;
- }
- }
- }
- }
- }
挡板图片 (111 * 10 )
球图片(10*10)
砖块图片(45 *20)
以上素材可以利用画图工具来完成,如修改尺寸,则需要调整代码中很多数值(窗口大小等等)。
后记:
1、本程序为初学Windows C SDK 窗口程序设计 做的一个比较 “粗糙” 的 小游戏,初衷是快速了解窗口程序设计的流程,以及消除对窗口编程的 “恐惧”,所以,很多 细节 都以舍弃,也写了很多很不规划的代码;
2、从以上的例子可以看出,窗口编程急需解决的问题是 “在哪里写代码” (通过消息处理来确定),“怎么写代码”(熟悉窗口元素的处理,利用以前学的编程逻辑,解决实际问题);
3、打砖块游戏的后续处理:如何重新开始游戏、如何设置游戏难度、如何更精细化处理游戏碰撞等等,都可以深入去处理一下。