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

📄 programview.cpp

📁 飞行棋游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// programView.cpp : implementation of the CProgramView class
//

#include "stdafx.h"
#include "program.h"

#include "programDoc.h"
#include "programView.h"
#include "Chess.h"
#include "Managers.h"
#include "stdlib.h"


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

/////////////////////////////////////////////////////////////////////////////
// CProgramView

IMPLEMENT_DYNCREATE(CProgramView, CView)

BEGIN_MESSAGE_MAP(CProgramView, CView)
	//{{AFX_MSG_MAP(CProgramView)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_TIMER()
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()



extern	Managers	manager;
/////////////////////////////////////////////////////////////////////////////
// CProgramView construction/destruction

CProgramView::CProgramView()
{
	// TODO: add construction code here

	plthis=manager.Planes;//将plthis指向manager.Planes,减少部分代码
	PlaneUpdata();//对lastplane[16]进行初始化
	control=0;//对control进行初始化,0表示此时骰子无法点击
	ischange = manager.Game_State;
	isOntimer = false;
}

CProgramView::~CProgramView()
{
}

BOOL CProgramView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	//加载背景位图
	m_background.LoadBitmap(IDB_BACKGROUND);
	//加载飞机位图
	m_greenplane.LoadBitmap(IDB_GREENPLANE);
	m_greenmask.LoadBitmap(IDB_GREENMASK);
	m_redplane.LoadBitmap(IDB_REDPLANE);
	m_redmask.LoadBitmap(IDB_REDMASK);
	m_yellowplane.LoadBitmap(IDB_YELLOWPLANE);
	m_yellowmask.LoadBitmap(IDB_YELLOWMASK);
	m_blueplane.LoadBitmap(IDB_BLUEPLANE);
	m_bluemask.LoadBitmap(IDB_BLUEMASK);
	//加载骰子位图
	m_roll.LoadBitmap(IDB_ROLL);
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CProgramView drawing

void CProgramView::OnDraw(CDC* pDC)
{
	CProgramDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	//对绘图CDC进行初始化
	static	int	i=1;
	if(i==1)
	{
		//背景CDC
		Background.CreateCompatibleDC (pDC);
		Background.SelectObject(m_background);
		//飞机CDC
		Greenplane.CreateCompatibleDC (pDC);
		Greenplane.SelectObject(m_greenplane);
		Redplane.CreateCompatibleDC (pDC);
		Redplane.SelectObject(m_redplane);
		Yellowplane.CreateCompatibleDC (pDC);
		Yellowplane.SelectObject(m_yellowplane);
		Blueplane.CreateCompatibleDC (pDC);
		Blueplane.SelectObject(m_blueplane);
		Greenmask.CreateCompatibleDC (pDC);
		Greenmask.SelectObject(m_greenmask);
		Redmask.CreateCompatibleDC (pDC);
		Redmask.SelectObject(m_redmask);
		Yellowmask.CreateCompatibleDC (pDC);
		Yellowmask.SelectObject(m_yellowmask);
		Bluemask.CreateCompatibleDC (pDC);
		Bluemask.SelectObject(m_bluemask);
		//骰子CDC
		Num.CreateCompatibleDC(pDC);
		Num.SelectObject(m_roll);
		//保证初始化只进行一次
		i++;
	}
/*	lastplane[0].state=2;
//	lastplane[0].step=4;
	int time1 = clock();
	int time2(0);
	int k(0);
	while(1)
	{	
		time2=clock();
		if(time2-time1>1000)
		{
			lastplane[0].step=k;	
	//		Drawplane(lastplane[0],pDC);
			PrintAll(pDC);
			time1=clock();
			if(k++ == 12)
				break;
		}
	
	}
	*/

	m_cflash.GotoFrame(m_cflash.GetFrameNum());
	if(ischange != manager.Game_State)
	{
		m_cflash.Play();
	}	

	PrintAll(pDC);//将16架飞机全部画一次
	PrintState(pDC);//打印此时的游戏状态
}

/////////////////////////////////////////////////////////////////////////////
// CProgramView diagnostics

#ifdef _DEBUG
void CProgramView::AssertValid() const
{
	CView::AssertValid();
}

void CProgramView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CProgramDoc* CProgramView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CProgramDoc)));
	return (CProgramDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CProgramView message handlers

void CProgramView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//进行光标位置的跟踪
	int	x=point.x;
	int	y=point.y;
	CString	st;
	st.Format("%d , %d  ",x,y);
	CDC	*pdc=GetDC();
	pdc->TextOut(40,10,st);
	ReleaseDC(pdc);
	CView::OnMouseMove(nFlags, point);

}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:Drawplane(Chess	plane,CDC *pdc)
* 函数介绍:画指定格子的飞机
* 输入参数:飞机所在的格子数step,画图传入的CDC指针pdc
* 输出参数:无
* 返回值  :无
*/
void CProgramView::Drawplane(Chess	plane,CDC *pdc)
{	

//记录最终画飞机的坐标
	int	x;
	int	y;

//当飞机处于状态2,即飞行状态时
	if(plane.state==2)
	{
		//第一分区的格子(左下),为0~12格
		if(plane.step/13==0)
		{	
		//第0~3格,记录当前坐标
			if(plane.step/4==0)
			{	
				x=215+plane.dou;
				y=497-plane.step*31;
			}
		//第4~7格,记录当前坐标
			else	if(plane.step/4==1)
			{
				x=190+plane.dou-(plane.step-4)*31;
				y=385;
			}
		//第8~12格,记录当前坐标
			else
			{
				x=90+plane.dou;
				y=350-(plane.step-8)*31;
			}
		}
		//第二分区的格子(左上),为13~25格
		else if(plane.step/13==1)
		{
			plane.step-=13;	//做相对变换,方便同第一区最相同的处理
			//第13~16格,可以同第一区的第0~3格做内似处理
			if(plane.step/4==0)
			{
				x=100+plane.dou+plane.step*31;
				y=190;
			}
			//第17~20格,可以同第一区的第4~6格做内似处理
			else	if(plane.step/4==1)
			{
				x=210+plane.dou;
				y=172-(plane.step-4)*31;
			}
			//第21~25格,可以同第一区的第8~12格做内似处理
			else
			{
				x=250+plane.dou+(plane.step-8)*31;
				y=65;
			}
		}
		//第三分区的格子(右上),为26~38格
		else if(plane.step/13==2)
		{
			plane.step-=26;	//做相对变换,方便同第一区最相同的处理
			//第26~29格
			if(plane.step/4==0)
			{
				x=405+plane.dou;
				y=82+plane.step*31;
			}
			//第30~33格
			else	if(plane.step/4==1)
			{
				x=425+plane.dou+(plane.step-4)*31;
				y=190;
			}
			//第34~38格
			else
			{
				x=530+plane.dou;
				y=225+(plane.step-8)*31;
			}
		}
		//第四分区的格子(右下),为39~51格
		else if(plane.step/13==3)
		{
			plane.step-=39;	//做相对变换,方便同第一区最相同的处理
			//第39~42格
			if(plane.step/4==0)
			{
				x=520+plane.dou-plane.step*31;
				y=385;
			}
			//第43~46格
			else	if(plane.step/4==1)
			{
				x=405+plane.dou;
				y=405+(plane.step-4)*31;
			}
			//第47~51格
			else
			{
				x=370+plane.dou-(plane.step-8)*31;
				y=505;
			}
		}

	}
//飞机进入状态1,即准备起飞状态
	else if(plane.state==1)
	{
		if(plane.step==51)//表示此时的plane为绿色飞机
		{
			x=200+plane.dou;
			y=516;
		}
		else	if(plane.step==12)//表示此时的plane为红色飞机
		{
			x=86+plane.dou;
			y=174;
		}
		else	if(plane.step==25)//表示此时的plane为黄色飞机
		{
			x=420+plane.dou;
			y=55;
		}
		else	if(plane.step==38)//表示此时的plane为蓝色飞机
		{
			x=540+plane.dou;
			y=400;
		}
	}
//飞机进入状态3,即准备降落状态
	else if(plane.state==3)
	{
		
		if(plane.step>=11&&plane.step<=15)//表示飞机处于红色跑道
		{
			x=130+plane.dou+(plane.step-11)*32;
			y=290;
		}
		else	if(plane.step>=24&&plane.step<=28)//表示飞机处于黄色跑道
		{	x=310+plane.dou;
			y=100+(plane.step-24)*32;
		}
		else	if(plane.step>=37&&plane.step<=41)//表示飞机处于蓝色跑道
		{
			x=490+plane.dou-(plane.step-37)*32;
			y=290;
		}
		else	if(plane.step>=50&&plane.step<=54)//表示飞机处于绿色跑道
		{	
			x=310+plane.dou;
			y=465-(plane.step-50)*32;
		}
	
	}
//飞机处于状态4,即已经降落
	else if(plane.state==4)
	{
		if(plane.Color==0)//如果是绿色飞机降落
			{
				x=310+plane.dou;
				y=310;
			}
		else	if(plane.Color==1)//如果是红色飞机降落
			{
				x=285;
				y=280;
			}
		else	if(plane.Color==2)//如果是黄色飞机降落
			{
				x=310;
				y=260;
			}
		else	if(plane.Color==3)//如果是蓝色飞机降落
			{
				x=335;
				y=285;
			}
	}
//对飞机的颜色进行判断,画出相应的图形
	switch(plane.Color)
	{
		//飞机为绿色飞机
		case	0:	pdc->BitBlt(x,y,41,35,&Greenmask,0,0,MERGEPAINT);
					pdc->BitBlt(x,y,41,35,&Greenplane,0,0,SRCAND);
					break;
		//飞机为红色飞机
		case	1:	pdc->BitBlt(x,y,41,35,&Redmask,0,0,MERGEPAINT);
					pdc->BitBlt(x,y,41,35,&Redplane,0,0,SRCAND);
					break;
		//飞机为黄色飞机
		case	2:	pdc->BitBlt(x,y,41,35,&Yellowmask,0,0,MERGEPAINT);
					pdc->BitBlt(x,y,41,35,&Yellowplane,0,0,SRCAND);
					break;
		//飞机为蓝色飞机
		case	3:	pdc->BitBlt(x,y,41,35,&Bluemask,0,0,MERGEPAINT);
					pdc->BitBlt(x,y,41,35,&Blueplane,0,0,SRCAND);
					break;	
	}
	

}

void CProgramView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default


	//获取当前光标的坐标
	int	mx=point.x;
	int	my=point.y;
	//创建pdc用于后面的绘图
	CDC	*pdc=GetDC();
	//如果用户点击的是开始按钮的时候
	if(mx>=660&&my>=260&&mx<=776&&my<=353)
	{
		int k;//用来记录用户的点击情况
		if(manager.Game_State!=8&&manager.Game_State!=-1)//如果此时仍在游戏中
			k=MessageBox( "您的游戏没有结束,是否重新开始?","飞行棋",MB_OKCANCEL);
		if(k!=IDCANCEL)//k值不为IDCANCEL,即包含值为IDOK和并没有出现对话况两
		{			   //种情况
			manager.GameStart();//开始游戏
			PlaneUpdata();//更新lastplane[16]
			PrintAll(pdc);//重画16架飞机
			PrintState(pdc);
			control=1;//control置为非0值
		}
	}
	//如果用户点击的是退出按钮的时候
	else if(mx>=647&&my>=407&&mx<=778&&my<=464)
	{
		int k;//用来记录用户的点击情况
		if(manager.Game_State!=8&&manager.Game_State!=-1)//如果此时仍游戏中
			k=MessageBox( "您的游戏没有结束,是否退出?","飞行棋",MB_OKCANCEL);
		if(k==IDOK)//用户点击确定按钮
		{
			exit(0);//推出程序
		}
	}
	//如果用户点的是骰子的时候
//	const char *k;
//	k = new char [100];
	//if(m_cflash.GetVariable(k) == 0);
	//	MessageBox("k=0");
/*	if(mx>=650&&my>=50&&mx<=770&&my<=168)
	{
		if(control!=0)//判断此时点击骰子时候有效
		{	
			roll=Roll();//产生0~6随机数
		static bool on = false;
			if(on)
			{
				on = false;
				m_cflash.StopPlay();
				char str[20];
				sprintf(str, "%c", m_cflash.num[(int)m_cflash.GetFrameNum()]);
				roll = atoi(str);
			}
			else
			{
				on = true;
				m_cflash.Play();

			}
			PrintState(pdc);//打印当前游戏状态			
			control=0;//将control置0
			if(manager.DoMsg(1,1,3,roll))//如果此时DoMsg返回为TRUE的时候,即需
			{							 //要发生改变的时候
				control++;//control++,则不再0
				PrintState(pdc);//打印当前游戏状态
			}

		}
		
	}*/

//如果用户点的区域不为以上任何一个区域的时候
	if(!isOntimer && control==0)//首先判断该次点击是否有效
	{
		int	i;
		for(i=0;i<=15;i++)//遍历16架飞机
		{
			if(SelectPlane(plthis[i],mx,my))//如果此时点击的为1~4状态时的飞机
			{
				//如果此时点击的飞机需要发生改变时
				if(manager.DoMsg(plthis[i].step,plthis[i].state,1,roll))
				{			
//					PlaneUpdata();
//					PrintAll(pdc);
//					PrintState(pdc);
			
					//timer=plthis[i].step-lastplane[i].step;
					//if(timer<0)
					//	timer = timer + 52;
					if(manager.PlLast.state!=0 && manager.PlThis->state!=0 && manager.PlThis->state!=4)
					{
						timer = roll;
						SetTimer(1,500,NULL);
						isOntimer = true;
					}
					planenum=i;
					control++;
					break;

⌨️ 快捷键说明

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