#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;
}
結果如圖所示: