1.介绍
消除所有的方块即可过关。操作指南:游戏中使用键盘方向键←→控制移动。首先构建双缓冲框架小球和砖块、客户区边界发生碰撞后改变运动方向。设置游戏关卡并绘制砖块分布。鼠标移动控制托盘。托盘没有接住小球生命值则减一。游戏结束时显示对话框以便继续或退出游戏。分为小球类,挡板类和砖块类。
2.程序
CBack.h
#pragma once
#include"allSys.h"
class CBack
{
public:
HBITMAP m_hBmpBack;
HBITMAP m_Start;
public:
CBack(void);
~CBack(void);
public:
void BackShow(HDC dc);
void IntiBack(HINSTANCE hIns);
void IntiStartBack(HINSTANCE hIns);
void BackStartShow(HDC dc);
};
CBack.cpp
#include "Back.h"
CBack::CBack(void)
{
m_hBmpBack=NULL;
}
CBack::~CBack(void)
{
}
void CBack::BackStartShow(HDC dc)
{
HDC hdc = ::CreateCompatibleDC(dc);
SelectObject(hdc,m_Start);
BitBlt(dc,0,0,620,1000,hdc,0,0,SRCCOPY);
DeleteDC(hdc);
}
void CBack::IntiBack(HINSTANCE hIns)
{
m_hBmpBack = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_BITMAP5));
#pragma once
#include"allSys.h"
//#include"Board.h"
class CBall
{
public:
int x;
int y;
int size; //尺寸
int vx; //速度
int vy;
//CBoard board;
HBITMAP m_hBmpBall;
public:
CBall(void);
~CBall(void);
public:
void InitBall(HINSTANCE hIns);//初始化小球
void MoveBall(); //小球的基本移动
void ShowBall(HDC hMemDC);//显示小球
//BOOL IsHitWall();
//BOOL IsHitBoard(CBoard &board);
};
}void CBack::IntiStartBack(HINSTANCE hIns){m_Start=::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_BITMAP5));}void CBack::BackShow(HDC dc){HDC hdc = ::CreateCompatibleDC(dc);SelectObject(hdc,m_hBmpBack);BitBlt(dc,0,0,620,1000,hdc,0,0,SRCCOPY);DeleteDC(hdc);}
CBall.h
#pragma once
#include"allSys.h"
//#include"Board.h"
class CBall
{
public:
int x;
int y;
int size; //尺寸
int vx; //速度
int vy;
//CBoard board;
HBITMAP m_hBmpBall;
public:
CBall(void);
~CBall(void);
public:
void InitBall(HINSTANCE hIns);//初始化小球
void MoveBall(); //小球的基本移动
void ShowBall(HDC hMemDC);//显示小球
//BOOL IsHitWall();
//BOOL IsHitBoard(CBoard &board);
};
CBall.cpp
#include "Ball.h"
CBall::CBall(void)
{
unsigned srand(time(0));
x = 310;
y = 684;
size = 15; //尺寸
vx = -4+rand()%9; //速度
vy = 3+rand()%2;
m_hBmpBall = NULL;
}
CBall::~CBall(void)
{
/*delete m_hBmpBall;
m_hBmpBall = NULL;*/
}
void CBall::InitBall(HINSTANCE hIns)
{
m_hBmpBall = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_BITMAP1));
}
void CBall::MoveBall()
{
/*if(this->IsHitWall()==true)
{
if(x <=0)
{
vx=(vx)*(-1);
}
if(x >=529)
{
vx=(vx)*(-1);
}
if(y <=0)
{
vy=(vy)*(-1);
}
if(y>=640)
{
vy=(vy)*(-1);
}
}
else if(this->IsHitBoard(board)==true)
{
vy=(vy)*(-1);
vx=(vx)*(1);
}*/
x+=vx;
y+=vy;
}
void CBall::ShowBall(HDC hMemDC)
{
HDC hdc = ::CreateCompatibleDC(hMemDC);
SelectObject(hdc,m_hBmpBall);
BitBlt(hMemDC,x,y,size,size,hdc,0,0,SRCAND);
DeleteDC(hdc);
}
//BOOL CBall::IsHitWall()
//{
// if(x<=0||x>=640)
// {
// return true;
// }
// if(y<=0)
// {
// return true;
// }
// return false;
//}
小球是否撞击到木板
//BOOL CBall::IsHitBoard(CBoard &board)
//{
// //判断球的坐标是否在木板的范围内
// if(y+4==board.y)
// {
// if(x>=board.x&&x<=board.x+35)
// {
// return true;
// }
// }
//return false;
//}
CBoard.h
#pragma once
#include"allSys.h"
#include"Ball.h"
class CBoard
{
public:
int x;
int y;
HBITMAP m_hBmpBoard;
bool begin;
public:
CBoard(void);
~CBoard(void);
public:
void InitBoard(HINSTANCE hIns);
void MoveBoard(int FX);
void ShowBoard(HDC hMemDC);
bool IsBallHitBoard(CBall& ball);
};
CBoard.cpp
#include "Board.h"
CBoard::CBoard(void)
{
x = 0;
y = 0;
m_hBmpBoard = NULL;
begin = false;
}
CBoard::~CBoard(void)
{
/*delete(m_hBmpBoard);
m_hBmpBoard = NULL;*/
}
void CBoard::InitBoard(HINSTANCE hIns)
{
m_hBmpBoard = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_BITMAP6));
x = 265;
y = 700;
}
void CBoard::MoveBoard(int FX)
{
if(FX== VK_UP)
{
begin = true;
}
if(FX == VK_LEFT)
{
if(x>0)
{
x-=2;
}
}
if(FX == VK_RIGHT)
{
if(x+90<620)
{
x+=2;
}
}
}
void CBoard::ShowBoard(HDC hMemDC)
{
HDC hdc = ::CreateCompatibleDC(hMemDC);
SelectObject(hdc,m_hBmpBoard);
BitBlt(hMemDC,x,y,90,14,hdc,0,0,SRCCOPY);
DeleteDC(hdc);
}
bool CBoard::IsBallHitBoard(CBall& ball)
{
左上
//if(ball.x>=x&&ball.x<=x+90&&ball.y>=y&&ball.y<=y+14)
//{
// return true;
//}
右上
//if(ball.x+ball.size>=x&&ball.x+ball.size<=x+90&&ball.y>=y&&ball.y<=y+14)
//{
// return true;
//}
//左下
if(ball.x>=x&&ball.x<=x+90&&ball.y+ball.size==y/*&&ball.y+ball.size<=y+14*/)
{
return true;
}
//右下
if(ball.x+ball.size>=x&&ball.x+ball.size<=x+90&&ball.y+ball.size>=y&&ball.y+ball.size<=y+14)
{
return true;
}
return false;
}
CBrick.h
#pragma once
#include"allSys.h"
#include"ball.h"
class CBrick
{
public:
int x;
int y;
HBITMAP m_hBmpBrick;
int m_color;
int size_x;
int size_y;
public:
CBrick();
~CBrick(void);
public:
bool IsBallHitBrick(CBall& ball); //判断小球是否撞击砖块
void ShowBrick(HDC hMemDC); //显示砖块
void InitBrick(HINSTANCE hIns,int n_bmp,int nColor);//初始化砖块
};
CBrick.cpp
#include "Brick.h"
CBrick::CBrick()
{
x = 0;
y = 0;
m_hBmpBrick = NULL;
m_color =0;
size_x = 58;
size_y =20;
}
CBrick::~CBrick(void)
{
delete(m_hBmpBrick);
m_hBmpBrick = NULL;
}
//判断小球是否撞击砖块
bool CBrick::IsBallHitBrick(CBall& ball)
{
左上
//if(ball.x>=x&&ball.x<=x+size_x&&ball.y>=y&&ball.y<=y+size_y)
//{
// return true;
//}
右上
//if(ball.x+ball.size>=x&&ball.x+ball.size<=x+size_x&&ball.y>=y&&ball.y<=y+size_y)
//{
// return true;
//}
左下
//if(ball.x>=x&&ball.x<=x+size_x&&ball.y+ball.size>=y&&ball.y+ball.size<=y+size_y)
//{
// return true;
//}
右下
//if(ball.x+ball.size>=x&&ball.x+ball.size<=x+size_x&&ball.y+ball.size>=y&&ball.y+ball.size<=y+size_y)
//{
// return true;
//}
//球撞击砖块的四个表面
//上表面//此时是球的下表面
if(ball.x>=x&&ball.x<=x+size_x&&ball.y+ball.size>=y&&ball.y+ball.size<=y+3)//5定义为此时球的y速度
{
return true;
}
//右表面(ball.x,ball.y)
if(ball.x>=x-3&&ball.x<=x&&ball.y>=y&&ball.y<=y+size_y)
{
return true;
}
//下表面(ball.x,ball.y)
if(ball.x>=x&&ball.x<=x+size_x&&ball.y>=y-3&&ball.y<=y)
{
return true;
}
//左表面(ball.x+ball.size,ball.y)
if(ball.x+ball.size>=x&&ball.x+ball.size<=x+3&&ball.y>=y&&ball.y<=y+size_y)
{
return true;
}
return false;
}
//显示砖块
void CBrick::ShowBrick(HDC hMemDC)
{
HDC hdc = ::CreateCompatibleDC(hMemDC);
SelectObject(hdc,m_hBmpBrick);
BitBlt(hMemDC,x,y,size_x,size_y,hdc,0,0,SRCCOPY);
DeleteDC(hdc);
}
//初始化砖块
void CBrick::InitBrick(HINSTANCE hIns,int n_bmp,int nColor)
{
m_hBmpBrick = ::LoadBitmap(hIns,MAKEINTRESOURCE(n_bmp));
m_color=nColor;
}
CBricksCtrl.h
#pragma once
#include"CGameCtrl.h"
#include"allSys.h"
#include"ball.h"
#include"Board.h"
#include"Brick.h"
#include"Back.h"
class CBricksCtrl:public CGameCtrl
{
public:
bool m_bBrickMoveFlag;
bool flag;//判断是否删除了砖块
CBack back;
CBoard board;
CBall ball;
list<CBrick*> m_lstBrick;
BOOL FX_flag;
public:
CBricksCtrl(void);
~CBricksCtrl(void);
public:
DECLARE()
virtual void OnCreateGame(WPARAM wParam); // WM_CREATE
virtual void OnGameDraw(); // WM_PAINT
virtual void OnGameRun(WPARAM nTimeID); // WM_TIMER
virtual void OnKeyDown(WPARAM nKey); //WM_KEYDOWN
virtual void OnKeyUp(WPARAM nKey); //WM_KEYUP
void GameOver();
void GameWin();
void AllBrickShow(HDC hMemDC);
void AllBollShow(HDC hMemDC);
void CreateBrick();
void BallMove();
void IsHitWall();
void IsHitBoard();
void IsHitBrick();
void DeleteBrick(list<CBrick*>::iterator ite);
};
CBricksCtrl.cpp
#include "BricksCtrl.h"
IMPLEMENT(CBricksCtrl)
CBricksCtrl::CBricksCtrl(void)
{
m_bBrickMoveFlag = false;
flag=false;
}
CBricksCtrl::~CBricksCtrl(void)
{
}
void CBricksCtrl::OnCreateGame(WPARAM wParam)
{
//调用初始化背景的函数
back.IntiStartBack (m_hIns);
//初始化木板
board.InitBoard(m_hIns);
ball.InitBall(m_hIns);
this->CreateBrick();
SetTimer(m_hMainWnd,BALL_MOVE_TIMER,10,NULL);
SetTimer(m_hMainWnd,WALL_MOVE_TIMER,10,NULL);
}// WM_CREATE
void CBricksCtrl::OnGameDraw()
{
HDC dc = GetDC(m_hMainWnd);
HDC hdcTemp = ::CreateCompatibleDC(dc); //创建一个兼容性dc来缓冲 避免闪图的问题
HBITMAP bitmap = ::CreateCompatibleBitmap(dc,620,1000);
//相当于dc的画板 兼容系统dc就有颜色 兼容 兼容性dc就死黑白色
SelectObject(hdcTemp,bitmap);
//--------------------------------------------------------------------------------
back.BackShow (hdcTemp);
back.BackStartShow (hdcTemp);
AllBollShow(hdcTemp);
AllBrickShow(hdcTemp);
board.ShowBoard(hdcTemp);
//-------------------------------------------------------------------------------
BitBlt(dc,0,0,620,1000,hdcTemp,0,0,SRCCOPY);
DeleteObject(bitmap);
DeleteDC(hdcTemp);
ReleaseDC(m_hMainWnd,dc);
}// WM_PAINT
void CBricksCtrl::OnGameRun(WPARAM nTimeID)
{
if(nTimeID==1)
{
if(board.begin )
{
this->BallMove ();
this->IsHitBrick();
this->IsHitBoard();
this->IsHitWall();
this->OnGameDraw();
this->GameOver ();
this->GameWin ();
}
}
}// WM_TIMER
void CBricksCtrl::OnKeyDown(WPARAM key)
{
m_bBrickMoveFlag = true;
while(m_bBrickMoveFlag)
{
MSG msg;
::GetMessage(&msg,NULL,0,0);
if(msg.message != WM_KEYDOWN)
{
::TranslateMessage(&msg);
::DispatchMessageA(&msg);
}
board .MoveBoard (key);
if(!board.begin)
{
ball.x = board.x+45;
ball.y = board.y-ball.size-1;
}
this->OnGameDraw();
}
}//WM_KEYDOWN
void CBricksCtrl::OnKeyUp(WPARAM key)
{
m_bBrickMoveFlag=false;
}//WM_KEYUP
void CBricksCtrl::GameOver()
{
//游戏结束:小球的y坐标
if(ball.y>board.y+20)
{
HBITMAP m_hBmp;
HDC dc = GetDC(m_hMainWnd);
HDC hdcTemp = ::CreateCompatibleDC(dc);
m_hBmp=::LoadBitmap (m_hIns,MAKEINTRESOURCE(IDB_BITMAP3));
SelectObject(hdcTemp,m_hBmp);
BitBlt(dc,50,300,600,400,hdcTemp,0,0,SRCAND);
DeleteDC(hdcTemp);
ReleaseDC(m_hMainWnd,dc);
//MessageBox (m_hMainWnd,"菜鸟,you 输了!","提示",MB_OK);
//this->OnGameDraw();
::KillTimer (m_hMainWnd,BALL_MOVE_TIMER);
::KillTimer (m_hMainWnd,WALL_MOVE_TIMER);
}
}
void CBricksCtrl::GameWin()
{
HBITMAP m_hBmp;
//判断砖块链表是否是空
if(m_lstBrick.empty ()==true)
{
::KillTimer (m_hMainWnd,BALL_MOVE_TIMER);
//链表为空贴烟花位图
HDC dc = GetDC(m_hMainWnd);
HDC hdcTemp = ::CreateCompatibleDC(dc);
m_hBmp=::LoadBitmap (m_hIns,MAKEINTRESOURCE(IDB_BITMAP2));
SelectObject(hdcTemp,m_hBmp);
BitBlt(dc,0,150,700,669,hdcTemp,0,0,SRCAND);
DeleteDC(hdcTemp);
ReleaseDC(m_hMainWnd,dc);
//this->OnGameDraw();
}
}
void CBricksCtrl::AllBrickShow(HDC hMemDC)
{
//所有砖块的显示
list<CBrick*>::iterator ite=m_lstBrick.begin();
while(ite!=m_lstBrick.end ())
{
(*ite)->ShowBrick(hMemDC);
ite++;
}
}
void CBricksCtrl::AllBollShow(HDC hMemDC)
{
ball.ShowBall(hMemDC);
}
void CBricksCtrl::CreateBrick()
{
/*for(int i=0;i<8;i++)
{
for(int j=0;j<4;j++)
{
CBrick* brick = new CBrick;
brick->x = 50+i*68;
brick->y = 50+j*30;
if(j<4)
{
brick->InitBrick(m_hIns,IDB_BITMAP11+j,j+1);
}
m_lstBrick.push_back(brick);
}
}*/
//T的横
for(int i=0;i<3;i++)
{
CBrick* brick = new CBrick;
brick->x = 30+i*63;
brick->y = 50;
brick->InitBrick(m_hIns,IDB_BITMAP11,1);
m_lstBrick.push_back(brick);
}
//T的竖
for(int i=0;i<8;i++)
{
CBrick* brick = new CBrick;
brick->y = 75+i*25;
brick->x =98;
brick->InitBrick(m_hIns,IDB_BITMAP11,1);
m_lstBrick.push_back(brick);
}
//L的竖
for(int i=0;i<8;i++)
{
CBrick* brick = new CBrick;
brick->x=224;
brick->y =230+i*25;
brick->InitBrick(m_hIns,IDB_BITMAP11+2,1+2);
m_lstBrick.push_back(brick);
}
//L的横
for(int i=0;i<2;i++)
{
CBrick* brick = new CBrick;
brick->x = 287+i*63;
brick->y = 405;
brick->InitBrick(m_hIns,IDB_BITMAP11+2,1+2);
m_lstBrick.push_back(brick);
}
//R的竖
for(int i=0;i<8;i++)
{
CBrick* brick = new CBrick;
brick->x=373;
brick->y =50+i*25;
brick->InitBrick(m_hIns,IDB_BITMAP11+5,1+5);
m_lstBrick.push_back(brick);
}
//R的上面横
for(int i=0;i<2;i++)
{
CBrick* brick = new CBrick;
brick->x=436+i*63;
brick->y =50;
brick->InitBrick(m_hIns,IDB_BITMAP11+5,1+5);
m_lstBrick.push_back(brick);
}
//R的右竖
for(int i=0;i<2;i++)
{
CBrick* brick = new CBrick;
brick->x=500;
brick->y =75+i*25;
brick->InitBrick(m_hIns,IDB_BITMAP11+5,1+5);
m_lstBrick.push_back(brick);
}
//R的下面横
for(int i=0;i<2;i++)
{
CBrick* brick = new CBrick;
brick->x=436+i*63;
brick->y =125;
brick->InitBrick(m_hIns,IDB_BITMAP11+5,1+5);
m_lstBrick.push_back(brick);
}
//R的尾巴
for(int i=0;i<2;i++)
{
CBrick* brick = new CBrick;
brick->x=436+i*63;
brick->y =175+i*50;
brick->InitBrick(m_hIns,IDB_BITMAP11+5,1+5);
m_lstBrick.push_back(brick);
}
}
void CBricksCtrl::BallMove()
{
ball.MoveBall ();
}
void CBricksCtrl::IsHitWall()
{
if(ball.x<=0||ball.x+ball.size>=620)
{
ball.vx*=-1;
}
if(ball.y<=0)
{
ball.vy*=-1;
}
}
void CBricksCtrl::IsHitBoard()
{
if(board.IsBallHitBoard(ball))
{
if(ball.x<=board.x+15||ball.x>board.x+75)
{
unsigned srand(time(0));
ball.vx = -4+rand()%9; //速度
ball.vy = 3+rand()%2;
}
ball.vy*=-1;
}
}
void CBricksCtrl::IsHitBrick()
{
if(m_lstBrick.empty() ==true)
{
return ;
}
list<CBrick*>::iterator ite= m_lstBrick.begin();
while(ite!= m_lstBrick.end())
{
flag = false;
if((*ite)->IsBallHitBrick(ball))
{
//flag = true;
砖块撞到四个角
//if(ball.x-(*ite)->x==ball.y-(*ite)->y)
//{
// ball.vx *= -1;
// ball.vy *= -1;
// DeleteBrick(ite);
// break;
//}
撞到右上表面
//if(ball.x-(*ite)->x>ball.y-(*ite)->y)
//{
// //上表面
// if((*ite)->x+(*ite)->size_x-ball.x>ball.y-(*ite)->y)
// {
// ball.vy *= -1;
// DeleteBrick(ite);
// break;
// }
// else
// {
// ball.vx *= -1;
// DeleteBrick(ite);
// break;
// }
//}
左下
//if(ball.x-(*ite)->x<ball.y-(*ite)->y)
//{
// //下面
// if((*ite)->x+(*ite)->size_x-ball.x<ball.y-(*ite)->y)
// {
// ball.vy *= -1;
// DeleteBrick(ite);
// break;
// }
// else
// {
// ball.vx *= -1;
// DeleteBrick(ite);
// break;
// }
flag = true;
//砖块撞到四个角
if(ball.x-(*ite)->x==ball.y-(*ite)->y)
{
ball.vx *= -1;
ball.vy *= -1;
DeleteBrick(ite);
break;
}
//撞到右上表面
if(ball.x-(*ite)->x>ball.y-(*ite)->y)
{
//上表面
if((*ite)->x+(*ite)->size_x-ball.x>ball.y-(*ite)->y)
{
ball.vy *= -1;
DeleteBrick(ite);
break;
}
else
{
ball.vx *= -1;
DeleteBrick(ite);
break;
}
}
//左下
if(ball.x-(*ite)->x<ball.y-(*ite)->y)
{
//下面
if((*ite)->x+(*ite)->size_x-ball.x<ball.y-(*ite)->y)
{
ball.vy *= -1;
DeleteBrick(ite);
break;
}
else
{
ball.vx *= -1;
DeleteBrick(ite);
break;
}
}
}
if(flag ==false)
ite++;
}
}
void CBricksCtrl::DeleteBrick(list<CBrick*>::iterator ite)
{
//delete (*ite);
ite = m_lstBrick.erase(ite);
}
CGameCtrl.h
#pragma once
#ifndef _CGAMECTRL_H_
#define _CGAMECTRL_H_
#include <windows.h>
#define DECLARE() static CGameCtrl* CreateObject();
#define IMPLEMENT(ThisClass) \
CGameCtrl* ThisClass::CreateObject()\
{\
return new ThisClass;\
}\
Init init##ThisClass(&ThisClass::CreateObject);
//-----------------------------------------
class CGameCtrl;
typedef CGameCtrl* (*PFUN_CREATE_OBJECT)(); // 创建子类的 对象的函数指针
//-----------------------------------------
class CGameCtrl
{
public:
static PFUN_CREATE_OBJECT pfnCreateObject; // 创建子类对象的函数指针
protected:
HINSTANCE m_hIns;
HWND m_hMainWnd; // 包含一个 主窗口的句柄
public:
CGameCtrl()
{
m_hIns = NULL;
m_hMainWnd = NULL;
}
virtual ~CGameCtrl()
{
}
public:
void SetHandle(HINSTANCE hIns,HWND hWnd)
{
m_hIns = hIns;
m_hMainWnd = hWnd;
}
public:
virtual void OnCreateGame(WPARAM wParam){} // WM_CREATE
virtual void OnGameDraw(){} // WM_PAINT
virtual void OnGameRun(WPARAM nTimeID){} // WM_TIMER
virtual void OnKeyDown(WPARAM nKeyChar){} // WM_KEYDOWN
virtual void OnKeyUp(WPARAM nKeyChar){} // WM_KEYUP
virtual void OnLButtonDown(POINT point){} // WM_LBUTTONDOWN
virtual void OnRButtonDown(POINT point){} // WM_RBUTTONDOWN
virtual void OnMouseMove(POINT point){} // WM_MOUSEMOVE
virtual void OnCloseGame(){} // WM_CLOSE
};
class Init
{
public:
Init(PFUN_CREATE_OBJECT pfn)
{
CGameCtrl::pfnCreateObject = pfn; // 给 函数指针 初始化
}
};
#endif//_CGAMECTRL_H_
WinMain.cpp
#include <windows.h>
#include "CGameCtrl.h"
#include <time.h>
// 加载资源 HINSTANCE 应用程序实例句柄
// 绘图 HDC 环境设备句柄
// 窗口 HWND 窗口句柄
// 创建的句柄 用完要删除
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hIns;
PFUN_CREATE_OBJECT CGameCtrl::pfnCreateObject = NULL; // 给 函数指针 初始化
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR pCmdLine,int nCmdShow)
{
srand((unsigned int)time(NULL));
hIns = hInstance;
// 1. 设计
WNDCLASSEX wndclass;
wndclass.cbClsExtra = 0; // 是否分配 额外空间
wndclass.cbWndExtra = 0; // 是否分配 额外空间
wndclass.cbSize = sizeof(wndclass);
wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW; // 画刷
wndclass.hCursor = NULL;
wndclass.hIcon = NULL;
wndclass.hIconSm = NULL; // 窗口左上的 小图标
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WndProc; // 这个窗口的 处理函数
wndclass.lpszClassName = "ClassName"; // 注册窗口 类的名子
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW|CS_VREDRAW;
// 2. 注册
if(::RegisterClassEx(&wndclass) == FALSE)
{
::MessageBox(NULL,"注册失败!","提示",MB_OK);
return 0;
}
// 3. 创建
HWND hWnd = ::CreateWindow("ClassName","O(∩_∩)O哈哈~",WS_OVERLAPPEDWINDOW,450,0,620+16,1000+38,NULL,NULL,hInstance,NULL);
if(hWnd == NULL)
{
::MessageBox(NULL,"创建失败!","提示",MB_OK);
return 0;
}
// 4. 显示
::ShowWindow(hWnd,SW_SHOW/*nCmdShow*/);
// 5. 交互(消息循环)
MSG message;
while(::GetMessage(&message,NULL,0,0))
{
// 翻译
::TranslateMessage(&message);
// 分发 调用回调函数 处理
::DispatchMessage(&message);
}
return 0;
}
CGameCtrl* pCtrl = NULL;
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_CREATE:
if(CGameCtrl::pfnCreateObject == NULL)
{
::MessageBox(NULL,"游戏创建失败","提示",MB_OK);
::DestroyWindow(hwnd);
::PostQuitMessage(0);
break;
}
else
{
pCtrl = (*(CGameCtrl::pfnCreateObject))(); // 创建一个 子类的 对象
pCtrl->SetHandle(hIns,hwnd); // 设置句柄
pCtrl->OnCreateGame(wParam); // 初始化游戏
}
break;
case WM_PAINT:
pCtrl->OnGameDraw();
break;
case WM_TIMER:
pCtrl->OnGameRun(wParam);
break;
case WM_KEYDOWN:
pCtrl->OnKeyDown(wParam);
break;
case WM_KEYUP:
pCtrl->OnKeyUp(wParam);
break;
case WM_LBUTTONDOWN:
{
POINT point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
pCtrl->OnLButtonDown(point);
}
break;
case WM_RBUTTONDOWN:
{
POINT point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
pCtrl->OnRButtonDown(point);
}
break;
case WM_MOUSEMOVE:
{
POINT point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
pCtrl->OnMouseMove(point);
}
break;
case WM_CLOSE: // 点击 关闭按钮 发送
pCtrl->OnCloseGame(); // 回收 资源
delete pCtrl;
pCtrl = NULL;
::DestroyWindow(hwnd); // 销毁窗口 发送一个 WM_DESTROY
break;
case WM_DESTROY: // 窗口 销毁 执行的 其他的过程
::PostQuitMessage(0); // 退出程序 发送一个 WM_QUIT
break;
case WM_QUIT: // 窗口 销毁 执行的 其他的过程
break;
}
return ::DefWindowProc( hwnd, uMsg, wParam, lParam);
}