>_<:理解上一个时间函数的概念和用法,本节的实现也比较简单

>_<:就是简单的绘图+时间函数

[游戏学习27] MFC 匀速运动-LMLPHP

>_<:TicTac.h

 #define EX 1            //该点左鼠标
#define OH 2 //该点右鼠标 class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance ();
}; class CMainWindow : public CFrameWnd //不是继承CFrameWnd 因此需要在CMainWindow()自己定义窗口类了
{
protected:
CPoint pos,killpos;
int vx,vy,ax,ay;
int killvx,killvy,killax,killay;
int num,Length; public:
CMainWindow (); protected:
virtual void PostNcDestroy ();//在程序终止之前销毁CMainWindow对象
afx_msg int OnCreate (LPCREATESTRUCT lpcs);
afx_msg void OnTimer (UINT nTimerID);
DECLARE_MESSAGE_MAP ()
};

>_<:TicTac.cpp

 #include <afxwin.h>
#include "TicTac.h"
#define ID_TIMER_ELLIPSE 1
#define ID_TIMER_RECTANGLE 2 CMyApp myApp; /////////////////////////////////////////////////////////////////////////
// CMyApp member functions BOOL CMyApp::InitInstance ()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
} /////////////////////////////////////////////////////////////////////////
// CMainWindow message map and member functions BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_CREATE ()
ON_WM_TIMER ()
END_MESSAGE_MAP () CMainWindow::CMainWindow ()
{
//初始化游戏
//InitGame();
pos=CPoint(,);
vx=killvx=;
vy=killvy= ;
ax=ay=killax=killay=;
Length=;
num=; //注册一个 WNDCLASS 窗口类.
CString strWndClass = AfxRegisterWndClass (
CS_DBLCLKS, // Class style(有双击时间发生的窗口类型)
AfxGetApp ()->LoadStandardCursor (IDC_ARROW), // Class cursor(加载一个系统光标,也可自己定义)
(HBRUSH) (COLOR_3DFACE + ), // Background brush(每次::BeginPaint时用它清空客户区);COLOR_3DFACE+1是指定窗口具有与按钮对话框一致的背景色和其他一些3D属性;默认为灰亮色
AfxGetApp ()->LoadStandardIcon (IDI_WINLOGO) // Class icon(加载系统图标,也可自己定义)
); //调用CWnd::CreateEx()创建主窗口
//第一个参数表示0个或是多个WS_EX标志组合;2:AfxRegisterWndClass()返回的WNDCLASS名称;
//3、标题;4、窗口样式
CreateEx (, strWndClass, _T ("Timer"),
WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX, //WS_THICKFRAME窗口可调大小属性(这里不用)
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, //初始位置和大小,这里用CW_USEDEFAULT让Windows拾取窗口和大小
NULL, NULL); //处理窗口位置和尺寸
CRect rect (, , , ); //理想客户区窗口矩形形状
CalcWindowRect (&rect); //根据分辨率、菜单...计算窗口矩形大小(必须在窗口创建后调用) SetWindowPos (NULL, , , rect.Width (), rect.Height (),
SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);
} //在程序结束之前销毁创建的CMainWindow对象
void CMainWindow::PostNcDestroy ()
{
delete this;
} int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)//回掉函数
{
if (CFrameWnd::OnCreate (lpcs) == -)
return -; if (!SetTimer (ID_TIMER_ELLIPSE, , NULL)) {
MessageBox (_T ("Error: SetTimer failed"));
return -;
}
return ;
} void CMainWindow::OnTimer (UINT nTimerID)
{
CRect rect;
GetClientRect (&rect); pos.x+=vx;
if(pos.x>rect.right-){
pos.x=rect.right-;
vx=-vx;
}
if(pos.x<){
pos.x=;
vx=-vx;
}
pos.y+=vy;
if(pos.y>rect.bottom-){
pos.y=rect.bottom-;
vy=-vy;
}
if(pos.y<){
pos.y=;
vy=-vy;
} if(num++==)killpos=pos;
if(num>=Length){
killpos.x+=killvx;
if(killpos.x>rect.right-){
killpos.x=rect.right-;
killvx=-killvx;
}
if(killpos.x<){
killpos.x=;
killvx=-killvx;
}
killpos.y+=killvy;
if(killpos.y>rect.bottom-){
killpos.y=rect.bottom-;
killvy=-killvy;
}
if(killpos.y<){
killpos.y=;
killvy=-killvy;
}} CClientDC dc (this); CPen pen(PS_NULL,,RGB(,,));
dc.SelectObject(&pen);
if(num==)dc.Rectangle(rect);
dc.Ellipse(killpos.x,killpos.y,killpos.x+,killpos.y+);
::DeleteObject(&pen); CBrush brush(RGB(,,));
dc.SelectObject (&brush);
dc.Ellipse(pos.x,pos.y,pos.x+,pos.y+);
::DeleteObject(&brush); CBrush brush1(RGB(,,));
dc.SelectObject (&brush1);
dc.Ellipse(pos.x+,pos.y+,pos.x+,pos.y+);
::DeleteObject(&brush1);
}
05-02 02:42