人工智能实验报告.docx
《人工智能实验报告.docx》由会员分享,可在线阅读,更多相关《人工智能实验报告.docx(10页珍藏版)》请在冰豆网上搜索。
![人工智能实验报告.docx](https://file1.bdocx.com/fileroot1/2023-7/9/37215674-6385-48b9-a0a4-7bf0065d284e/37215674-6385-48b9-a0a4-7bf0065d284e1.gif)
人工智能实验报告
人工智能实验报告
班级:
数媒081
学号:
108028017
姓名:
徐浩
2010-2011学年第1学期
实验名称实验一XNA编程基础
课时安排7课时
实验要求
掌握XNA游戏基本运行框架,二维纹理绘制,三维模型绘制的代码实现方法。
实验核心代码:
protectedoverridevoidInitialize()
{
base.Initialize();
Viewportvp=graphics.GraphicsDevice.Viewport;//窗口大小规定
spotlightPosition.X=vp.X+vp.Width/2;
spotlightPosition.Y=vp.Y+vp.Height/2;//光的位置设定
catPosition.X=vp.X+vp.Width/4;
catPosition.Y=vp.Y+vp.Height/2;
}
protectedoverridevoidUpdate(GameTimegameTime)
{
HandleInput();
//clampthecat'spositionsothatitstaysonthescreen.
Viewportvp=graphics.GraphicsDevice.Viewport;
catPosition.X=MathHelper.Clamp(catPosition.X,vp.X,vp.X+vp.Width);
catPosition.Y=MathHelper.Clamp(catPosition.Y,vp.Y,vp.Y+vp.Height);
//usetheTurnToFacefunctiontoupdatethespotlightAngletoface
//towardsthecat.
spotlightAngle=TurnToFace(spotlightPosition,catPosition,spotlightAngle,
SpotlightTurnSpeed);
base.Update(gameTime);
}
privatestaticfloatTurnToFace(Vector2position,Vector2faceThis,
floatcurrentAngle,floatturnSpeed)
{
//considerthisdiagram:
//C
///|
///|
///|y
///o|
//S--------
//x
//
//whereSisthepositionofthespotlight,Cisthepositionofthecat,
//and"o"istheanglethatthespotlightshouldbefacinginorderto
//pointatthecat.weneedtoknowwhatois.usingtrig,weknowthat
//tan(theta)=opposite/adjacent
//tan(o)=y/x
//ifwetakethearctanofbothsidesofthisequation...
//arctan(tan(o))=arctan(y/x)
//o=arctan(y/x)
//so,wecanusexandytofindo,our"desiredAngle."
//xandyarejustthedifferencesinpositionbetweenthetwoobjects.
floatx=faceThis.X-position.X;
floaty=faceThis.Y-position.Y;
floatdesiredAngle=(float)Math.Atan2(y,x);
floatdifference=WrapAngle(desiredAngle-currentAngle);
//clampthatbetween-turnSpeedandturnSpeed.
difference=MathHelper.Clamp(difference,-turnSpeed,turnSpeed);
//so,theclosestwecangettoourtargetiscurrentAngle+difference.
//returnthat,usingWrapAngleagain.
returnWrapAngle(currentAngle+difference);
}
实验心得:
通过这个实验初步了解了xna的几个主要函数的功能,比如Update,Draw函数。
本实验中采取了三角函数的一些运用来充当算法,以此计算光线的转动。
三角函数在人工智能的算法中充当很重要的角色。
Update函数中主要就是游戏刷新的问题。
Draw函数中主要是关于纹理贴图。
实验名称实验二智能体追逐与躲避
课时安排4课时
实验要求
理解游戏智能体追逐与躲避的数学理论思想,掌握XNA核心代码
实验核心代码:
protectedoverridevoidInitialize()
{
//TODO:
Addyourinitializationlogichere
vp=graphics.GraphicsDevice.Viewport;
//将屏幕分成8×6区域,左右半边各随机放一个障碍物
intrand1=newRandom((int)(DateTime.Now.Ticks>>1)).Next(24);
intrand2=newRandom((int)(DateTime.Now.Ticks>>3)).Next(24);
v2BarrierPos[0].X=(rand1%4*2+1)*vp.Width/(8*2);
v2BarrierPos[0].Y=(rand1/4*2+1)*vp.Height/(6*2);
v2BarrierPos[1].X=(rand2%4*2+1)*vp.Width/(8*2)+vp.Width/2;
v2BarrierPos[1].Y=(rand2/4*2+1)*vp.Height/(6*2);
//计算猫的位置
if(rand1==0)rand1=1;
elserand1=rand1-1;
v2Cat1Pos.X=(rand1%4*2+1)*vp.Width/(8*2);
v2Cat1Pos.Y=(rand1/4*2+1)*vp.Height/(6*2);
//计算老鼠的位置
if(rand2==0)rand2=1;
elserand2=rand2-1;
v2MousePos.X=(rand2%4*2+1)*vp.Width/(8*2)+vp.Width/2;
v2MousePos.Y=(rand2/4*2+1)*vp.Height/(6*2);//根据屏幕来设定老鼠的位置
base.Initialize();
}
privatevoidUpdateMouse(GameTimegameTime)
{
//TODO:
与猫有一定距离后,逃跑
//TODO:
碰到障碍物,转90度逃跑
Viewportvp=graphics.GraphicsDevice.Viewport;
v2MousePos.X=MathHelper.Clamp(v2MousePos.X,vp.X,vp.X+vp.Width);
v2MousePos.Y=MathHelper.Clamp(v2MousePos.Y,vp.Y,vp.Y+vp.Height);
}
}
实验心得:
本次试验主要关于物体的追逐问题。
实验中的猫捉老鼠例子,主要是一个追逐运动,当然里面也有关于随机出现障碍物的问题。
而在此次实验中,涉及了关于包围盒的概念,包围和主要是用来体现碰撞的方面的,实验的智能就体现在老鼠可以根据猫的位置判断是否安全。
实验中包围盒确实比较重要的,对我们以后学习有很大的借鉴作用。
实验名称实验三智能体群集
课时安排2课时
实验要求
理解游戏智能体群集行为的数学理论思想,掌握XNA核心代码
实验核心代码:
Flock:
constintflockSize=40;
intboundryWidth;
intboundryHeight;
Texture2DbirdTex;
Listflock;
publicAIParametersFlockParams
{
set
{
flockParams=value;
}
}
protectedAIParametersflockParams;
publicFlock(Texture2DtheTex,intscreenWidth,intscreenHeight,
AIParametersflockParameters)
{
boundryWidth=screenWidth;
boundryHeight=screenHeight;
birdTex=theTex;
flock=newList();
flockParams=flockParameters;
ResetFlock();
}
publicvoidUpdate(GameTimegameTime,CattheCat)
{
foreach(BirdthisBirdinflock)
{
thisBird.ResetThink();
foreach(BirdtheOtherBirdinflock)
{
//thischeckissowedon'ttrytoflytoourself!
if(thisBird!
=theOtherBird)
{
thisBird.ReactTo(theOtherBird,refflockParams);
}
}
//Lookforthecat
thisBird.ReactTo(theCat,refflockParams);
thisBird.Update(gameTime,refflockParams);
}
}
publicvoidDraw(SpriteBatchspriteBatch,GameTimegameTime)
{
foreach(BirdtheBirdinflock)
{
theBird.Draw(spriteBatch,gameTime);
}
}
publicvoidResetFlock()
{
flock.Clear();
flock.Capacity=flockSize;
BirdtempBird;
Vector2tempDir;
Vector2tempLoc;
Randomrandom=newRandom();
for(inti=0;i{
tempLoc=newVector2((float)
random.Next(boundryWidth),(float)random.Next(boundryHeight));
tempDir=newVector2((float)
random.NextDouble()-0.5f,(float)random.NextDouble()-0.5f);
tempDir.Normalize();
tempBird=newBird(birdTex,tempDir,tempLoc,
boundryWidth,boundryHeight);
flock.Add(tempBird);
}
}
#endregion
}
}
实验心得:
经过本次实验的学习,我们大致了解了集群的原理,集群在游戏智能中是一个比较重要的一块内容,理解了游戏智能体群集行为的数学理论思想,对以后的游戏智能开发有很大的作用,当然这次学习了XNA核心代码,通过这些学习,我们发觉这是一个十分有趣的程序行为,对我们以后在程序中制作多物体的运动起了很大的帮助。
实验名称实验四游戏有限状态机
课时安排4课时
实验要求
通过游戏菜单状态控制理解游戏中的有限状态机思想,掌握XNA核心代码
实验核心代码:
classMainMenuScreen:
MenuScreen
{
#regionInitialization
///
///Constructorfillsinthemenucontents.
///
publicMainMenuScreen()
:
base("主菜单")
{
//Createourmenuentries.
MenuEntryplayGameMenuEntry=newMenuEntry("开始游戏");
MenuEntryoptionsMenuEntry=newMenuEntry("配置选项");
MenuEntryexitMenuEntry=newMenuEntry("退出");
//Hookupmenueventhandlers.
playGameMenuEntry.Selected+=PlayGameMenuEntrySelected;
optionsMenuEntry.Selected+=OptionsMenuEntrySelected;
exitMenuEntry.Selected+=OnCancel;
//Addentriestothemenu.
MenuEntries.Add(playGameMenuEntry);
MenuEntries.Add(optionsMenuEntry);
MenuEntries.Add(exitMenuEntry);
}
voidPlayGameMenuEntrySelected(objectsender,EventArgse)
{
ScreenManager.ClearScreen();
ScreenManager.AddScreen(newGameplayScreen());
}
///
///EventhandlerforwhentheOptionsmenuentryisselected.
///
voidOptionsMenuEntrySelected(objectsender,EventArgse)
{
ScreenManager.ClearScreen();
ScreenManager.AddScreen(newOptionsMenuScreen());
}
#endregion
}
}
MenuEnter:
publicvirtualvoidDraw(MenuScreenscreen,boolisSelected,GameTimegameTime)
{
//Drawtheselectedentryinyellow,otherwisewhite.
Colorcolor=isSelected?
Color.Yellow:
Color.White;
//Modifythealphatofadetextoutduringtransitions.
//color*=1.0f;
//Drawtext,centeredonthemiddleofeachline.
ScreenManagerscreenManager=screen.ScreenManager;
SpriteBatchspriteBatch=screenManager.SpriteBatch;
SpriteFontfont=screenManager.Font;
Vector2origin=newVector2(0,font.LineSpacing/2);
spriteBatch.DrawString(font,text,position,color,0,
origin,1.0f,SpriteEffects.None,0);
}
实验心得:
菜单系统在游戏中充当及其重要的角色,好的菜单能让游戏本身会更具备吸引力,实验中我明白到在XNA程序中,可以通过修改代码实现菜单的内容,当然菜单中的选是要在一个文本文档中写好,程序在调用是遍历那个文档寻找菜单中的字母。
二级菜单同样应具备很强的可调用性。
另外这其中我也学习了包括怎么在程序中添加中文选项。