⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 canvasframe.cpp

📁 《Visual C++游戏设计入门》的配套代码
💻 CPP
字号:
// canvasFrame.cpp : implementation file
//

#include "stdafx.h"
#include "canvasr.h"
#include "canvasFrame.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// canvasFrame
IMPLEMENT_DYNCREATE(canvasFrame, CFrameWnd)
float x,y,vx=9,vy=0,ax=-0.5,ay=8,g=15,xlast,ylast;
canvasFrame::canvasFrame()
{
	Create(NULL,"绘图窗口");
	CClientDC dc(this);
	int width = dc.GetDeviceCaps(HORZRES);
	int height = dc.GetDeviceCaps(VERTRES);
	GetWindowRect( &rect );
	width = ( width - ( rect.right - rect.left ))/2 ;
	height = (height - (rect.bottom - rect.top ))/2 ;
	MoveWindow( width , height , (rect.right - rect.left ) , (rect.bottom - rect.top ) ,true);
	GetClientRect(&rect);
	mdc = new CDC;
	mdc->CreateCompatibleDC(&dc);
	ball = new CBitmap;
	ball->m_hObject = (HBITMAP)::LoadImage(NULL,"ball.bmp",IMAGE_BITMAP,81,81,LR_LOADFROMFILE); //载入小球图
	mdc->SelectObject(ball); //选择位图到 mdc 中
}

canvasFrame::~canvasFrame()
{
	delete mdc;
	delete ball;
}


BEGIN_MESSAGE_MAP(canvasFrame, CFrameWnd)
	//{{AFX_MSG_MAP(canvasFrame)
	ON_WM_TIMER()
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// canvasFrame message handlers

void canvasFrame::OnTimer(UINT nIDEvent) 
{
	CFrameWnd::OnTimer(nIDEvent);
	CClientDC dc(this);
	dc.BitBlt(xlast,ylast,81,81,mdc,0,0,WHITENESS);  //覆盖上次贴图
	dc.BitBlt(x,y,81,81,mdc,0,0,SRCCOPY);  //贴上小球图
	ylast=y;    //记录此次 Y 坐标
	xlast=x;    //记录此次 X 坐标
	if(vy>=0)   //小球下坠
	{
		vy+=g;
		y+=vy;
		if(y+60 >= rect.bottom)  //是否碰到下边缘
		{
			vy = -vy;
			y = rect.bottom -60;
			vy+=ay;        //受到 Y 轴方向的摩擦力
			vx+=ax;        //受到 X 轴方向的摩擦力
			if(vx<=0)
				vx=0;
		}
	}
	else        //小球弹起
	{
		vy+=g;
		if(vy>=0)
			vy=0;
		y+=vy;
	}
	x+=vx;
	if(x==xlast && y==ylast)
		KillTimer(nIDEvent);
}

int canvasFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	SetTimer(1,100,NULL);
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -