📄 tanksview.cpp
字号:
//Tanks
//Copyright John Manslow
//29/09/2001
// TanksView.cpp : implementation of the CTanksView class
//
#include "stdafx.h"
#include "Tanks.h"
#include "TanksDoc.h"
#include "TanksView.h"
#include "CWorld.h"
#include "CProjectile.h"
#include "CTank.h"
#include "CMLP.h"
#include "CConditionalDistribution.h"
#include "CUnconditionalDistribution.h"
#include "math.h"
#include "fstream.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTanksView
//Too many of these warnings for them to be useful in this file!
#pragma warning(disable:4244)
IMPLEMENT_DYNCREATE(CTanksView, CView)
BEGIN_MESSAGE_MAP(CTanksView, CView)
//{{AFX_MSG_MAP(CTanksView)
ON_WM_TIMER()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//See TanksDoc for explanations of these
extern BOOL boGeneratingErrorTrainingData;
extern CWorld *pWorld;
extern CMLP *pMLP;
extern CConditionalDistribution *pErrorDistribution;
extern CUnconditionalDistribution *pUnconditionalErrorDistribution;
extern double *pdMax,*pdMin;
extern double dErrorMin,dErrorMax;
extern double *pdErrorInputMin,*pdErrorInputMax;
/////////////////////////////////////////////////////////////////////////////
// CTanksView construction/destruction
CTanksView::CTanksView()
{
}
CTanksView::~CTanksView()
{
}
BOOL CTanksView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTanksView drawing
void CTanksView::OnDraw(CDC* pDC)
{
CTanksDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//Get the size of the window. Useful for drawing the terrain at the bottom of the window and
//centreing the wind speed arrow in it.
CRect rect;
GetClientRect(&rect);
int x = (rect.Width());
int y = (rect.Height());
//Draw the terrain at the bottom of the window.
unsigned long i;
for(i=0;i<pWorld->ulTerrainResolution;i++)
{
pDC->MoveTo(i,y);
pDC->LineTo(i,y-pWorld->plHeight[i]);
}
//The sign variable is used to reverse the direction of the wind speed arrow
//for "negative" wind speeds - i.e. winds blowing from right to left.
double dSign=1.0;
if(pWorld->dWindSpeed<0)
{
dSign=-1;
}
//Draw the centre-aligned wind speed arrow with a length proportional to the
//wind speed
pDC->Rectangle(0.5*x,50,15*dSign+0.5*x+100.0*pWorld->dWindSpeed,51);
pDC->MoveTo(0.5*x,50);
pDC->LineTo(0.5*x-10*dSign,55);
pDC->MoveTo(0.5*x,50);
pDC->LineTo(0.5*x-10*dSign,45);
pDC->MoveTo(0.5*x+5*dSign+00.0*pWorld->dWindSpeed,50);
pDC->LineTo(0.5*x-5*dSign+00.0*pWorld->dWindSpeed,55);
pDC->MoveTo(0.5*x+5*dSign+00.0*pWorld->dWindSpeed,50);
pDC->LineTo(0.5*x-5*dSign+00.0*pWorld->dWindSpeed,45);
pDC->MoveTo(15*dSign+0.5*x-0*dSign+100.0*pWorld->dWindSpeed,50);
pDC->LineTo(15*dSign+0.5*x-10*dSign+100.0*pWorld->dWindSpeed,55);
pDC->MoveTo(15*dSign+0.5*x-0*dSign+100.0*pWorld->dWindSpeed,50);
pDC->LineTo(15*dSign+0.5*x-10*dSign+100.0*pWorld->dWindSpeed,45);
//If a projectile exists, draw it as a rectangle
if(pWorld->pProjectile!=NULL)
pDC->Rectangle(pWorld->pProjectile->dxPosition-4,y-pWorld->pProjectile->dyPosition-2,
pWorld->pProjectile->dxPosition,y-pWorld->pProjectile->dyPosition+2);
//All shapes drawn after this point are filled black
pDC->SelectStockObject(BLACK_BRUSH);
//Define the exact position for the player's tank graphic.
long tx=long(pWorld->ppPlayer[0]->dxPosition-10.0);
long ty=long(y-pWorld->ppPlayer[0]->dyPosition+1.0);
//Can be used to scale the tank graphics. Should be left at 4.0 since this
//is the length for which collision detection is calibrated.
double dScale=4.0;
//Draw the tank's wheels
pDC->Ellipse(tx,ty,dScale+tx,-dScale+ty);
pDC->Ellipse(dScale+tx,ty,2*dScale+tx,-dScale+ty);
pDC->Ellipse(2*dScale+tx,ty,3*dScale+tx,-dScale+ty);
pDC->Ellipse(3*dScale+tx,ty,4*dScale+tx,-dScale+ty);
pDC->Ellipse(4*dScale+tx,-0.25*dScale+ty,5*dScale+tx,-1.25*dScale+ty);
//Draw its hull
pDC->Rectangle(tx,-dScale+ty,4.5*dScale+tx,-1.5*dScale+ty);
pDC->Rectangle(tx,-1.5*dScale+ty,2*dScale+tx,-2.5*dScale+ty);
CPoint Poly[3];
Poly[0].x=tx+2*dScale;
Poly[0].y=ty-1.5*dScale;
Poly[1].x=tx+2.5*dScale;
Poly[1].y=ty-1.5*dScale;
Poly[2].x=tx+2*dScale;
Poly[2].y=ty-2.5*dScale;
pDC->Polygon(Poly,3);
//Draw its barrel at the correct angle
CPoint Barrel[4];
Barrel[0].x=tx+2.0*dScale-0.1*pWorld->ppPlayer[0]->dBarrely*dScale;;
Barrel[0].y=ty-2.0*dScale-0.1*pWorld->ppPlayer[0]->dBarrelx*dScale;
Barrel[1].x=tx+2.0*dScale+0.1*pWorld->ppPlayer[0]->dBarrely*dScale;;
Barrel[1].y=ty-2.0*dScale+0.1*pWorld->ppPlayer[0]->dBarrelx*dScale;
Barrel[2].x=tx+2.0*dScale+3.9*pWorld->ppPlayer[0]->dBarrelx*dScale;;
Barrel[2].y=ty-2.0*dScale-4.1*pWorld->ppPlayer[0]->dBarrely*dScale;
Barrel[3].x=tx+2.0*dScale+4.1*pWorld->ppPlayer[0]->dBarrelx*dScale;;
Barrel[3].y=ty-2.0*dScale-3.9*pWorld->ppPlayer[0]->dBarrely*dScale;
pDC->Polygon(Barrel,4);
//Define exact position of AI tank
tx=pWorld->ppPlayer[1]->dxPosition+10;
ty=y-pWorld->ppPlayer[1]->dyPosition+1;
//Render it
pDC->Ellipse(tx,ty,-dScale+tx,-dScale+ty);
pDC->Ellipse(-dScale+tx,ty,-2*dScale+tx,-dScale+ty);
pDC->Ellipse(-2*dScale+tx,ty,-3*dScale+tx,-dScale+ty);
pDC->Ellipse(-3*dScale+tx,ty,-4*dScale+tx,-dScale+ty);
pDC->Ellipse(-4*dScale+tx,-0.25*dScale+ty,-5*dScale+tx,-1.25*dScale+ty);
pDC->Rectangle(tx,-dScale+ty,-4.5*dScale+tx,-1.5*dScale+ty);
pDC->Rectangle(tx,-1.5*dScale+ty,-2*dScale+tx,-2.5*dScale+ty);
Poly[0].x=tx-2*dScale;
Poly[0].y=ty-1.5*dScale;
Poly[1].x=tx-2.5*dScale;
Poly[1].y=ty-1.5*dScale;
Poly[2].x=tx-2*dScale;
Poly[2].y=ty-2.5*dScale;
pDC->Polygon(Poly,3);
Barrel[0].x=tx-2.0*dScale+0.1*pWorld->ppPlayer[1]->dBarrely*dScale;;
Barrel[0].y=ty-2.0*dScale-0.1*pWorld->ppPlayer[1]->dBarrelx*dScale;
Barrel[1].x=tx-2.0*dScale-0.1*pWorld->ppPlayer[1]->dBarrely*dScale;;
Barrel[1].y=ty-2.0*dScale+0.1*pWorld->ppPlayer[1]->dBarrelx*dScale;
Barrel[2].x=tx-2.0*dScale+3.9*pWorld->ppPlayer[1]->dBarrelx*dScale;;
Barrel[2].y=ty-2.0*dScale-4.1*pWorld->ppPlayer[1]->dBarrely*dScale;
Barrel[3].x=tx-2.0*dScale+4.1*pWorld->ppPlayer[1]->dBarrelx*dScale;;
Barrel[3].y=ty-2.0*dScale-3.9*pWorld->ppPlayer[1]->dBarrely*dScale;
pDC->Polygon(Barrel,4);
//Prepare storage for scores
char *pString=new char[100];
//Dispplay the player's score in the top left of the window.
sprintf(pString,"%0.0lf",pWorld->dScore[0]);
pDC->TextOut(25,20,pString);
//Display the AI's score in the top right.
sprintf(pString,"%3.0lf",pWorld->dScore[1]);
pDC->TextOut(715,20,pString);
//Clean up.
delete []pString;
//Set the timer to generate ticks every 10 ms or so to update the game world
SetTimer(10,10,NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CTanksView diagnostics
#ifdef _DEBUG
void CTanksView::AssertValid() const
{
CView::AssertValid();
}
void CTanksView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -