当前位置:编程学习 > 网站相关 >>

游戏基础012---多背景循环动画

代码下载 /2012/0118/20120118081212897.rar

 /************************************************************************/ 
/* 
多背景循环动画的背景循环原理与前面讲的背景训话原理相同,不过由于不同
背景在远近层次上机实际视觉移动速度上并不相同,因此在贴图时需要决定不同
背景贴图的先后顺序及滚动速度
*/ 
/************************************************************************// 
#include "stdafx.h" 
 
//全局变量声明 
HINSTANCE hInst; 
HBITMAP dra,bg[3];//bg[3]表示3组背景 
HDC     hdc,mdc,bufdc; 
HWND    hWnd; 
DWORD   tPre,tNow; 
int     x0=0,x1=0,x2=0,num=0; 
 
 
//全局函数 
void MyPaint(HDC); 
ATOM                MyRegisterClass(HINSTANCE hInstance); 
BOOL                InitInstance(HINSTANCE, int); 
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM); 
void                MyPaint(HDC hdc); 
 
//****程序入口************************************** 
int APIENTRY WinMain(HINSTANCE hInstance, 
                     HINSTANCE hPrevInstance, 
                     LPSTR     lpCmdLine, 
                     int       nCmdShow) 

    MSG msg; 
 
    MyRegisterClass(hInstance); 
 
    //运行初始化函数 
    if (!InitInstance (hInstance, nCmdShow))  
    { 
        return FALSE; 
    } 
 
    //游戏循环 
    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 >= 100) 
            { 
                MyPaint(hdc); 
            } 
        } 
 
    } 
 
    return msg.wParam; 

 
//****定义及注册窗口类别函数************************* 
ATOM MyRegisterClass(HINSTANCE hInstance) 

    WNDCLASSEX wcex; 
 
    wcex.cbSize = sizeof(WNDCLASSEX);  
    wcex.style          = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc    = (WNDPROC)WndProc; 
    wcex.cbClsExtra     = 0; 
    wcex.cbWndExtra     = 0; 
    wcex.hInstance      = hInstance; 
    wcex.hIcon          = NULL; 
    wcex.hCursor        = NULL; 
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1); 
    wcex.lpszMenuName   = NULL; 
    wcex.lpszClassName  = "canvas";  //类别名称 
    wcex.hIconSm        = NULL; 
 
    return RegisterClassEx(&wcex); 

 
//****初始化************************************* 
// 加载位图并设定各对象初始值 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 

    char filename[20] = ""; 
    HBITMAP bmp; 
    hInst = hInstance; 
 
    hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW, 
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 
 
    if (!hWnd) 
    { 
        return FALSE; 
    } 
 
    MoveWindow(hWnd,10,10,600,450,true); 
    ShowWindow(hWnd, nCmdShow); 
    UpdateWindow(hWnd); 
 
    hdc = GetDC(hWnd); 
    mdc = CreateCompatibleDC(hdc); 
    bufdc = CreateCompatibleDC(hdc); 
 
    bmp = CreateCompatibleBitmap(hdc,640,480); 
    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; 

 
/************************************************************************/ 
/*
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); 
 
    //贴
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,