參考:
http://blog.xuite.net/jin117/blog/29655829 "去背程式教學附下載網址"
http://create.msdn.com/en-US/education/catalog/tutorial/2d_chapter_9 "2D範例"
http://msdn.microsoft.com/zh-tw/dd310332#XNA "MSDN邊做邊學系列-XBOX遊戲開發教學"
邊做邊學系列1 圖十六.教學在畫面中產生中文
修改處以下介紹
紅字可更改數值 不同顏色底是有互相關聯
------------------------------------------------------------------------------------
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;
using XnaBreakout.Helpers;
namespace WindowsGame7
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
bool lostGame = false; //*一開始不會出現*
Texture2D backgroundTexture; //背景
Rectangle viewportRect; //視野
SpriteBatch spriteBatch;
GameObject flyingsaucer; //炮
const int maxmissiles = 5; //*子彈數量*
GameObject[] missiles;
GamePadState previousGamePadState = GamePad.GetState(PlayerIndex.One);
KeyboardState previousKeyboardState = Keyboard.GetState();
const int maxEnemies = 5;
const float maxTankHeight = 0.8f; //*設定坦克出現位置*
const float minTankHeight = 0.6f;
const float maxTankVelocity = 5.0f; //*坦克速度*
const float minTankVelocity = 1.0f;
Random random = new Random();
GameObject[] tanks;
int score;
int miss;
SpriteFont font;
Vector2 scoreDrawPoint = new Vector2(0.4f, 0.0f); //*得分記數位置*
Vector2 missDrawPoint = new Vector2(0.2f, 0.0f); //*失分記數位置*
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
backgroundTexture = Content.Load<Texture2D>("models\\background");
flyingsaucer = new GameObject(Content.Load<Texture2D>("models\\flyingsaucer"));
flyingsaucer.position = new Vector2(150, graphics.GraphicsDevice.Viewport.Height - 550);//*設定
飛碟的高度*
missiles = new GameObject[maxmissiles];
for (int i = 0; i < maxmissiles; i++) //子彈增加
{
missiles[i] = new GameObject(Content.Load<Texture2D>("models\\missile"));
}
tanks = new GameObject[maxEnemies];
for (int i = 0; i < maxEnemies; i++) //敵人增加
{
tanks[i] = new GameObject(
Content.Load<Texture2D>("models\\tank1"));
}
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);
Window.Title = "外星人入侵!!!"; //*標題*
base.LoadContent();
}
public void UpdateMissiles()
{
foreach (GameObject missile in missiles)
{
if (missile.alive)
{
missile.position += missile.velocity;
if (!viewportRect.Contains(new Point(
(int)missile.position.X,
(int)missile.position.Y)))
{
missile.alive = false;
continue;
}
Rectangle missileRect = new Rectangle(
(int)missile.position.X,
(int)missile.position.Y,
missile.sprite.Width,
missile.sprite.Height);
foreach (GameObject tank in tanks)
{
Rectangle enemyRect = new Rectangle(
(int)tank.position.X,
(int)tank.position.Y,
tank.sprite.Width,
tank.sprite.Height);
if (missileRect.Intersects(enemyRect))
{
missile.alive = false;
tank.alive = false;
score += 1;
break;
}
}
}
}
}
protected override void UnloadContent()
{
base.UnloadContent();
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
flyingsaucer.rotation += gamePadState.ThumbSticks.Left.X * 0.1f;
if (gamePadState.Buttons.A == ButtonState.Pressed &&
previousGamePadState.Buttons.A == ButtonState.Released)
{
FireCannonBall();
}
#if !XBOX
KeyboardState keyboardState = Keyboard.GetState(); //*移動飛碟*
if (keyboardState.IsKeyDown(Keys.Left))
{
flyingsaucer.position.X -= 5.0f;
}
if (keyboardState.IsKeyDown(Keys.Right))
{
flyingsaucer.position.X += 5.0f;
}
if (keyboardState.IsKeyDown(Keys.Space) &&
previousKeyboardState.IsKeyUp(Keys.Space))
{
FireCannonBall();
}
#endif
UpdateMissiles();
UpdateEnemies();
previousGamePadState = gamePadState;
#if !XBOX
previousKeyboardState = keyboardState;
#endif
base.Update(gameTime);
}
public void UpdateEnemies()
{
foreach (GameObject tank in tanks)
{
if (tank.alive)
{
tank.position += tank.velocity;
if (!viewportRect.Contains(new Point(
(int)tank.position.X,
(int)tank.position.Y)))
{
tank.alive = false;
miss += 1; //*被進攻坦克+1*
if (miss > 5) //*超過5台坦克就輸了*
{
lostGame = true;
}
}
}
else
{
tank.alive = true;
tank.position = new Vector2(
viewportRect.Right,
MathHelper.Lerp(
(float)viewportRect.Height * minTankHeight,
(float)viewportRect.Height * maxTankHeight,
(float)random.NextDouble()));
tank.velocity = new Vector2(
MathHelper.Lerp(
-minTankVelocity,
-maxTankVelocity,
(float)random.NextDouble()), 0);
}
}
}
public void FireCannonBall()
{
foreach (GameObject ball in missiles)
{
if (!ball.alive)
{
ball.alive = true;
ball.position = flyingsaucer.position - ball.center;
ball.velocity = new Vector2(
(float)Math.Sin(flyingsaucer.rotation),
(float)Math.Cos(flyingsaucer.rotation)) * 5.0f; //*飛碟速度,砲彈方向 Sin跟Cos對換*
return;
}
}
}
/// <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);
spriteBatch.Draw(backgroundTexture, viewportRect,
Color.White);
foreach (GameObject ball in missiles)
{
if (ball.alive)
{
spriteBatch.Draw(ball.sprite,
ball.position, Color.Gold);
}
}
spriteBatch.Draw(flyingsaucer.sprite,
flyingsaucer.position, null, Color.White, flyingsaucer.rotation, flyingsaucer.center, 1.5f, SpriteEffects.None, 0);//*飛碟位置大小*
foreach (GameObject enemy in tanks)
{
if (enemy.alive)
{
spriteBatch.Draw(enemy.sprite,
enemy.position, Color.White);
}
}
spriteBatch.DrawString(font,"殲滅坦克數 : " + score.ToString()+"台", new Vector2
(scoreDrawPoint.X * viewportRect.Width,scoreDrawPoint.Y * viewportRect.Height),
Color.Yellow); //畫出打擊坦克數
spriteBatch.DrawString(font,"進攻坦克數 : " + miss.ToString() + "台"
,Vector2.Zero,Color.Red); //畫出進攻坦克數
if (lostGame==true)
spriteBatch.DrawString(font, //*失敗條件及字幕出現位置*
" 超過五台坦克你失敗了!",new Vector2(250,250),Color.Black);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
----------------------------------------------------------------------------------------
完成圖~
中文字~