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

游戏基础010---单一背景滚动

源码下载:/2012/0118/20120118081046132.rar

/************************************************************************/ 
/* 
单一背景滚动:利用一张相当大的背景图,当游戏进行的时候,随着画面中人物的
移动,背景的现实区域也跟着移动。只要在每次背景画面更新时改变要显示到窗口
上的区域就可以了
*/ 
/************************************************************************// 
#include "stdafx.h" 
 
//全局变量声明 
HINSTANCE hInst; 
HBITMAP map; 
HDC     hdc,mdc; 
HWND    hWnd; 
DWORD   tPre,tNow; 
int     x=0,y=0; 
 
 
//全局函数 
void MyPaint(HDC); 
ATOM                MyRegisterClass(HINSTANCE hInstance); 
BOOL                InitInstance(HINSTANCE, int); 
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM); 
 
//****程序入口************************************** 
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 >= 10) 
            { 
                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] = ""; 
    int i; 
 
    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); 
 
    map = (HBITMAP)LoadImage(NULL,"map.bmp",IMAGE_BITMAP,1548,1129,LR_LOADFROMFILE); 
 
    SelectObject(mdc,map); 
 
    MyPaint(hdc); 
 
    return TRUE; 

 www.zzzyk.com
/************************************************************************/ 
/*
按背景图剪裁左上角坐标经行贴图
*/ 
/************************************************************************/ 
void MyPaint(HDC hdc) 

    BitBlt(hdc,0,0,640,480,mdc,x,y,SRCCOPY); 
    tPre = GetTickCount(); 

 
 
//****回调函数*********************************** 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 

 
    switch (message)  
    { 
    case WM_KEYDOWN:                 //按键消息 
        switch (wParam)  
        { 
        case VK_UP:              //按下向上箭头 
            y -= 20; 
            if(y < 0) 
    
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,