有了子彈蜂奸,子彈想有個(gè)飛機(jī)能發(fā)射子彈了舌稀。好,現(xiàn)在我們就實(shí)現(xiàn)玩家的戰(zhàn)機(jī)
/// <summary>
/// Description of GamePlayer.
/// </summary>
public class GamePlayer : GameObject
{
private GameScene mScene = null;
private Vector2 InitPosition;
public Vector2 Speed;
public bool IsLive;
private float ShootDelay = 0;
private float ShootDelayTime = 0;
private int PlayerSpriteIndex;
private int Life = 0; // 生命值
private float ReliveTime = 0; // 重生時(shí)間
private float GodTime = 0; // 無(wú)敵時(shí)間
}
這是玩家類的定義剖效,當(dāng)然也是繼承自GameObject的嫉入。
我們可以看到,玩家類定義的東西比GameBullet明顯多了一點(diǎn)璧尸。但大體上差不多的咒林。
然后是具體的實(shí)例化和一些基本函數(shù)
public GamePlayer(GameScene scene, Vector2 position, Vector2 speed)
{
this.mScene = scene;
this.InitPosition = position;
this.Position = position;
this.Speed = speed;
this.BoxCollider = new Box((int)position.X, (int)position.Y, 8, 8);
this.IsLive = true;
this.Life = 3;
this.ShootDelay = 0.10f;
this.PlayerSpriteIndex = 0;
}
public bool Collide(Box box, int decLife)
{
if ( IsLive == false || GodTime > 0 )
{
return false;
}
if ( BoxCollider.Collide(box) == true )
{
DecLife(decLife);
return true;
}
return false;
}
public void DecLife(int decLife)
{
if ( GodTime > 0 )
{
return;
}
Life -= decLife;
GameBombManager.AddBomb(Position, false, 0.5f);
if ( Life <= 0 && Config.IsDebug == false )
{
IsLive = false;
}
else
{
IsLive = false;
ReliveTime = 1.5f;
}
}
public void Init()
{
Position = InitPosition;
IsLive = true;
ShootDelayTime = 0;
PlayerSpriteIndex = 0;
GodTime = 3.0f;
BoxCollider.X = (int)Position.X;
BoxCollider.Y = (int)Position.Y + 3;
}
DecLife負(fù)責(zé)扣除玩家生命值,然后添加爆炸效果爷光,然后處理無(wú)敵啊映九,重生啊之類的時(shí)間
接下來(lái)是繪制部分的代碼, 很簡(jiǎn)單瞎颗,就是根據(jù)玩家位置件甥,把玩家畫到屏幕上就行
public override void Render(Graphics g)
{
if ( IsLive == false )
{
return;
}
g.DrawImage(Data.PlayerSource,
new Rectangle((int)Position.X - Config.HalfPlayerWidth, (int)Position.Y - Config.HalfPlayerHeight, Config.PlayerWidth, Config.PlayerHeight),
new Rectangle(PlayerSpriteIndex * Config.PlayerWidth, 0, Config.PlayerWidth, Config.PlayerHeight), GraphicsUnit.Pixel);
g.DrawImage(Data.PlayerPointSource,
new Rectangle((int)Position.X - 5, (int)Position.Y - 5, 10, 10),
new Rectangle(0, 0, 10, 10),
GraphicsUnit.Pixel);
g.DrawImage(Data.HpSource,
new Rectangle(5, 25, 24, 24),
new Rectangle(0, 0, 24, 24),
GraphicsUnit.Pixel);
g.DrawString(Life.ToString(), Data.NormalFont, Brushes.White, 30, 27);
if ( Config.IsDebug && Config.IsShowBox )
{
g.DrawRectangle(Pens.Green, BoxCollider.ToRectangle());
}
}
然后是最重要的Update了。
public override void Update(float elapsedTime)
{
if ( IsLive == false )
{
if ( ReliveTime > 0 )
{
ReliveTime -= elapsedTime;
if ( ReliveTime <= 0 )
{
Init();
}
}
return;
}
if ( GodTime > 0 )
{
GodTime -= elapsedTime;
}
if ( mScene.EnemyManager.Collide(BoxCollider, 10) == true )
{
DecLife(1);
return;
}
if ( mScene.BossManager.Collide(BoxCollider, 10) == true )
{
DecLife(1);
return;
}
if ( ShootDelayTime > 0 )
{
ShootDelayTime -= elapsedTime;
}
this.PlayerSpriteIndex = 0;
if ( Input.IsKeyDown(Keys.Left) )
{
Position.X -= (Speed.X * elapsedTime);
this.PlayerSpriteIndex = 1;
}
if ( Input.IsKeyDown(Keys.Right) )
{
Position.X += (Speed.X * elapsedTime);
this.PlayerSpriteIndex = 2;
}
if ( Input.IsKeyDown(Keys.Up) )
{
Position.Y -= (Speed.Y * elapsedTime);
}
if ( Input.IsKeyDown(Keys.Down) )
{
Position.Y += (Speed.Y * elapsedTime);
}
if ( Input.IsKeyDown(Keys.Z) || Input.IsKeyDown(Keys.Space) )
{
if ( ShootDelayTime <= 0 )
{
Shoot();
}
}
if ( Position.X < Config.HalfPlayerWidth )
{
Position.X = Config.HalfPlayerWidth;
}
if ( Position.X >= Config.ScreenWidth - Config.HalfPlayerWidth )
{
Position.X = Config.ScreenWidth - Config.HalfPlayerWidth;
}
if ( Position.Y < Config.HalfPlayerHeight )
{
Position.Y = Config.HalfPlayerHeight;
}
if ( Position.Y >= Config.ScreenHeight - Config.HalfPlayerHeight )
{
Position.Y = Config.ScreenHeight - Config.HalfPlayerHeight;
}
BoxCollider.X = (int)Position.X;
BoxCollider.Y = (int)Position.Y;
}
可以看到哼拔,大概分為幾部分
第一部分是處理重生了和無(wú)敵引有。。檢測(cè)到自己IsLive為false后倦逐,就開始倒計(jì)時(shí)譬正。
第二部分是處理碰撞,其實(shí)是委托給了EnemyManager和BossManager進(jìn)行處理
第三部分是處理按鍵信息檬姥,唯一要注意的時(shí)曾我,射擊有CD的..
最后是射擊的部分
private void Shoot()
{
ShootDelayTime = ShootDelay;
GameScene.MainScene.AddBullet(new GameBullet(true, new Vector2(Position.X, Position.Y - Data.BulletsSize[0].Height / 2), new Vector2(0, -400), new Vector2(0, 0), 0, 1, 0));
}
也就是用了我們上一章的例子,創(chuàng)建子彈健民,并且賦值一些數(shù)據(jù)而已
好抒巢,我們現(xiàn)在有一架飛機(jī),并且可以在屏幕上移動(dòng)和射擊了秉犹。蛉谜。
等等稚晚,有人會(huì)問,我現(xiàn)在什么都看不到型诚,而且代碼怎么編譯客燕?
GameScene是什么?EnemyManager又是什么狰贯?別急也搓,慢慢來(lái),下一章就將GameScene