#include "MainClass.h"
#define WINDOW_WIDTH 640 //为窗口宽度定义的宏,以方便在此处修改窗口宽度
#define WINDOW_HEIGHT 480 //为窗口高度定义的宏,以方便在此处修改窗口高度
#define WINDOW_TITLE "游戏开发的梦想" //为窗口标题定义的宏
//全局变量
HBITMAP girl[4], bg;
HDC hdc, mdc, bufdc;
HWND hWnd;
DWORD tPre, tNow;
int num, dir, x, y;
//--------------------------------------------WinMain() 入口函数-----------------------------------------------
//应用程序的入口函数
//-------------------------------------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg = { 0 };//定义并初始化msg
MyRegisterClass(hInstance);
if (!InitInstance(hInstance, nShowCmd))
{
return FALSE;
}
//游戏循环
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
tNow = GetTickCount();
if (tNow - tPre >= 100)//相差0.1秒时再进行重绘操作
MyPaint(hdc);
}
}
return msg.wParam;
}
//--------------------------------------------MyRegisterClass() 函数-------------------------------------------
//注册窗口
//-------------------------------------------------------------------------------------------------------------
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//创建窗口第一步:开始设计一个完整的窗口类
WNDCLASSEX wndClass;//定义一个窗口类
wndClass.cbSize = sizeof(WNDCLASSEX);//设置结构体的字节数大小
wndClass.style = CS_HREDRAW | CS_VREDRAW;//设置窗口的样式
wndClass.lpfnWndProc = (WNDPROC)WndProc;//设置指向窗口过程函数的指针
wndClass.cbClsExtra = 0;//窗口类的附加内存,取0就可以了
wndClass.cbWndExtra = 0;//窗口的附加内存,依然取0就行了
wndClass.hInstance = hInstance;//指定包含窗口过程的程序的实例句柄
wndClass.hIcon = (HICON) ::LoadImage(NULL, "icon.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);//加载自定义ico图标
wndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);//指定窗口类的光标句柄
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//指定一个灰色画刷句柄
wndClass.lpszMenuName = NULL;//用一个以空终止的字符串,指定菜单资源的名字.
wndClass.lpszClassName = "GameClass";//指定窗口类的名称
//wndClass.hIconsm = NULL;
//创建窗口第二步:注册窗口类
return RegisterClassEx(&wndClass);//设计完窗口后,需要对窗口类进行注册,这样才能创建该类型的窗口
}
//--------------------------------------------InitInstance() 函数----------------------------------------------
//加载资源
//-------------------------------------------------------------------------------------------------------------
BOOL InitInstance(HINSTANCE hInstance, int nShowCmd)
{
HBITMAP bmp;
//创建窗口第三步:正式创建窗口
//创建窗口函数
hWnd = CreateWindow("GameClass", WINDOW_TITLE, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
//创建窗口第四步:窗口的移动,显示和更新
MoveWindow(hWnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, true);//调整窗口显示时的位置及窗口的大小
ShowWindow(hWnd, nShowCmd);//设定显示窗口时的状态
UpdateWindow(hWnd);//将窗口绘制于显示设备上
hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
//建立空白位图
bmp = CreateCompatibleBitmap(hdc, 640, 480);
SelectObject(mdc, bmp);
x = 300;
y = 250;
dir = 0;
num = 0;
//加载图片
girl[0] = (HBITMAP)LoadImage(NULL, "girl0.bmp", IMAGE_BITMAP, 440, 148, LR_LOADFROMFILE);
girl[1] = (HBITMAP)LoadImage(NULL, "girl1.bmp", IMAGE_BITMAP, 424, 154, LR_LOADFROMFILE);
girl[2] = (HBITMAP)LoadImage(NULL, "girl2.bmp", IMAGE_BITMAP, 480, 148, LR_LOADFROMFILE);
girl[3] = (HBITMAP)LoadImage(NULL, "girl3.bmp", IMAGE_BITMAP, 480, 148, LR_LOADFROMFILE);
bg = (HBITMAP)LoadImage(NULL, "bg.bmp", IMAGE_BITMAP, 640, 480, LR_LOADFROMFILE);
MyPaint(hdc);
return TRUE;
}
//--------------------------------------------MyPaint() 函数---------------------------------------------------
//绘制图形
//-------------------------------------------------------------------------------------------------------------
void MyPaint(HDC hdc)
{
int w, h;
SelectObject(bufdc, bg);
BitBlt(mdc, 0, 0, 640, 480, bufdc, 0, 0, SRCCOPY);
SelectObject(bufdc, girl[dir]);
switch (dir)
{
case 0:
w = 55;
h = 74;
break;
case 1:
w = 53;
h = 77;
break;
case 2:
w = 60;
h = 74;
break;
case 3:
w = 60;
h = 74;
break;
}
//透明处理
BitBlt(mdc, x, y, w, h, bufdc, num*w, h, SRCAND);
BitBlt(mdc, x, y, w, h, bufdc, num*w, 0, SRCPAINT);
//贴图
BitBlt(hdc, 0, 0, 640, 480, mdc, 0, 0, SRCCOPY);
num++;
if (num == 8)
num = 0;
tPre = GetTickCount(); //记录此次绘图时间
}
//--------------------------------------------Game_CleanUp() 函数----------------------------------------------
//资源清理函数,在此函数中进行退出前的资源的清理工作
//-------------------------------------------------------------------------------------------------------------
BOOL Game_CleanUp()
{
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(bg);
for (int i = 0; i < 4; i++)
{
DeleteObject(girl[i]);
}
ReleaseDC(hWnd, hdc);
return TRUE;
}
//--------------------------------------------WndProc() 窗口消息回调函数---------------------------------------
//窗口过程函数WndProc,对窗口消息进行处理
//-------------------------------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
switch (message)
{
case WM_PAINT://窗口重绘消息
{
hdc = BeginPaint(hwnd, &ps);
MyPaint(hdc);
EndPaint(hwnd, &ps);
}
break;
case WM_TIMER://时间消息
{
MyPaint(hdc);
}break;
case WM_KEYDOWN://若是键盘按下消息
{
if (wParam == VK_ESCAPE)//如果按下的键是ESC
{
if (MessageBox(hwnd, "退出?", "提示", MB_YESNO | MB_ICONQUESTION) == IDYES)
DestroyWindow(hwnd);//销毁窗口,并发送一条WM_DESTROY消息
}
switch (wParam)
{
case VK_UP: //上
{
switch (dir)
{
case 0:
y -= 10;
break;
case 1:
x -= 1;
y -= 8;
break;
case 2:
x += 2;
y -= 10;
break;
case 3:
x += 2;
y -= 10;
break;
}
if (y < 0)
y = 0;
dir = 0;//方向
}
break;
case VK_DOWN: //下
{
switch (dir)
{
case 0:
x += 1;
y += 8;
break;
case 1:
y += 10;
break;
case 2:
x += 3;
y += 6;
break;
case 3:
x += 3;
y += 6;
break;
}
if (y > 375)
y = 375;
dir = 1;//方向
}
break;
case VK_LEFT: //左
{
switch (dir)
{
case 0:
x -= 12;
break;
case 1:
x -= 13;
y += 4;
break;
case 2:
x -= 10;
break;
case 3:
x -= 10;
break;
}
if (x < 0)
x = 0;
dir = 2;//方向
}
break;
case VK_RIGHT: //右
{
switch (dir)
{
case 0:
x += 8;
break;
case 1:
x += 7;
y += 4;
break;
case 2:
x += 10;
break;
case 3:
x += 10;
break;
}
if (x > 575)
x = 575;
dir = 3;//方向
}
break;
}
}
break;
case WM_CLOSE://关闭按钮
{
if (MessageBox(hwnd, "是否要退出?", "提示", MB_YESNO | MB_ICONQUESTION) == IDYES)
DestroyWindow(hwnd);//销毁窗口,并发送一条WM_DESTROY消息
else
return(0);
}
break;
case WM_DESTROY://窗口结束消息
{
//删除资源
Game_CleanUp();
PostQuitMessage(0);//向系统表明有个线程有终止的请求,用来响应WM_DESTROY消息
}
break;
default://若是上述case条件都不符合,则执行该default语句
{
return DefWindowProc(hwnd, message, wParam, lParam);//调用默认的窗口过程
}
}
return 0;//正常退出
}