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

源码地址:http://download.csdn.net/detail/liu_liu213/4027060

/************************************************************************/
/* 
单一背景滚动:利用一张相当大的背景图,当游戏进行的时候,随着画面中人物的
移动,背景的现实区域也跟着移动。只要在每次背景画面更新时改变要显示到窗口
上的区域就可以了
*/
/************************************************************************//
#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;
}

/************************************************************************/
/*
按背景图剪裁左上角坐标经行贴图
*/
/************************************************************************/
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)
				y = 0;
			break;
		case VK_DOWN:			
			y += 20;
			if(y > 660)
				y = 660;
			break;
		case VK_LEFT:			 
			x -= 20;
			if(x < 0)
				x = 0;
			break;
		case VK_RIGHT:			
			x += 20;
			if(x > 910)
				x = 910;
			break;
		}
		break;
		case WM_DESTROY:	
			DeleteDC(mdc);
			DeleteObject(map);
			ReleaseDC(hWnd,hdc);
			PostQuitMessage(0);
			break;
		default:							
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值