//资源图片

 

 

 

#include "MainClass.h"

#define WINDOW_WIDTH 640 //为窗口宽度定义的宏,以方便在此处修改窗口宽度
#define WINDOW_HEIGHT 480 //为窗口高度定义的宏,以方便在此处修改窗口高度
#define WINDOW_TITLE "游戏开发的梦想" //为窗口标题定义的宏

//全局变量
HBITMAP dra, bg[3];
HDC hdc, mdc, bufdc;
HWND hWnd;
DWORD tPre, tNow;
int x0 = 0, x1 = 0, x2 = 0, num = 0;


//--------------------------------------------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)
{
char filename[20] = "";
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);
//将空的位图转入mdc中
SelectObject(mdc, bmp);
//加载图片
bg[0] = (HBITMAP)LoadImage(NULL, "bg0.bmp", IMAGE_BITMAP, 640, 480, LR_LOADFROMFILE);
bg[1] = (HBITMAP)LoadImage(NULL, "bg1.bmp", IMAGE_BITMAP, 640, 600, LR_LOADFROMFILE);
bg[2] = (HBITMAP)LoadImage(NULL, "bg2.bmp", IMAGE_BITMAP, 640, 600, LR_LOADFROMFILE);
dra = (HBITMAP)LoadImage(NULL, "dra.bmp", IMAGE_BITMAP, 760, 198, LR_LOADFROMFILE);

MyPaint(hdc);
return TRUE;
}

//--------------------------------------------MyPaint() 函数---------------------------------------------------
//绘制图形
//1.按照各背景远近顺序进行循环背景贴图
//2.进行前景恐龙图的透明贴图
//3.重设背景图的切割宽度与跑动恐龙图的图号
//-------------------------------------------------------------------------------------------------------------
void MyPaint(HDC hdc)
{
//贴上天空图
SelectObject(bufdc, bg[0]);
BitBlt(mdc, 0, 0, x0, 300, bufdc, 640 - x0, 0, SRCCOPY);
BitBlt(mdc, x0, 0, 640 - x0, 300, bufdc, 0, 0, SRCCOPY);

//贴上草地图
BitBlt(mdc, 0, 300, x2, 180, bufdc, 640 - x2, 300, SRCCOPY);
BitBlt(mdc, x2, 300, 640 - x2, 180, bufdc, 0, 300, SRCCOPY);

//贴上山峦图并处理透明
SelectObject(bufdc, bg[1]);
BitBlt(mdc, 0, 0, x1, 300, bufdc, 640 - x1, 300, SRCAND);
BitBlt(mdc, x1, 0, 640 - x1, 300, bufdc, 0, 300, SRCAND);
BitBlt(mdc, 0, 0, x1, 300, bufdc, 640 - x1, 0, SRCPAINT);
BitBlt(mdc, x1, 0, 640 - x1, 300, bufdc, 0, 0, SRCPAINT);

//贴上房屋图并透明处理
SelectObject(bufdc, bg[2]);
BitBlt(mdc, 0, 250, x2, 300, bufdc, 640 - x2, 300, SRCAND);
BitBlt(mdc, x2, 250, 640 - x2, 300, bufdc, 0, 300, SRCAND);
BitBlt(mdc, 0, 250, x2, 300, bufdc, 640 - x2, 0, SRCPAINT);
BitBlt(mdc, x2, 250, 640 - x2, 300, bufdc, 0, 0, SRCPAINT);

//贴上恐龙图并透明处理
SelectObject(bufdc, dra);
BitBlt(mdc, 250, 350, 95, 99, bufdc, num * 95, 99, SRCAND);
BitBlt(mdc, 250, 350, 95, 99, bufdc, num * 95, 0, SRCPAINT);

BitBlt(hdc, 0, 0, 640, 480, mdc, 0, 0, SRCCOPY);

tPre = GetTickCount();

x0 += 5; //重设天空背景切割宽度
if (x0 == 640)
x0 = 0;

x1 += 8; //重设山峦背景切割宽度
if (x1 == 640)
x1 = 0;

x2 += 16; //重设草地及房屋背景切割宽度
if (x2 == 640)
x2 = 0;

num++; //重设跑动图的图号
if (num == 8)
num = 0;
}


//--------------------------------------------Game_CleanUp() 函数----------------------------------------------
//资源清理函数,在此函数中进行退出前的资源的清理工作
//-------------------------------------------------------------------------------------------------------------
BOOL Game_CleanUp()
{
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(dra);
for (int i = 0; i < 3; i++)
{
DeleteObject(bg[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: //上
break;
case VK_DOWN: //下
break;
case VK_LEFT: //左
break;
case VK_RIGHT: //右
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;//正常退出
}

 

posted on 2015-11-23 14:47  zishen  阅读(524)  评论(0编辑  收藏  举报