设为首页 加入收藏

TOP

(Visual C++)游戏开发笔记十八 游戏基础物理建模(一) 匀速与加速运动 (二)
2014-11-23 19:42:51 】 浏览:470
Tags:Visual 游戏 开发 笔记 十八 基础 物理 建模 匀速 加速运动
true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
bmp = CreateCompatibleBitmap(hdc,640,480);

SelectObject(mdc,bmp);

bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);
bird = (HBITMAP)LoadImage(NULL,"angrybird.bmp",IMAGE_BITMAP,120,60,LR_LOADFROMFILE);

GetClientRect(hWnd,&rect); //取得内部窗口区域的大小
MyPaint(hdc);

return TRUE;
}

//****自定义绘图函数*********************************
// 1.进行窗口贴图
// 2.计算小鸟贴图坐标并判断小鸟是否碰到窗口边沿
void MyPaint(HDC hdc)
{
SelectObject(bufdc,bg);
BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);

SelectObject(bufdc,bird);
BitBlt(mdc,x,y,60,60,bufdc,60,0,SRCAND);
BitBlt(mdc,x,y,60,60,bufdc,0,0,SRCPAINT);


BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);

//计算X轴贴图坐标与速度
x += vx;
if(x <= 0)
{
x = 0;
vx = -vx;
}
else if(x >= rect.right-60)
{
x = rect.right - 60;
vx = -vx;
}

//计算Y轴贴图坐标与速度
y += vy;
if(y<=0)
{
y = 0;
vy = -vy;
}
else if(y >= rect.bottom-60)
{
y = rect.bottom - 60;
vy = -vy;
}

tPre = GetTickCount(); //记录此次绘图时间
}

////****消息处理函数***********************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_KEYDOWN: //按键消息
if(wParam==VK_ESCAPE) //按下【Esc】键
PostQuitMessage(0);
break;
case WM_DESTROY: //窗口结束消息
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(bg);
DeleteObject(bird);
ReleaseDC(hWnd,hdc);
PostQuitMessage(0);
break;
default: //其他消息
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
#include "stdafx.h"
#include


//全局变量声明
HINSTANCE hInst;
HBITMAP bg,bird;
HDC hdc,mdc,bufdc;
HWND hWnd;
DWORD tPre,tNow,tCheck;
RECT rect; //定义一个RECT结构体,用于储存内部窗口区域的坐标
int x=50,y=50,vx=15,vy=15; //x与y是小鸟在窗口中的贴图坐标,vx与vy为小鸟在x与y轴运动的速度分量

//全局函数声明
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);

//****WinMain函数,程序入口点函数**************************************
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

MyRegisterClass(hInstance);

//初始化
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

//消息循环
GetMessage(&msg,NULL,NULL,NULL); //初始化msg
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
tNow = GetTickCount();
if(tNow-tPre >= 40)
MyPaint(hdc);
}
}

return msg.wParam;
}

//****设计一个窗口类,类似填空题,使用窗口结构体*********************
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇(Visual C++)游戏开发笔记二十.. 下一篇(Visual C++)游戏开发笔记二十..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目