通用的东西就不写了,节约空间
游戏规则:
小行星会源源不断的从屏幕上方出现,每个小行星会有随机的速度和角度,玩家操控飞机闪避这些小行星,每出现一个行星,分数+1
class Meteor
{
public Texture2D sprite; //used to store the picture of meteor
public Vector2 position; //the position of the meteor
public double angle; //the angle of the meteor
public Vector2 center; //the center of the meteor picture
public Vector2 velocity; //the velocity of the meteor
public bool alive; //used to determine the meteor state
public Rectangle rect; //the rectangle of meteor
//initial the meteor class
public Meteor(Texture2D loadedTexture)
{
angle = 0.0f;
position = Vector2.Zero;
sprite = loadedTexture;
center = new Vector2(sprite.Width / 2, sprite.Height / 2);
velocity = Vector2.Zero;
alive = false;
}
}
class SpaceShip
{
public Texture2D sprite; //used to store the picture of space ship
public Vector2 position; //the space ship's position
public Vector2 center; //the center of the space ship
public Vector2 velocity; //the space ship's velocity
public bool alive; //used to determine the space ship's state
public Rectangle rect; //the rectangle of space ship
//initial the space ship class
public SpaceShip(Texture2D loadedTexture)
{
position = Vector2.Zero;
sprite = loadedTexture;
center = new Vector2(sprite.Width / 2, sprite.Height / 2);
velocity = Vector2.Zero;
alive = false;
}
}
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D background; //used to store the backgroud picture
Rectangle viewportRect; //viewport's rectangle
SpaceShip spaceship; //the space ship
const int maxMeteors = 10; //the max number of meteors
Meteor[] meteorList; //a meteor list to hold all meteors
const float maxMeteorVelocity = 8.0f; //max velocity of meteor
const float minMeteorVelocity = 3.0f; //min velocity of meteor
Random random = new Random(); //used to get random value
int score = 0; //used to store score, initial is 0
int highestScore = 0; //the highest score
SpriteFont font;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
//load background picture
background = Content.Load<Texture2D>("Sprites\\SpaceBackground");
//load the space ship
spaceship = new SpaceShip(Content.Load<Texture2D>("Sprites\\spaceShip"));
spaceship.position = new Vector2(graphics.GraphicsDevice.Viewport.Width/2,graphics.GraphicsDevice.Viewport.Height-75);//set up the position
spaceship.rect = new Rectangle((int)spaceship.position.X, (int)spaceship.position.Y, 30, 30);
//load meteors
meteorList = new Meteor[maxMeteors];
for (int i=0; i<maxMeteors; i++)
{
meteorList[i] = new Meteor(Content.Load<Texture2D>("Sprites\\meteor"));
}
//load game font
font = Content.Load<SpriteFont>("Fonts\\GameFont");
//drawable area of the game screen.
viewportRect = new Rectangle(0, 0,
graphics.GraphicsDevice.Viewport.Width,
graphics.GraphicsDevice.Viewport.Height);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
//update the meteor position
public void UpdateMeteors()
{
foreach (Meteor meteor in meteorList)
{
if (meteor.alive) //if meteor.alive is true
{
meteor.position += meteor.velocity; //update meteor's position
meteor.rect.X = (int)meteor.position.X;
meteor.rect.Y = (int)meteor.position.Y;
//if the meteor out of the screen
if (!viewportRect.Contains(new Point(
(int)meteor.position.X,
(int)meteor.position.Y)))
{
meteor.alive = false;
}
}
else
{
meteor.alive = true;
//position (x,y), x is a random value between screen left and screen right, y is the screen top
meteor.position = new Vector2(
MathHelper.Lerp(
(float)viewportRect.Left,
(float)viewportRect.Right,
(float)random.NextDouble()),
viewportRect.Top);
meteor.rect = new Rectangle((int)meteor.position.X,(int)meteor.position.Y, 45, 45);
//the angle of the meteor's velocity
meteor.angle = MathHelper.Lerp(MathHelper.ToRadians(-15), MathHelper.ToRadians(15), (float)random.NextDouble());
//the velocity of the meteor
meteor.velocity = new Vector2(
//x velocity
MathHelper.Lerp(
minMeteorVelocity,
maxMeteorVelocity,
(float)random.NextDouble())*(float)Math.Sin(meteor.angle),
//y velocity
MathHelper.Lerp(
minMeteorVelocity,
maxMeteorVelocity,
(float)random.NextDouble())*(float)Math.Cos(meteor.angle)
);
score++; //each alive meteor increase score by 1
if (highestScore < score)
highestScore = score; //get the highest score
}
//if meteor hits the space ship
if (meteor.rect.Intersects(spaceship.rect))
{
//initial the space ship's position
spaceship.position = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height - 75);
//kill all meteors in the meteor list therefore initial meteors
foreach (Meteor meteors in meteorList)
{
meteors.alive = false;
}
//initial score
score = 0;
}
}
}
//update the position of space ship
public void UpdateSpaceShip()
{
spaceship.rect.X = (int)spaceship.position.X;
spaceship.rect.Y = (int)spaceship.position.Y;
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
#if !XBOX
KeyboardState keyboardState = Keyboard.GetState();
//'Q' exit the game
if (keyboardState.IsKeyDown(Keys.Q))
this.Exit();
if (keyboardState.IsKeyDown(Keys.Left))
{
//make sure the space ship doesn't move out of screen
if (spaceship.position.X > viewportRect.Left - 3)
spaceship.position.X -= 3;
}
if (keyboardState.IsKeyDown(Keys.Right))
{
//make sure the space ship doesn't move out of screen
if (spaceship.position.X < viewportRect.Right - 3)
spaceship.position.X += 3;
}
if (keyboardState.IsKeyDown(Keys.Down))
{
//make sure the space ship doesn't move out of screen
if (spaceship.position.Y < graphics.GraphicsDevice.Viewport.Height - 3)
spaceship.position.Y += 3;
}
if (keyboardState.IsKeyDown(Keys.Up))
{
//make sure the space ship doesn't move out of screen
if (spaceship.position.Y > 3)
spaceship.position.Y -= 3;
}
#endif
UpdateSpaceShip();
UpdateMeteors();
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
//Draw the background sized to the width and height of the screen.
spriteBatch.Draw(background, viewportRect,
Color.White);
spriteBatch.Draw(spaceship.sprite, spaceship.rect, Color.White);
//Draw meteors
foreach (Meteor meteor in meteorList)
{
if (meteor.alive)
{
spriteBatch.Draw(meteor.sprite,
meteor.rect, Color.White);
}
}
//draw score
spriteBatch.DrawString(font,
"Score: " + score.ToString(),
new Vector2(20,20),
Color.Yellow);
spriteBatch.DrawString(font,
"Highest Score: " + highestScore.ToString(),
new Vector2(160,20),
Color.Red);
spriteBatch.End();
base.Draw(gameTime);
}
}