#include
#include
#include
HDC windowDC = NULL;//窗口DC
RECT rect;
HDC memDC =NULL;//记忆DC
HWND hwnd ;
HFONT Font;//字体物件
HDC flowerDC=NULL;//鲜花图片DC
HBITMAP flower=NULL;
UINT width;//屏幕宽
UINT height;//屏幕高
int Role_Status;//运动方向
bool isWin = false;
//定义运动方向
#define Role_UP 0
#define Role_DOWN 1
#define Role_LEFT 2
#define Role_RIGHT 3
char * ch ="尤一炜是个傻冒。";
int character[3][3]={{3,2,0},
{4,8,1},
{5,6,7}};
//int character[3][3]={{0,1,2},
// {3,7,5},
// {6,4,8}};
//空白图片所在的位置
int focusX=1;
int focusY=1;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void DoMain(void);//主循环
void Move(void);//移动主角
void ClearScr(void);//清屏
void ShowScr(void);//显示屏幕
void showFlower(void);//显示鲜花
void swap(int &a,int &b);
bool checkWin(void);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("尤一炜是个傻冒。") ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;//回调消息处理函数
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
//wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;//窗口图标
wndclass.hIcon = LoadIcon (hInstance, "QQ.ico") ;//窗口图标
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName= szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
width=GetSystemMetrics(SM_CXSCREEN);
height= GetSystemMetrics(SM_CYSCREEN);
hwnd = CreateWindow( szAppName, // window class name
TEXT ("尤一炜是个傻冒。"), // window caption
WS_OVERLAPPEDWINDOW, // window style
width/2-150,// initial x position
height/2-150,// initial y position
316,// initial x size
337,// initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
windowDC=GetDC(hwnd);
//创建字体
Font=CreateFont(100,
0,
0,
0,
400,
FALSE,
FALSE,
0,
GB2312_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS,
"黑体");
SelectObject(windowDC,Font);
SetBkMode(windowDC,TRANSPARENT);
SetTextColor(windowDC,RGB(0,0,255));
flowerDC=CreateCompatibleDC(windowDC);
flower=CreateCompatibleBitmap(windowDC,width,height);
flower=(HBITMAP)LoadImage(NULL,"flower.bmp",IMAGE_BITMAP,310,310,LR_LOADFROMFILE);
SelectObject(flowerDC,flower);
//启动前选让电脑随机走100步,打乱图片
srand((unsigned)time(0));
for(int i =0;i < 100;i++)
{
Role_Status = rand () % 4;
Move();
}
ShowScr();
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
if(false==isWin)
{
ClearScr();
DoMain();
ShowScr();
if(checkWin())
{
showFlower();//游戏结束,显示鲜花图片
}
}
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
//DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
// DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
//return 0 ;
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"确定结束游戏?","确定结束游戏?",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage (0) ;
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0 ;
}
void DoMain(void)
{
if(GetAsyncKeyState(VK_UP)<0)//获取按键状态:上
{
Role_Status=Role_UP;
Move();
}
if(GetAsyncKeyState(VK_DOWN)<0)//获取按键状态:下
{
Role_Status=Role_DOWN;
Move();
}
if(GetAsyncKeyState(VK_LEFT)<0)//获取按键状态:左
{
Role_Status=Role_LEFT;
Move();
}
if(GetAsyncKeyState(VK_RIGHT)<0)//获取按键状态:右
{
Role_Status=Role_RIGHT;
Move();
}
if(GetAsyncKeyState(VK_SPACE)<0)//获取按键状态:空格
{
}
}
void Move(void)
{
//ClearScr();
switch (Role_Status)
{
case Role_UP:
if(0==focusY||1==focusY)
{
swap(character[focusY][focusX],character[focusY+1][focusX]);
focusY+=1;
}
break;
case Role_DOWN:
if(1==focusY||2==focusY)
{
swap(character[focusY][focusX],character[focusY-1][focusX]);
focusY-=1;
}
break;
case Role_LEFT:
if(0==focusX||1==focusX)
{
swap(character[focusY][focusX],character[focusY][focusX+1]);
focusX+=1;
}
break;
case Role_RIGHT:
if(1==focusX||2==focusX)
{
swap(character[focusY][focusX],character[focusY][focusX-1]);
focusX-=1;
}
break;
}
//ShowScr();
//if(checkWin())
//{
// showFlower();
// //ClearScr();
// //TextOut(windowDC,0,100,"真聪明",strlen("真聪明"));
//}
}
void ClearScr(void)
{
HBRUSH hBrush ;
HDC hdc ;
rect.left =0;
rect.top =0;
rect.right =300;
rect.bottom = 300;
hBrush = CreateSolidBrush ( RGB (255,255,255)) ;
hdc = GetDC (hwnd) ;
FillRect(hdc, &rect, hBrush);
}
void ShowScr(void)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
TextOut(windowDC,j*100,i*100,ch+character[i][j]*2,2);
}
}
//画4条直线,分成9格
MoveToEx (windowDC, 100, 0, NULL) ;
LineTo (windowDC, 100,300) ;
MoveToEx (windowDC, 200, 0, NULL) ;
LineTo (windowDC, 200,300) ;
MoveToEx (windowDC, 0, 100, NULL) ;
LineTo (windowDC, 300,100) ;
MoveToEx (windowDC, 0, 200, NULL) ;
LineTo (windowDC, 300,200) ;
}
void swap(int &a,int &b)
{
int temp =a;
a=b;
b=temp;
}
bool checkWin()
{
int k=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(k!=character[i][j])
{
return false;
}
k++;
}
}
isWin=true;
return true;
}
void showFlower(void)
{
BitBlt(windowDC,0,0,width,height,flowerDC,0,0,SRCCOPY);
}
求大神注释每一行代码啊?