#include "CAlien.h"
//the vertices making up its shape
const int NumAlienVerts = 19;
const SPoint Alien[NumAlienVerts] = { SPoint(1,3),
SPoint(4,1),
SPoint(4,-1),
SPoint(2,-4),
SPoint(1,-1),
SPoint(0,-2),
SPoint(-1,-1),
SPoint(-2,-4),
SPoint(-4,-1),
SPoint(-4,1),
SPoint(-1,3),
SPoint(-2,1),
SPoint(-1.5,0.5),
SPoint(-2,0),
SPoint(-2.5,1),
SPoint(2,1),
SPoint(1.5,0.5),
SPoint(2,0),
SPoint(2.5,1)};
//--------------------------------- ctor ---------------------------------
//
//------------------------------------------------------------------------
CAlien::CAlien(): m_dScale(CParams::dAlienScale),
m_vVelocity(SVector2D(0, 0)),
m_dMass(CParams::dAlienMass),
m_iAge(0),
m_bWarning(false)
{
//set its position
m_vPos = SVector2D(RandInt(0, CParams::WindowWidth), CParams::WindowHeight);
//create the vertex buffer
for (int i=0; i<NumAlienVerts; ++i)
{
m_vecAlienVB.push_back(Alien[i]);
}
//setup its bounding box
m_AlienBBox.left = m_vPos.x - (4*CParams::dAlienScale);
m_AlienBBox.right = m_vPos.x + (4*CParams::dAlienScale);
m_AlienBBox.top = m_vPos.y + (3*CParams::dAlienScale);
m_AlienBBox.bottom= m_vPos.y - (4*CParams::dAlienScale);
}
//------------------------------ Render ----------------------------------
void CAlien::Render(HDC &surface, HPEN &GreenPen, HPEN &RedPen)
{
//transform the vertices
WorldTransform();
//select in green pen
HPEN OldPen = (HPEN)SelectObject(surface, GreenPen);
//draw body
MoveToEx(surface, m_vecAlienVBTrans[0].x, m_vecAlienVBTrans[0].y, NULL);
for (int vtx=0; vtx<11; ++vtx)
{
LineTo(surface, m_vecAlienVBTrans[vtx].x, m_vecAlienVBTrans[vtx].y);
}
LineTo(surface, m_vecAlienVBTrans[0].x, m_vecAlienVBTrans[0].y);
//select in red pen
SelectObject(surface, RedPen);
//left eye
MoveToEx(surface, m_vecAlienVBTrans[11].x, m_vecAlienVBTrans[11].y, NULL);
for (vtx=12; vtx<15; ++vtx)
{
LineTo(surface, m_vecAlienVBTrans[vtx].x, m_vecAlienVBTrans[vtx].y);
}
//right eye
MoveToEx(surface, m_vecAlienVBTrans[15].x, m_vecAlienVBTrans[15].y, NULL);
for (vtx=16; vtx<19; ++vtx)
{
LineTo(surface, m_vecAlienVBTrans[vtx].x, m_vecAlienVBTrans[vtx].y);
}
//replace the old pen
SelectObject(surface, OldPen);
//display warning if a problem with the network
if (m_bWarning)
{
string s = "Wrong amount of inputs!";
TextOut(surface, 110, 200, s.c_str(), s.size());
}
}
//---------------------WorldTransform--------------------------------
//
// sets up the translation matrices for the ship and applies the
// world transform to the ships vertex buffer
//-------------------------------------------------------------------
void CAlien::WorldTransform()
{
//copy the original vertices into the buffer about to be transformed
m_vecAlienVBTrans = m_vecAlienVB;
//create a transformation matrix
C2DMatrix matTransform;
//scale
matTransform.Scale(m_dScale, m_dScale);
//and translate
matTransform.Translate(m_vPos.x, m_vPos.y);
//now transform the ships vertices
matTransform.TransformSPoints(m_vecAlienVBTrans);
}
//----------------------------- GetActionFromNetwork ---------------------
//
// this function updates the neural net and returns the action the
// network selects as its output
//------------------------------------------------------------------------
action_type CAlien::GetActionFromNetwork(const vector<CBullet> &bullets,
const SVector2D &GunPos)
{
//the inputs into the net
vector<double> NetInputs;
//This will hold the outputs from the neural net
static vector<double> outputs(0,3);
//add in the vector to the gun turret
int XComponentToTurret = GunPos.x - m_vPos.x;
int YComponentToTurret = GunPos.y - m_vPos.y;
NetInputs.push_back(XComponentToTurret);
NetInputs.push_back(YComponentToTurret);
//now any bullets
for (int blt=0; blt<bullets.size(); ++blt)
{
if (bullets[blt].Active())
{
double xComponent = bullets[blt].Pos().x - m_vPos.x;
double yComponent = bullets[blt].Pos().y - m_vPos.y;
NetInputs.push_back(xComponent);
NetInputs.push_back(yComponent);
}
else
{
//if a bullet is innactive just input the vector to
//the gun turret
NetInputs.push_back(XComponentToTurret);
NetInputs.push_back(YComponentToTurret);
}
}
//feed the inputs into the net and get the outputs
outputs = m_ItsBrain.Update(NetInputs);
//this is set if there is a problem with the update
if (outputs.size() == 0)
{
m_bWarning = true;
}
//determine which action is valid this frame. The highest valued
//output over 0.9. If none are over 0.9 then just drift with
//gravity
double BiggestSoFar = 0;
action_type action = drift;
for (int i=0; i<outputs.size(); ++i)
{
if( (outputs[i] > BiggestSoFar) && (outputs[i] > 0.9))
{
action = (action_type)i;
BiggestSoFar = outputs[i];
}
}
return action;
}
//----------------------------- Update -----------------------------------
//
// Checks for user keypresses and updates the aliens parameters accordingly
//------------------------------------------------------------------------
bool CAlien::Update(vector<CBullet> &bullets,const SVector2D &GunPos)
{
//update age
++m_iAge;
//get the next action from the neural network
int action = GetActionFromNetwork(bullets, GunPos);
//switch on the action
switch(action)
{
case thrust_left:
{
m_vVelocity.x -= CParams::dMaxThrustLateral/m_dMass;
}
break;
case thrust_right:
{
m_vVelocity.x += CParams::dMaxThrustLateral/m_dMass;
}
break;
case thrust_up:
{
m_vVelocity.y += CParams::dMaxThrustVertical/m_dMass;
}
break;
default:break;
}
//add in gravity
SVector2D gravity(0, CParams::dGravityPerTick);
m_vVelocity += gravity;
//clamp the velocity of the alien
Clamp(m_vVelocity.x, -CParams::dMaxVelocity, CParams::dMaxVelocity);
Clamp(m_vVelocity.y, -CParams::dMaxVelocity, CParams::dMaxVelocity);
//update the alien's position
m_vPos += m_vVelocity;
//wrap around window width
if (m_vPos.x < 0)
{
m_vPos.x = CParams::WindowWidth;
}
if (m_vPos.x > CParams::WindowWidth)
{
m_vPos.x = 0;
}
//update the bounding box
m_AlienBBox.left = m_vPos.x - (4*CParams::dAlienScale);
m_AlienBBox.right = m_vPos.x + (4*CParams::dAlienScale);
m_AlienBBox.top = m_vPos.y + (3*CParams::dAlienScale);
m_AlienBBox.bottom= m_vPos.y - (4*CParams::dAlienScale);
//an alien dies if it drops below the gun, flys too high
//or is hit by a bullet
if ( (m_vPos.y > (CParams::WindowHeight + 5)) ||
(m_vPos.y < 15) ||
CheckForCollision(bullets))
{
return false;
}
return true;
}
//------------------------- CheckFo
没有合适的资源?快使用搜索试试~ 我知道了~
使用C++制作戰車射擊-100%提供源码
共54个文件
h:12个
cpp:10个
sbr:9个
4星 · 超过85%的资源 需积分: 3 24 下载量 52 浏览量
2010-04-19
12:56:47
上传
评论
收藏 11.12MB RAR 举报
温馨提示
使用C++制作战车射击-100%提供源码 以及可执行的源码, 让你回到任天堂的时代 以下的资源也很不错, 加减可以看一下o 使用C++制作3D动画人物-100%提供源码 http://download.csdn.net/source/2255453 Linux kernel 每一行都完全注释-初学者必备 http://download.csdn.net/source/1982431 Programming Embedded Systems 2nd http://download.csdn.net/source/1982338 J2ME pdf and code 郭克华 http://download.csdn.net/source/2229809 C语言程序设计(Visual+C 6.0环境) http://download.csdn.net/source/2232878 Visual C++ 60 MFC + code 学习最强宝典 http://download.csdn.net/source/2236266 ASP.NET Web (第一次亲密接触ASP.NET) http://download.csdn.net/source/2241466 http://hqioan.download.csdn.net/
资源推荐
资源详情
资源评论
收起资源包目录
战车射击.rar (54个子文件)
main.cpp 6KB
params.ini 287B
resource.h 456B
CTimer.cpp 3KB
BrainyAliens.ncb 353KB
CBullet.h 1KB
CTimer.h 969B
CParams.h 3KB
BrainyAliens.opt 52KB
CGun.h 2KB
CAlien.h 3KB
utils.h 2KB
SVector2D.h 2KB
CBullet.cpp 3KB
BrainyAliens.dsp 5KB
CNeuralNet.cpp 7KB
CController.cpp 8KB
CGun.cpp 5KB
CAlien.cpp 10KB
Script1.aps 17KB
CNeuralNet.h 2KB
Release
CParams.sbr 0B
BrainyAliens.pch 7.79MB
CController.sbr 0B
CParams.obj 101KB
CTimer.obj 3KB
vc60.idb 65KB
CGun.obj 18KB
CAlien.sbr 0B
CAlien.obj 26KB
CNeuralNet.sbr 0B
main.obj 12KB
C2DMatrix.sbr 0B
C2DMatrix.obj 6KB
BrainyAliens.exe 220KB
main.sbr 0B
BrainyAliens.bsc 2.27MB
CController.obj 101KB
CBullet.obj 12KB
CTimer.sbr 0B
CGun.sbr 0B
CBullet.sbr 0B
Script1.res 412B
CNeuralNet.obj 26KB
C2DMatrix.h 3KB
CParams.cpp 3KB
defines.h 271B
BrainyAliens.plg 3KB
C2DMatrix.cpp 1KB
BrainyAliens.dsw 549B
Script1.rc 2KB
CController.h 2KB
utils.cpp 10B
icon1.ico 318B
共 54 条
- 1
资源评论
- sunhuiyuan12012-06-29还可以,感觉界面少了点
- 天小明2014-10-13不错不错,功能还是没完善
- ForcaFCB2015-11-11还行吧,觉得挺好的,有些看不懂
76587309834890
- 粉丝: 40
- 资源: 230
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功