Learning XNA3.0 第七章 第一节(1)
2010-09-17 12:46
106 查看
Learning XNA 3.0 第七章 整到一起
好吧,现在你已经有了一个坚实的构思而且对于一个可能会成为很犀利的游戏有了一个开始。再说一次,这个游戏的概念是玩家控制一个精灵躲避在屏幕上到处出没的追踪精灵,与此同时,你还要去抓正在逃跑的精灵。现在你需要给你的游戏加一些几分和游戏逻辑,还要再略微调整一下,才能让你的游戏变成你想要的样子。
在这一章里你首先要做的事情就是给游戏增加一些计分逻辑。当你在写游戏并且打算着手于计分设计的时候,第一步就是确定哪些事件会引发分数的改变。有时某些武器打中玩家会引起分数的改变,有时玩家自己打中东西你也要改变分数,再有时,玩家完成了某件事情,你也要改变分数(比如,解除一个谜,回答了一个问题,推倒一个小loli等等)。
这个游戏里呢,如果一个敌人没有撞到你就离开屏幕了,你就要改变分数了,也就是说,玩家成功的躲避了敌人。
假如有这么一个计分机制,那就有点意思了。就是你给某些个单独的敌人设置的分数高一点。有些敌人会比其他人值钱点,比如说,那个敌人速度很快,或者体积很大,或者是个领导……随你喜欢~
除了计算分数外,你还要往屏幕上画分数。我们先要解决这个方面的问题,然后再看看当一个敌人飞出屏幕外而没有撞到你的时候如何调整分数。
在这个章节里,除了给游戏增加计分和学习使用SpriteFont类绘制文本外,你还要通过往游戏里添加各种类型的精灵图像和各种不同的声音来丰富你的游戏。你还要加入背景图像,考虑游戏状态,还要给游戏添加一个开关。
绘制2D文本
本章代码基于你第六章写的代码。打开那个游戏项目文件,然后在整个章节中使用它……
(我了个去,要是没有以前的代码咋办……)
//作者有个官方网站,我找到了把网址加进来,大家可以去下载源码。
首先,你要给Sprite基类添加一个呈现分数值的整型变量(注意要通过自动生成属性来添加一个public的get访问器)。
public int scoreValue {get; protected set;}
更改Sprite类里的两个构造函数,让他们接收一个记录当前精灵分数的整型值。第一个构造函数应该把值传递给第二个构造函数,第二个构造函数应该利用这个值来对成员变量scoreValue进行设置。这样Sprite类的构造函数看起来应该是这个样子的:
public Sprite(Texture2D textureImage, Vector2 position, Point frameSize,
int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed,
string collisionCueName, int scoreValue)
: this(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed, defaultMillisecondsPerFrame, collisionCueName,
scoreValue)
{
}
public Sprite(Texture2D textureImage, Vector2 position, Point frameSize,
int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed,
int millisecondsPerFrame, string collisionCueName, int scoreValue)
{
this.textureImage = textureImage;
this.position = position;
this.frameSize = frameSize;
this.collisionOffset = collisionOffset;
this.currentFrame = currentFrame;
this.sheetSize = sheetSize;
this.speed = speed;
this.collisionCueName = collisionCueName;
this.millisecondsPerFrame = millisecondsPerFrame;
this.scoreValue = scoreValue;
}
你还要对子类的构造函数进行更改(AutomatedSprite,ChasingSprite, EvadingSprite, 还有 UserControlledSprite),让它们接收一个记录分数的整型变量参数的值,然后把值传送给父类的构造函数。这几个类的构造函数应该改成这样:
AutomatedSprite类的构造函数:
public AutomatedSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
Vector2 speed, string collisionCueName, int scoreValue)
: base(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed, collisionCueName, scoreValue)
{
}
public AutomatedSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
Vector2 speed, int millisecondsPerFrame, string collisionCueName,
int scoreValue)
: base(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed, millisecondsPerFrame, collisionCueName, scoreValue)
{
}
ChasingSprite类的构造函数:
public ChasingSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame,
Point sheetSize, Vector2 speed, string collisionCueName,
SpriteManager spriteManager, int scoreValue)
: base(textureImage, position, frameSize, collisionOffset,
currentFrame, sheetSize, speed, collisionCueName, scoreValue)
{
this.spriteManager = spriteManager;
}
public ChasingSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame,
Point sheetSize, Vector2 speed, int millisecondsPerFrame,
string collisionCueName, SpriteManager spriteManager,
int scoreValue)
: base(textureImage, position, frameSize, collisionOffset,
currentFrame, sheetSize, speed, millisecondsPerFrame,
collisionCueName, scoreValue)
{
this.spriteManager = spriteManager;
}
EvadingSprite类的构造函数:
public EvadingSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame,
Point sheetSize, Vector2 speed, string collisionCueName,
SpriteManager spriteManager, float evasionSpeedModifier,
int evasionRange, int scoreValue)
: base(textureImage, position, frameSize, collisionOffset,
currentFrame, sheetSize, speed, collisionCueName, scoreValue)
{
this.spriteManager = spriteManager;
this.evasionSpeedModifier = evasionSpeedModifier;
this.evasionRange = evasionRange;
}
public EvadingSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame,
Point sheetSize, Vector2 speed, int millisecondsPerFrame,
string collisionCueName, SpriteManager spriteManager,
float evasionSpeedModifier, int evasionRange,
int scoreValue)
: base(textureImage, position, frameSize, collisionOffset,
currentFrame, sheetSize, speed, millisecondsPerFrame,
collisionCueName, scoreValue)
{
this.spriteManager = spriteManager;
this.evasionSpeedModifier = evasionSpeedModifier;
this.evasionRange = evasionRange;
}
UserControlledSprite类的构造函数:
public UserControlledSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
Vector2 speed)
: base(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed, null, 0)
{
}
public UserControlledSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
Vector2 speed, int millisecondsPerFrame)
: base(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed, millisecondsPerFrame, null, 0)
{
}
UserControlledSprite类没有一个与之关联的分数变量,因为你没法通过躲过自己然后给自己加分吧?因此,你不必给这个类的构造函数添加一个新的参数,但是你还是要给基类构造函数中的scoreValue参数传一个0值。
好吧,现在你已经有了一个坚实的构思而且对于一个可能会成为很犀利的游戏有了一个开始。再说一次,这个游戏的概念是玩家控制一个精灵躲避在屏幕上到处出没的追踪精灵,与此同时,你还要去抓正在逃跑的精灵。现在你需要给你的游戏加一些几分和游戏逻辑,还要再略微调整一下,才能让你的游戏变成你想要的样子。
在这一章里你首先要做的事情就是给游戏增加一些计分逻辑。当你在写游戏并且打算着手于计分设计的时候,第一步就是确定哪些事件会引发分数的改变。有时某些武器打中玩家会引起分数的改变,有时玩家自己打中东西你也要改变分数,再有时,玩家完成了某件事情,你也要改变分数(比如,解除一个谜,回答了一个问题,推倒一个小loli等等)。
这个游戏里呢,如果一个敌人没有撞到你就离开屏幕了,你就要改变分数了,也就是说,玩家成功的躲避了敌人。
假如有这么一个计分机制,那就有点意思了。就是你给某些个单独的敌人设置的分数高一点。有些敌人会比其他人值钱点,比如说,那个敌人速度很快,或者体积很大,或者是个领导……随你喜欢~
除了计算分数外,你还要往屏幕上画分数。我们先要解决这个方面的问题,然后再看看当一个敌人飞出屏幕外而没有撞到你的时候如何调整分数。
在这个章节里,除了给游戏增加计分和学习使用SpriteFont类绘制文本外,你还要通过往游戏里添加各种类型的精灵图像和各种不同的声音来丰富你的游戏。你还要加入背景图像,考虑游戏状态,还要给游戏添加一个开关。
绘制2D文本
本章代码基于你第六章写的代码。打开那个游戏项目文件,然后在整个章节中使用它……
(我了个去,要是没有以前的代码咋办……)
//作者有个官方网站,我找到了把网址加进来,大家可以去下载源码。
首先,你要给Sprite基类添加一个呈现分数值的整型变量(注意要通过自动生成属性来添加一个public的get访问器)。
public int scoreValue {get; protected set;}
更改Sprite类里的两个构造函数,让他们接收一个记录当前精灵分数的整型值。第一个构造函数应该把值传递给第二个构造函数,第二个构造函数应该利用这个值来对成员变量scoreValue进行设置。这样Sprite类的构造函数看起来应该是这个样子的:
public Sprite(Texture2D textureImage, Vector2 position, Point frameSize,
int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed,
string collisionCueName, int scoreValue)
: this(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed, defaultMillisecondsPerFrame, collisionCueName,
scoreValue)
{
}
public Sprite(Texture2D textureImage, Vector2 position, Point frameSize,
int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed,
int millisecondsPerFrame, string collisionCueName, int scoreValue)
{
this.textureImage = textureImage;
this.position = position;
this.frameSize = frameSize;
this.collisionOffset = collisionOffset;
this.currentFrame = currentFrame;
this.sheetSize = sheetSize;
this.speed = speed;
this.collisionCueName = collisionCueName;
this.millisecondsPerFrame = millisecondsPerFrame;
this.scoreValue = scoreValue;
}
你还要对子类的构造函数进行更改(AutomatedSprite,ChasingSprite, EvadingSprite, 还有 UserControlledSprite),让它们接收一个记录分数的整型变量参数的值,然后把值传送给父类的构造函数。这几个类的构造函数应该改成这样:
AutomatedSprite类的构造函数:
public AutomatedSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
Vector2 speed, string collisionCueName, int scoreValue)
: base(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed, collisionCueName, scoreValue)
{
}
public AutomatedSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
Vector2 speed, int millisecondsPerFrame, string collisionCueName,
int scoreValue)
: base(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed, millisecondsPerFrame, collisionCueName, scoreValue)
{
}
ChasingSprite类的构造函数:
public ChasingSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame,
Point sheetSize, Vector2 speed, string collisionCueName,
SpriteManager spriteManager, int scoreValue)
: base(textureImage, position, frameSize, collisionOffset,
currentFrame, sheetSize, speed, collisionCueName, scoreValue)
{
this.spriteManager = spriteManager;
}
public ChasingSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame,
Point sheetSize, Vector2 speed, int millisecondsPerFrame,
string collisionCueName, SpriteManager spriteManager,
int scoreValue)
: base(textureImage, position, frameSize, collisionOffset,
currentFrame, sheetSize, speed, millisecondsPerFrame,
collisionCueName, scoreValue)
{
this.spriteManager = spriteManager;
}
EvadingSprite类的构造函数:
public EvadingSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame,
Point sheetSize, Vector2 speed, string collisionCueName,
SpriteManager spriteManager, float evasionSpeedModifier,
int evasionRange, int scoreValue)
: base(textureImage, position, frameSize, collisionOffset,
currentFrame, sheetSize, speed, collisionCueName, scoreValue)
{
this.spriteManager = spriteManager;
this.evasionSpeedModifier = evasionSpeedModifier;
this.evasionRange = evasionRange;
}
public EvadingSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame,
Point sheetSize, Vector2 speed, int millisecondsPerFrame,
string collisionCueName, SpriteManager spriteManager,
float evasionSpeedModifier, int evasionRange,
int scoreValue)
: base(textureImage, position, frameSize, collisionOffset,
currentFrame, sheetSize, speed, millisecondsPerFrame,
collisionCueName, scoreValue)
{
this.spriteManager = spriteManager;
this.evasionSpeedModifier = evasionSpeedModifier;
this.evasionRange = evasionRange;
}
UserControlledSprite类的构造函数:
public UserControlledSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
Vector2 speed)
: base(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed, null, 0)
{
}
public UserControlledSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
Vector2 speed, int millisecondsPerFrame)
: base(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed, millisecondsPerFrame, null, 0)
{
}
UserControlledSprite类没有一个与之关联的分数变量,因为你没法通过躲过自己然后给自己加分吧?因此,你不必给这个类的构造函数添加一个新的参数,但是你还是要给基类构造函数中的scoreValue参数传一个0值。
相关文章推荐
- Learning XNA3.0 第七章 第一节(2)
- 第七章 vb控制结构 第一节 选择控制结构
- 第七章[多线程]第一节、Java中的多线程
- 第三部分:Android 应用程序接口指南---第一节:应用程序组件---第七章 App Widgets
- 第七章 股票的交易程序与方式 第一节 开户
- learn python第七章:面向对象编程 (第一节:类)
- SQL SERVER2000教程-第七章 Transact-SQL编程 第一节 变量
- SQL SERVER2000教程-第七章 Transact-SQL编程 第一节 变量
- BAT教程 :第一节(批处理基础)
- C++ primer之第七章(类 4)
- 马士兵J2SE-第七章-容器
- 第七章-图(5)图的最小生成树(Kruskal和Prim算法)
- 1、考研组成原理(第七章 I/O系统)
- 第一节、Vue.js框架是什么,为什么选择它?
- 破解完全入门篇 第七章-寻找软件的注册码
- vue第一节—数据绑定和第一个vue应用
- 第七章 php自定义函数实现代码
- 高级编程技术 week4 第七章
- 第七章 图形程序设计
- 《鸟哥的Linux私房菜基础》第七章、Linux 档案与目录管理