【游戏程序设计】匀速直线运动

版权声明:转载请注明出处。 https://blog.csdn.net/baidu_38304645/article/details/82827028

小球碰壁会回弹。

运行结果:

#include <windows.h>
#pragma comment(lib, "winmm.lib")									//调用PlaySound函数所需库文件t
#pragma comment(lib, "Msimg32.lib")

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE L"【游戏程序设计】匀速直线运动"

HINSTANCE hInst;
HDC hdc, mdc, bufdc;
HBITMAP hbg, hball;													//位图句柄,用来存储背景和运动的小球
DWORD g_iNow, g_iPre;												//声明两个变量来记录时间,g_tPre记录上一次绘图的时间,g_tNow记录此次准备绘图的时间                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
RECT rect;															//存储客户区所在的矩形
int x, y, vx, vy;													//x,y存储小球的位置,vx,vy用来表示速度的x分量和y分量

int  MyWindowClass(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyDraw(HDC);

/*****************************************************************************************************************
在不同的应用程序中,在此处添加相关的全局变量
******************************************************************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPreInstace,
									LPSTR lpCmdLine, int nCmdShow) 
{
	MyWindowClass(hInstance);
	PlaySound(L"sound.wav", NULL, SND_FILENAME| SND_ASYNC| SND_LOOP);	//循环播放背景音乐
	if(!InitInstance(hInstance, nCmdShow))
		return FALSE;
	MSG msg = {0};
	while(msg.message != WM_QUIT) 
	{
		if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			g_iNow = GetTickCount();
			if(g_iNow - g_iPre >= 40)
			{
				MyDraw(hdc);
				g_iPre = GetTickCount();
			}
		}
	}

	return msg.wParam;
}

int MyWindowClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = NULL;
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = L"gamebase";
	wcex.hIconSm = NULL;

	return RegisterClassEx(&wcex); 
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	hInst = hInstance;
	HWND hwnd = CreateWindow(L"gamebase", WINDOW_TITLE, WS_OVERLAPPEDWINDOW, 
		CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
	if(!hwnd)
		return FALSE;
	MoveWindow(hwnd, 10, 10, WINDOW_WIDTH, WINDOW_HEIGHT, true);
	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	hdc = GetDC(hwnd);
	mdc = CreateCompatibleDC(hdc);
	bufdc = CreateCompatibleDC(hdc);
	HBITMAP bmp = CreateCompatibleBitmap(hdc, WINDOW_WIDTH, WINDOW_HEIGHT);
	SelectObject(mdc, bmp);
	
	hbg = (HBITMAP)LoadImage(NULL, L"bg.bmp", IMAGE_BITMAP, WINDOW_WIDTH, WINDOW_HEIGHT, LR_LOADFROMFILE);
	hball = (HBITMAP)LoadImage(NULL, L"ball.bmp", IMAGE_BITMAP, 26, 26, LR_LOADFROMFILE);
	//设定小球的初始位置和初始速度分量
	x = 50;
	y = 50;
	vx = 20;
	vy = 20;

	GetClientRect(hwnd, &rect);

	return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_KEYDOWN:										//按下键盘消息
		switch(wParam)										//判断按键的虚拟键码
		{
		case VK_ESCAPE:										//按下【Esc】键
			DestroyWindow(hwnd);
			break;
		}
		break;
/**************************************************************************************************************
在退出程序前,往往在此处删除创建的相关资源
***************************************************************************************************************/
	case WM_DESTROY:
		DeleteObject(hbg);
		DeleteObject(hball);
		DeleteDC(mdc);
		DeleteDC(bufdc);
		ReleaseDC(hwnd, hdc);
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	return 0;
}
/***************************************************************************************************************
在函数MyDraw()中进行相关绘制工作
****************************************************************************************************************/
void MyDraw(HDC hdc)
{
	//绘制背景图片
	SelectObject(bufdc, hbg);
	BitBlt(mdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, bufdc, 0, 0, SRCCOPY);
	//绘制小球图片
	SelectObject(bufdc, hball);
	TransparentBlt(mdc, x, y, 26, 26, bufdc, 0, 0, 26, 26, RGB(0,0,0));
	//将内存DC内容映射到屏幕上
	BitBlt(hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, mdc, 0, 0, SRCCOPY);

	//计算小球的下一次位置
	x += vx;
	if(x > rect.right-26)
	{
		x = rect.right-26;
		vx = -vx;
	}
	else if(x < 0)
	{
		x = 0;
		vx = -vx;
	}

	y += vy;
	if(y > rect.bottom-26)
	{
		y = rect.bottom-26;
		vy = -vy;
	}
	else if(y < 0)
	{
		y = 0;
		vy = -vy;
	}
}

猜你喜欢

转载自blog.csdn.net/baidu_38304645/article/details/82827028