using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace XNAGame
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;//画笔
Texture2D backgroud;//背景图片
Rectangle rect;
Texture2D cannon;//定义大炮
float cannon_rotation;//定义角度
List<GameObject> UFOs = new List<GameObject>();//定义集合
List<GameObject> CannonBalls = new List<GameObject>();
Random rd = new Random();//定义随机数
KeyboardState prevKeyBordState;//获取键盘状态
//Texture2D explosion;
//
const int explosionWith = 320;
const int explosionHight = 240;
Point explosionSize = new Point(4, 5);
//int currentexplosionX = 0;
//int currentexplosionY = 0;
//Rectangle sourceRect;
List<GameObject> Explosions = new List<GameObject>();
float timer = 0f;
float interal = 1000f/60f;
AudioEngine engine;//对应di
WaveBank waveback;
SoundBank soundbank;
Cue mycue;
SpriteFont GameFont;
int score = 0;
SpriteFont ChineseFont;
/// <summary>
/// 最先执行的程序入口
/// </summary>
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";//Content的根目录存放声音图片信息
}
/// <summary>
/// 初始化代码
/// </summary>
protected override void Initialize()
{
base.Initialize();
}
/// <summary>
/// 载入游戏资源
/// </summary>
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
backgroud = Content.Load<Texture2D>("background");
rect =new Rectangle (0,0,graphics .GraphicsDevice .Viewport.Width ,graphics.GraphicsDevice .Viewport .Height);
cannon = Content.Load<Texture2D>("my\\cannon");
cannon_rotation = -MathHelper .PiOver2;
for (int i = 0; i <5; i++)
{
GameObject ufo = new GameObject();
ufo.sprite = Content.Load<Texture2D>("enemy");
ufo.alive = false;
//ufo.position = new Vector2(0, MathHelper.Lerp(0f, 300f, (float)rd.NextDouble()));
//ufo.velocity = new Vector2(MathHelper.Lerp(0.5f, 5f, (float)rd.NextDouble()), 0);
UFOs.Add(ufo);//加载
}
for (int i = 0; i < 5; i++)
{
GameObject ball = new GameObject();
ball.sprite = Content.Load<Texture2D>("cannonball");
ball.alive = false;
CannonBalls.Add(ball);
}
//explosion =Content.Load<Texture2D >("explosion");
for (int i = 0; i < 5; i++)
{
GameObject ex=new GameObject ();
ex.alive = false;
ex.currentExplosionX = 0;
ex.currentExplosionY = 0;
ex.sprite = Content.Load<Texture2D>("explosion");
Explosions.Add(ex);
}
engine = new AudioEngine("Content/Audioproject.xgs");
waveback = new WaveBank(engine, "Content/Wave Bank.xwb");
soundbank = new SoundBank(engine, "Content/Sound Bank.xsb");
//soundbank.PlayCue("background");
//播放背景音乐
mycue = soundbank.GetCue("background");
mycue.Play();
GameFont = Content.Load<SpriteFont>("GameFont");
ChineseFont = Content.Load<SpriteFont>("ChineseFont");
}
/// <summary>
/// 在游戏结束时释放资源
/// </summary>
protected override void UnloadContent()
{
}
/// <summary>
/// 更新游戏数据
/// </summary>
/// <param name="gameTime"></param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)//获取第一个玩家按键的信息
this.Exit();
//获取键盘信息
KeyboardState keyboard = Keyboard.GetState();
if (keyboard .IsKeyDown ( Keys.Right )||keyboard .IsKeyDown (Keys.D))
{
cannon_rotation += 0.1f;
}
if (keyboard .IsKeyDown (Keys.Left )||keyboard.IsKeyDown (Keys.A))
{
cannon_rotation -=0.1f;
}
//限制在(0到PI/2)
cannon_rotation = MathHelper.Clamp(cannon_rotation, -MathHelper.Pi , 0);
UpdateUFOs();
//判断按键的状态
if (keyboard .IsKeyDown (Keys.Space )&&!prevKeyBordState .IsKeyDown (Keys.Space ))//上次的按键状态
{
foreach (var ball in CannonBalls )
{
if (ball .alive ==false )
{
ball.alive = true;
ball.position = new Vector2(graphics .GraphicsDevice .Viewport .Width /2, 500);
ball.velocity = new Vector2(
(float ) Math.Cos(cannon_rotation) * 5,
(float ) Math.Sin(cannon_rotation) * 5);
//gametime
//声音播放
soundbank.PlayCue("fire");
break ;//程序执行后终止,
}
}
}
UpdateCannonBalls();
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer >interal )
{
updateExplosion();
timer = 0;
}
if (keyboard.IsKeyDown(Keys.P) && !prevKeyBordState.IsKeyDown(Keys.P))//上次的按键状态
{
if (mycue.IsPaused)
{
mycue.Resume();
}
else
{
mycue.Pause();
}
}
if (keyboard.IsKeyDown(Keys.S) && !prevKeyBordState.IsKeyDown(Keys.S))//上次的按键状态
{
if (mycue.IsStopped)
{
mycue = soundbank.GetCue("background");
mycue.Play();
}
else
{
mycue.Stop(AudioStopOptions.Immediate);
}
}
prevKeyBordState = keyboard;
base.Update(gameTime);
}
/// <summary>
/// 更新爆炸效果
/// </summary>
private void updateExplosion()
{
foreach (var ex in Explosions )
{
if (ex.alive == true)
{
ex.sourceRect = new Rectangle(
ex.currentExplosionX * explosionWith,
ex.currentExplosionY * explosionHight,
explosionWith,
explosionHight);
ex.currentExplosionX++;
if (ex.currentExplosionX >=explosionSize.X)
{
ex.currentExplosionX = 0;
ex.currentExplosionY++;
if (ex.currentExplosionY >= explosionSize.Y)
{
ex.currentExplosionY = 0;
ex.alive = false;
}
}
}
}
//sourceRect = new Rectangle(
// currentexplosionX * explosionWith,
// currentexplosionY * explosionHight,
// explosionWith,
// explosionHight);
}
/// <summary>
/// 更新炮弹
/// </summary>
private void UpdateCannonBalls()
{
foreach (var ball in CannonBalls)
{
if (ball.alive == true)
{
ball.position += ball.velocity;
if (!rect.Contains(new Point((int)ball.position.X, (int)ball.position.Y)))
{
ball.alive = false;
}
else
{
//
Rectangle rectBall = new Rectangle(
(int)ball.position.X,
(int)ball.position.Y,
ball.sprite.Width,
ball.sprite.Height);
foreach (var ufo in UFOs)
{
Rectangle rectUFO = new Rectangle(
(int)ufo.position.X,
(int)ufo.position.Y,
ufo.sprite.Width,
ufo.sprite.Height
);
if (rectBall.Intersects(rectUFO))
{
ball.alive = false;
ufo.alive = false;
CreateExplosion(ufo);
score++;
break;
}
}
}
}
}
}
/// <summary>
/// 产生爆炸
/// </summary>
/// <param name="ufo"></param>
private void CreateExplosion(GameObject ufo)
{
foreach (var ex in Explosions)
{
if (ex.alive == false)
{
ex.alive = true;
ex.currentExplosionX = 0;
ex.currentExplosionY = 0;
ex.position = new Vector2(
ufo.position.X -
(explosionWith - ufo.sprite.Width) / 2,
ufo.position.Y - (explosionHight - ufo.sprite.Height) / 2
);
}
soundbank.PlayCue("explosion");
break ;
}
}
/// <summary>
/// 更新UFO信息
/// </summary>
private void UpdateUFOs()
{
foreach (var ufo in UFOs)
{
//ufo.position.X = ufo.velocity.X;
//ufo.position.Y = ufo.velocity.Y;
if (ufo.alive == true)
{
ufo.position += ufo.velocity;
if (ufo.position.X > 800)
{
ufo.alive = false;
}
}
if (ufo.alive == false)
{
ufo.alive = true;
//初始化UFO
ufo.position = new Vector2(0, MathHelper.Lerp(0f, 300f, (float)rd.NextDouble()));
ufo.velocity = new Vector2(MathHelper.Lerp(0.5f, 5f, (float)rd.NextDouble()), 0);
//MathHelper.Lerp(min,max,rd)=min+(max-min)*rd;
}
}
}
/// <summary>
/// 绘制游戏界面
/// </summary>
/// <param name="gameTime"></param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
//游戏的开始
spriteBatch.Begin();
//绘制背景图片
spriteBatch.Draw(backgroud, rect, Color.White);
spriteBatch.Draw(cannon, new Vector2(graphics .GraphicsDevice .Viewport .Width /2, 500),
//new Rectangle (0,0,cannon .Width,cannon.Height ),
null ,
Color.White,
////-(float) Math.PI /2,
//-MathHelper .PiOver2 ,
cannon_rotation,
new Vector2 (cannon.Width/2,cannon .Height /2), 1f,
SpriteEffects .None ,0f
);//(纹理参数,位置,颜色)
//游戏的结束
foreach (var ufo in UFOs)
{
if (ufo.alive ==true )
{
spriteBatch.Draw(ufo.sprite, ufo.position, Color.White);
}
}
foreach (var ball in CannonBalls )
{
if (ball .alive ==true )
{
spriteBatch.Draw(
ball.sprite,
ball.position,
Color.White);
}
}
foreach (var ex in Explosions )
{
spriteBatch.Draw(ex.sprite, ex.position ,
ex.sourceRect , Color.White);
}
spriteBatch.DrawString(GameFont, "Score:"+score .ToString (), new Vector2(50, 50), Color.Yellow);
spriteBatch.DrawString(ChineseFont, "分数:" + score.ToString(), new Vector2(50, 100), Color.Yellow);
spriteBatch.End();
base.Draw(gameTime);
}
}
}