#include "stdafx.h" #include "MyGameFrame.h" #include <stdio.h> //恐龙结构体 //全局变量 HWND hWnd; HINSTANCE hInst; HDC hdc, mdc,bufdc; HBITMAP bg[3],dra; DWORD tPre, tNow; BOOL bPause = FALSE; int i; int x0, x1, x2; int num = 0; // 此代码模块中包含的函数的前向声明: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitWindow(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); VOID CartoonPaint(HDC hdc); void Sort(int n); int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // 初始化全局字符串 MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitWindow(hInstance, nCmdShow))//初始化窗口 { return FALSE;//如果不成功则返回FALSE,并退出程序 } MSG msg; //创建消息类对象 // TODO: 在此放置运行代码。 //定义局部变量 HBITMAP bmp; char filename[20] = ""; //建立内存DC和窗口DC hdc = GetDC(hWnd); mdc = CreateCompatibleDC(hdc); bufdc = CreateCompatibleDC(hdc); //建立适用位图 bmp = CreateCompatibleBitmap(hdc,640,480); SelectObject(mdc, bmp); //加载图片 for (i = 0; i < 3; i++) { sprintf_s(filename, "bg%d.bmp", i); if(i<1) bg[i] = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 640, 480, LR_LOADFROMFILE); bg[i] = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 640, 600, LR_LOADFROMFILE); } dra = (HBITMAP)LoadImage(NULL, "dra2.bmp", IMAGE_BITMAP, 760, 198, LR_LOADFROMFILE); //绘制恐龙 CartoonPaint(hdc); // 主消息循环: PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);//赋初值 while (msg.message != WM_QUIT) //进入游戏消息循环: { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); //获得游戏玩家输入的消息; DispatchMessage(&msg);//分配玩家消息并响应用户消息。 } else { tNow = GetTickCount(); if (tNow - tPre >= 30) if (bPause) CartoonPaint(hdc); } } return (int)msg.wParam; } // // 函数: MyRegisterClass() // // 注册Windows类,一款游戏有且仅有一个主窗口,与游戏程序唯一对应。创建游戏窗口前要填写窗口类结构体WNDCLASSEX // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex = { wcex.cbSize = sizeof(WNDCLASSEX), CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW, WndProc,0,0, hInstance, LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYGAMEFRAME)), LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW + 1),NULL, _T("GameFrame"), LoadIcon(NULL,MAKEINTRESOURCE(IDI_SMALL)) }; return RegisterClassEx(&wcex); } // // 函数: InitInstance(HINSTANCE, int) // // 目的: 保存实例句柄并创建主窗口 // // 注释: // // 在此函数中,我们在全局变量中保存实例句柄并 // 创建和显示主程序窗口。 // BOOL InitWindow(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 将实例句柄存储在全局变量中 hWnd = CreateWindow ( _T("GameFrame"), _T("游戏框架"), WS_OVERLAPPEDWINDOW^WS_THICKFRAME^WS_MAXIMIZEBOX,//普通样式,不能改变大小,不能最大化 100, 100, 640,500, nullptr, nullptr, hInstance, nullptr ); 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 msg, WPARAM wparam, LPARAM lparam) { switch (msg) //判断消息 { case WM_CREATE: //执行初始化代码 return(0); break; //键盘控制龙运动或静止 case WM_CHAR: bPause = !bPause; break; case WM_DESTROY: //关闭程序,发送WM_QUIT消息 PostQuitMessage(0); return(0); break; default: break; } return (DefWindowProc(hwnd, msg, wparam, lparam)); } void CartoonPaint(HDC hdc) { //绘制背景 //贴天空图 SelectObject(bufdc, bg[0]); BitBlt(mdc, 0, 0, x0, 414, bufdc, 640 - x0, 0, SRCCOPY); BitBlt(mdc, x0, 0, 640 - x0, 414, bufdc, 0, 0, SRCCOPY); //贴草地图 BitBlt(mdc, 0, 375, x2, 300, bufdc, 640 - x2, 414, SRCCOPY); BitBlt(mdc, x2, 375, 640 - x2, 300, bufdc, 0,414, SRCCOPY); //贴山峦并透明 SelectObject(bufdc, bg[1]); BitBlt(mdc, 0, 75, x1, 300, bufdc, 640 - x1, 300, SRCAND); BitBlt(mdc, x1, 75, 640 - x1, 300, bufdc, 0, 300, SRCAND); BitBlt(mdc, 0, 75, x1, 300, bufdc, 640 - x1, 0, SRCPAINT); BitBlt(mdc, x1, 75, 640 - x1, 300, bufdc, 0, 0, SRCPAINT); //贴房屋并透明 SelectObject(bufdc, bg[2]); BitBlt(mdc, 0, 325, x2, 300, bufdc, 640 - x2, 300, SRCAND); BitBlt(mdc, x2, 325, 640 - x2, 300, bufdc, 0, 300, SRCAND); BitBlt(mdc, 0, 325, x2, 300, bufdc, 640 - x2, 0, SRCPAINT); BitBlt(mdc, x2, 325, 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 += 1; if (x0 >= 640) x0 = 0; //设置山峦背景切割宽度,运动速度 x1 += 2; if (x1 >= 640) x1 = 0; //设置草地及房屋切割宽度,运动速度 x2 += 4; if (x2 >= 640) x2 = 0; num++; //恐龙图号 if (num == 8) num = 0; }
结果如图所示: