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

📄 game2view.cpp

📁 一个简易打砖块游戏
💻 CPP
字号:
// game2View.cpp : implementation of the CGame2View class
//

#include "stdafx.h"
#include "game2.h"

#include "game2Doc.h"
#include "game2View.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define right    1
#define left     2
/////////////////////////////////////////////////////////////////////////////
// CGame2View

IMPLEMENT_DYNCREATE(CGame2View, CView)

BEGIN_MESSAGE_MAP(CGame2View, CView)
	//{{AFX_MSG_MAP(CGame2View)
	ON_WM_TIMER()
	ON_COMMAND(ID_start, Onstart)
	ON_COMMAND(ID_pause, Onpause)
	ON_WM_KEYDOWN()
	ON_COMMAND(ID_st, Onst)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGame2View construction/destruction
#define MAX_NUM 50
struct Adj 
{ int xl;
  int yl;
  int t;
}Adj,Arct[MAX_NUM][MAX_NUM];
CGame2View::CGame2View()
{	
}

CGame2View::~CGame2View()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGame2View drawing

void CGame2View::OnDraw(CDC* pDC)
{
	CGame2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
    pDC->Rectangle(5, 5, 618, 618);
    pDC->Rectangle(8, 8, 615, 615);	
	pDC->TextOut(150,0, "ESC 开始 F1 继续 F2 暂停/继续 <- 左移 -> 右移 ^发射");
	// TODO: add draw code for nativedata here
}

/////////////////////////////////////////////////////////////////////////////
// CGame2View printing

BOOL CGame2View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CGame2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CGame2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CGame2View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CGame2View message handlers

void CGame2View::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC  dc(this);
	CClientDC  dc1(this);
	dc.TextOut(150,0, "ESC 开始 F1 外挂 F2 暂停/继续 <- 左移 -> 右移 ^发射");
	dc.TextOut(260,50, "第一关");
    CPen pen1(PS_SOLID, 1, RGB(255,255,255)),*p;
    p=dc.SelectObject(&pen1);
	dc.Ellipse(x0+r, y0+r, x0-r, y0-r);
	calculte();
	CPen pen(PS_SOLID, 1, RGB(255,0,0)),*poldpen;
    poldpen=dc.SelectObject(&pen);
	dc.Ellipse(x0+r, y0+r, x0-r, y0-r);
    dc.SelectObject(poldpen);
	CBrush brush3(RGB(255,255,255)),*pOldbrush3;
	pOldbrush3=dc.SelectObject(&brush3);
	if(s)
	switch(HeadDirection)
	{	
	case right:
		x+=speed;
		y+=speed;
	    dc.Rectangle(x-speed,400,x,420);
		s=false;
		break;
	case left:
		x-=speed;
		y-=speed;
	    dc.Rectangle(y,400,y+speed,420);
		s=false;
		break;
	}
    dc.SelectObject(pOldbrush3);
    CBrush brush4(RGB(0,255,0)),*pOldbrush4;
	pOldbrush4=dc.SelectObject(&brush4);
	if(x<=10) {x=10; y=x+l;dc.Rectangle(x,400,y,420);}
	if(y>=610){y=610;x=y-l;dc.Rectangle(x,400,y,420);}
	else dc.Rectangle(x,400,y,420);
	CBrush brush2(RGB(0,0,0)),*pOldbrush2;
	pOldbrush2=dc.SelectObject(&brush2);
	for(i=0;i<20;i++)
    for(j=0;j<10;j++)
    if(Arct[i][j].t==0)
    dc.Rectangle(Arct[i][j].xl,Arct[i][j].yl,Arct[i][j].xl+20,Arct[i][j].yl+10);
    dc.SelectObject(&pOldbrush2);
	if(life>0)
	{if(y0>600) continu();}
    CPen pen6(PS_SOLID, 1, RGB(255,0,0)),*poldpen6;
    poldpen6=dc1.SelectObject(&pen6);
	if(life==0) 
	{
	dc1.Rectangle(440, 470, 600, 520);
	dc1.TextOut(445,480, "you lost your three life");}
	dc1.TextOut(450,450, str);
	str.Format("你的得分是:%d分",add);
	if(add==400)
    {dc1.Rectangle(420, 470, 600, 520);
	dc1.TextOut(425,480, "you are tough,you win the game!");}
	CView::OnTimer(nIDEvent);
}

void CGame2View::Onstart() 
{   
	SetTimer(1,0.01,NULL);
	sc=1;
	// TODO: add construction code here
 

	// TODO: Add your command handler code here
	
}

void CGame2View::Onpause() 
{   
	KillTimer(1);
	sc=0;
	
	// TODO: Add your command handler code here
	
}

void CGame2View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC  dc1(this);
	CPen pen7(PS_SOLID, 1, RGB(255,255,255)),*poldpen7;
    poldpen7=dc1.SelectObject(&pen7);
	if(life>0)
	{
	if(VK_RIGHT==nChar)
	{HeadDirection=right; s=true;}
	if(VK_LEFT==nChar)
	{	HeadDirection=left;
	   s=true;}
	if(VK_UP==nChar)
		s1=1;
	}
	if(VK_ESCAPE==nChar)
       Onst();
	if(VK_F1==nChar)
	   {life=3;
    dc1.Rectangle(440, 470, 600, 520);
	dc1.TextOut(445,480, "");}
	 
	if(VK_F2==nChar)
	{
	s3=true;
	if(s3&&sc==1)
	{Onpause();s3=false;}
	if(s3&&sc==0)
    {Onstart(); s3=false;}
	}
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CGame2View::calculte()
{   l=y-x;
    if(s1==1) 
{   vx=2;
    vy=-2;
	s1=2;
}
if(s1==0)
x0=(x+y)/2;
	CClientDC  dc(this);
    CBrush brush2(RGB(255,255,255)),*pOldbrush2;
	pOldbrush2=dc.SelectObject(&brush2);
    CPen pen3(PS_SOLID, 1, RGB(255,255,255)),*poldpen3;
    poldpen3=dc.SelectObject(&pen3);
	x0 = x0 + vx*dt;
	y0 = y0 + vy*dt;
	//////////////////////////////////////////////////////////////////////////
	if((vx<0 && x0-r<=10) ||(vx>0 && x0+r>=610))
		vx = -vx;
	if (vy<0 && y0-r<=10) 
		vy = -vy;
	if(vy>0 && x0>=x&&x0<=y && y0+r==400)
	    vy = -vy;
    if(vy>0 && (x0-x)*(x0-x)+(y0-400)*(y0-400)==r*r)
	    vy = -vy;
    if(vy>0 && (x0-y)*(x0-y)+(y0-400)*(y0-400)==r*r)
		vy = -vy;
	add=0;
	for(i=0;i<20;i++)
    for(j=0;j<10;j++)
	{   add+=Arct[i][j].t;
		if(Arct[i][j].t==0)
	{
		if(vx<0 && x0-r==(Arct[i][j].xl+20)&& y0>Arct[i][j].yl&& y0<(Arct[i][j].yl+20)) 
        {vx = -vx;Arct[i][j].t=2;dc.Rectangle(Arct[i][j].xl,Arct[i][j].yl,Arct[i][j].xl+20,Arct[i][j].yl+10);}
	    if(vx>0 && x0+r==Arct[i][j].xl && y0>Arct[i][j].yl&& y0<(Arct[i][j].yl+20)) 
        {vx = -vx;Arct[i][j].t=2;dc.Rectangle(Arct[i][j].xl,Arct[i][j].yl,Arct[i][j].xl+20,Arct[i][j].yl+10);}
        if(vy<0 && y0-r==(Arct[i][j].yl+20) && x0>Arct[i][j].xl&&x0<(Arct[i][j].xl+20)) 
        {vy = -vy;Arct[i][j].t=2;dc.Rectangle(Arct[i][j].xl,Arct[i][j].yl,Arct[i][j].xl+20,Arct[i][j].yl+10);}
	    if(vy>0 && (y0+r)==Arct[i][j].yl && x0>Arct[i][j].xl&& x0<(Arct[i][j].xl+20))
        {vy = -vy;Arct[i][j].t=2;dc.Rectangle(Arct[i][j].xl,Arct[i][j].yl,Arct[i][j].xl+20,Arct[i][j].yl+10);}
		if(vy>0 && (Arct[i][j].xl-x0)*(Arct[i][j].xl-x0)+(Arct[i][j].yl-y0)*(Arct[i][j].yl-y0)<=r*r)
		{vy = -vy;vx=-vx;Arct[i][j].t=2;dc.Rectangle(Arct[i][j].xl,Arct[i][j].yl,Arct[i][j].xl+20,Arct[i][j].yl+10);}
		if(vy>0 && (Arct[i][j].xl+20-x0)*(Arct[i][j].xl+20-x0)+(Arct[i][j].yl-y0)*(Arct[i][j].yl-y0)<=r*r)
         {vy = -vy;vx=-vx;Arct[i][j].t=2;dc.Rectangle(Arct[i][j].xl,Arct[i][j].yl,Arct[i][j].xl+20,Arct[i][j].yl+10);}
		if(vy<0 && (Arct[i][j].xl-x0)*(Arct[i][j].xl-x0)+(Arct[i][j].yl+10-y0)*(Arct[i][j].yl+10-y0)<=r*r)
         {vy = -vy;vx=-vx;Arct[i][j].t=2;dc.Rectangle(Arct[i][j].xl,Arct[i][j].yl,Arct[i][j].xl+20,Arct[i][j].yl+10);}
		if(vy<0 && (Arct[i][j].xl+20-x0)*(Arct[i][j].xl+20-x0)+(Arct[i][j].yl+10-y0)*(Arct[i][j].yl+10-y0)<=r*r)
         {vy = -vy;vx=-vx;Arct[i][j].t=2;dc.Rectangle(Arct[i][j].xl,Arct[i][j].yl,Arct[i][j].xl+20,Arct[i][j].yl+10);}
	}  
	}
   dc.SelectObject(&pOldbrush2);
   dc.SelectObject(poldpen3);
}  

void CGame2View::intial()
{
for(i=0;i<20;i++)
for(j=0;j<10;j++)
{
Arct[i][j].xl=110+20*i;
Arct[i][j].yl=110+10*j;
Arct[i][j].t=0;
}	
}

void CGame2View::Onst() 
{   SetTimer(1,0.01,NULL);
    x=250;
    y=360;
	// TODO: add construction code here
    speed=15;
    s=true;
    x0=305;
    r=7;
    y0=400-r;
    dt=1;
    vx=0;
    vy=0;
    s1=0;
	sc=1;
	life=3;
	add=0;
    intial();
	// TODO: Add your command handler code here
	
}



void CGame2View::continu()
{
    CClientDC  dc(this);
	SetTimer(1,0.01,NULL);
    y=360;
	x=270;
	// TODO: add construction code here
    speed=15;
    s=true;
    x0=305;
    r=7;
    y0=400-r;
    dt=1;
    vx=0;
    vy=0;
    s1=0;
	add=0;
	sc=1;
	life--;
    CBrush brush6(RGB(255,255,255)),*pOldbrush6;
	pOldbrush6=dc.SelectObject(&brush6);
	CPen pen4(PS_SOLID, 1, RGB(255,255,255)),*poldpen4;
    poldpen4=dc.SelectObject(&pen4);
	dc.Rectangle(10,300,610,420);
    dc.SelectObject(&pOldbrush6);
    dc.SelectObject(&poldpen4);
}

⌨️ 快捷键说明

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