2009年9月25日 星期五

3D座標轉2D座標

3D轉2D?聽起來很簡單。可是發現裡面牽涉一堆陣列的時候頭就痛
這是研究結果
Vector3 screenSpace = graphics.GraphicsDevice.Viewport.Project(
Vector3.Zero, cameraProjectionMaxtrix, cameraViewMaxtrix, new Matrix(0.1f, 0.0f, 0.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.0f, 0.0f, 0.0f, 0.1f, 0.0f
, ship.position.X, ship.position.Y, ship.position.Z, 1.0f));

座標就在這裡出現
screenSpace.X ←怪物的X座標
screenSpace.Y ←怪物的Y座標

利用此座標,在怪物或是機械死亡的瞬間,繪出爆炸效果,就是爆破了

2009年9月22日 星期二

XNA畫線


介紹一下XNA劃線功能,vb劃線很簡單 System.DrawLine(參數自己查...)
,可是XNA卻搞一堆鬼東西,明明號稱『123,站著寫真簡單』,真是不簡單
不廢話,以下為source code 自己抄
namespace DrawLine1
{
///
/// This is the main type for your game
///

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;


VertexPositionNormalTexture[] line; // Our start and end points.
Matrix world, projection, view; // Transformation Matrices
BasicEffect basicEffect; // Standard Effect to draw with
VertexDeclaration vertexDeclaration; // Our format for the line.
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

///
/// 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.
///

protected override void Initialize()
{
// TODO: Add your initialization logic here
InitializeTransforms();
InitializeBasicEffect();
base.Initialize();
}
private void InitializeTransforms()
{
world = Matrix.CreateTranslation(new Vector3(-1.5f, -0.5f, 0.0f));
view = Matrix.CreateLookAt(
new Vector3(0.0f, 0.0f, 7.0f),
new Vector3(0.0f, 0.0f, 0.0f),
Vector3.Up
);
projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45),
(float)graphics.GraphicsDevice.Viewport.Width /
(float)graphics.GraphicsDevice.Viewport.Height,
1.0f, 100.0f);
}
///
/// Setup up the required effect so we can see our scene
///

private void InitializeBasicEffect()
{
// Not part of the Effect per se, but is required for drawing
vertexDeclaration = new VertexDeclaration(
graphics.GraphicsDevice,
VertexPositionNormalTexture.VertexElements
);
basicEffect = new BasicEffect(graphics.GraphicsDevice, null);
basicEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);
basicEffect.World = world;
basicEffect.View = view;
basicEffect.Projection = projection;
}
///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

// TODO: use this.Content to load your game content here
}

///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///

protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///

/// Provides a snapshot of timing values.
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}
private void DrawLine()
{
line = new VertexPositionNormalTexture[2];
line[0] = new VertexPositionNormalTexture(new Vector3(0, 0, 0),
Vector3.Forward,
Vector2.One
);
line[1] = new VertexPositionNormalTexture(new Vector3(0, 1, 0),
Vector3.Forward,
Vector2.One
);
graphics.GraphicsDevice.DrawUserIndexedPrimitives(
PrimitiveType.LineList,
line,
0, // vertex buffer offset to add to each element of the index buffer
2, // number of vertices in pointList
new short[2] { 0, 1 }, // the index buffer
0, // first index element to read
1 // number of primitives to draw
);
}
///
/// This is called when the game should draw itself.
///

/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration;
// The effect is a compiled effect created and compiled elsewhere
// in the application.
basicEffect.Begin();
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Begin();
DrawLine();
pass.End();
}
basicEffect.End();
// TODO: Add your drawing code here

base.Draw(gameTime);
}
}
}

2009年9月14日 星期一

打飛碟









目前進度:
機關槍
打到會爆炸
用滑鼠瞄準目標
滑鼠滾輪縮放
標示目前使用的武器
武器攻擊力、速度、冷卻時間設定
敵人爆炸會振動
進度棒
切換武器按鈕
未完成:
主選單
劇情
增加子砲塔


ps.為什麼我的工作不是寫遊戲。。。。

2009年9月10日 星期四

XNA小塔射飛碟(VI) 發射子彈時播放聲音



//Game1.cs
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;

namespace BG_3D
{
///
/// This is the main type for your game
///

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

GameObject terrain = new GameObject();
GameObject missileLauncherBase = new GameObject();
GameObject missileLauncherHead = new GameObject();

const int numMissiles = 2000;
GameObject[] missiles;

GamePadState previousState;
#if !XBOX
KeyboardState previousKeyboardState;
#endif

const float launcherHeadMuzzleOffset = 20.0f;
const float missilePower = 20.0f;

AudioEngine audioEngine;
SoundBank soundBank;
WaveBank waveBank;

Vector3 cameraPosition = new Vector3(0.0f, 60.0f, 160.0f);
Vector3 cameraLookAt = new Vector3(0.0f, 50.0f, 0.0f);
Matrix cameraProjectionMatrix;
Matrix cameraViewMatrix;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

///
/// 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.
///

protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}

///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

audioEngine = new AudioEngine("Content\\Audio\\TutorialAudio.xgs");
waveBank = new WaveBank(audioEngine, "Content\\Audio\\Wave Bank.xwb");
soundBank = new SoundBank(audioEngine, "Content\\Audio\\Sound Bank.xsb");

cameraViewMatrix = Matrix.CreateLookAt(
cameraPosition,
cameraLookAt,
Vector3.Up);

cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f),
graphics.GraphicsDevice.Viewport.AspectRatio,
1.0f,
10000.0f);

terrain.model = Content.Load(
"Models\\terrain");

missileLauncherBase.model = Content.Load(
"Models\\launcher_base");
missileLauncherBase.scale = 0.2f;

missileLauncherHead.model = Content.Load(
"Models\\launcher_head");
missileLauncherHead.scale = 0.2f;
missileLauncherHead.position = missileLauncherBase.position +
new Vector3(0.0f, 20.0f, 0.0f);

missiles = new GameObject[numMissiles];
for (int i = 0; i < numMissiles; i++)
{
missiles[i] = new GameObject();
missiles[i].model =
Content.Load("Models\\missile");
missiles[i].scale = 3.0f;
}

// TODO: use this.Content to load your game content here
}

///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///

protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///

/// Provides a snapshot of timing values.
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);

missileLauncherHead.rotation.Y -=
gamePadState.ThumbSticks.Left.X * 0.1f;

missileLauncherHead.rotation.X +=
gamePadState.ThumbSticks.Left.Y * 0.1f;

#if !XBOX
KeyboardState keyboardState = Keyboard.GetState();
if(keyboardState.IsKeyDown(Keys.Left))
{
missileLauncherHead.rotation.Y += 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Right))
{
missileLauncherHead.rotation.Y -= 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Up))
{
missileLauncherHead.rotation.X += 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Down))
{
missileLauncherHead.rotation.X -= 0.05f;
}
#endif

missileLauncherHead.rotation.Y = MathHelper.Clamp(
missileLauncherHead.rotation.Y,
-MathHelper.PiOver4, MathHelper.PiOver4);

missileLauncherHead.rotation.X = MathHelper.Clamp(
missileLauncherHead.rotation.X,
0, MathHelper.PiOver4);

if (gamePadState.Buttons.A == ButtonState.Pressed &&
previousState.Buttons.A == ButtonState.Released)
{
FireMissile();
}

#if !XBOX
//if(keyboardState.IsKeyDown(Keys.Space) &&
// previousKeyboardState.IsKeyUp(Keys.Space))
if(keyboardState.IsKeyDown(Keys.Space))
{
FireMissile();
}
#endif

UpdateMissiles();

audioEngine.Update();

previousState = gamePadState;
#if !XBOX
previousKeyboardState = keyboardState;
#endif

// TODO: Add your update logic here

base.Update(gameTime);
}

void FireMissile()
{
foreach (GameObject missile in missiles)
{
if (!missile.alive)
{
soundBank.PlayCue("missilelaunch");

missile.velocity = GetMissileMuzzleVelocity();
missile.position = GetMissileMuzzlePosition();
missile.rotation = missileLauncherHead.rotation;
missile.alive = true;
break;
}
}
}

Vector3 GetMissileMuzzleVelocity()
{
Matrix rotationMatrix =
Matrix.CreateFromYawPitchRoll(
missileLauncherHead.rotation.Y,
missileLauncherHead.rotation.X,
0);

return Vector3.Normalize(
Vector3.Transform(Vector3.Forward,
rotationMatrix)) * missilePower;
}

Vector3 GetMissileMuzzlePosition()
{
return missileLauncherHead.position +
(Vector3.Normalize(
GetMissileMuzzleVelocity()) *
launcherHeadMuzzleOffset);
}

void UpdateMissiles()
{
foreach (GameObject missile in missiles)
{
if (missile.alive)
{
missile.position += missile.velocity;
if (missile.position.Z < -1000.0f)
{
missile.alive = false;
}
}
}
}

///
/// This is called when the game should draw itself.
///

/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

DrawGameObject(terrain);
DrawGameObject(missileLauncherBase);
DrawGameObject(missileLauncherHead);

foreach (GameObject missile in missiles)
{
if (missile.alive)
{
DrawGameObject(missile);
}
}

// TODO: Add your drawing code here

base.Draw(gameTime);
}

void DrawGameObject(GameObject gameobject)
{
foreach (ModelMesh mesh in gameobject.model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;

effect.World =
Matrix.CreateFromYawPitchRoll(
gameobject.rotation.Y,
gameobject.rotation.X,
gameobject.rotation.Z) *

Matrix.CreateScale(gameobject.scale) *

Matrix.CreateTranslation(gameobject.position);

effect.Projection = cameraProjectionMatrix;
effect.View = cameraViewMatrix;
}
mesh.Draw();
}
}
}
}

//GameObject.cs
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;

namespace BG_3D
{
///
/// This is the main type for your game
///

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

GameObject terrain = new GameObject();
GameObject missileLauncherBase = new GameObject();
GameObject missileLauncherHead = new GameObject();

const int numMissiles = 2000;
GameObject[] missiles;

GamePadState previousState;
#if !XBOX
KeyboardState previousKeyboardState;
#endif

const float launcherHeadMuzzleOffset = 20.0f;
const float missilePower = 20.0f;

AudioEngine audioEngine;
SoundBank soundBank;
WaveBank waveBank;

Vector3 cameraPosition = new Vector3(0.0f, 60.0f, 160.0f);
Vector3 cameraLookAt = new Vector3(0.0f, 50.0f, 0.0f);
Matrix cameraProjectionMatrix;
Matrix cameraViewMatrix;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

///
/// 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.
///

protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}

///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

audioEngine = new AudioEngine("Content\\Audio\\TutorialAudio.xgs");
waveBank = new WaveBank(audioEngine, "Content\\Audio\\Wave Bank.xwb");
soundBank = new SoundBank(audioEngine, "Content\\Audio\\Sound Bank.xsb");

cameraViewMatrix = Matrix.CreateLookAt(
cameraPosition,
cameraLookAt,
Vector3.Up);

cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f),
graphics.GraphicsDevice.Viewport.AspectRatio,
1.0f,
10000.0f);

terrain.model = Content.Load(
"Models\\terrain");

missileLauncherBase.model = Content.Load(
"Models\\launcher_base");
missileLauncherBase.scale = 0.2f;

missileLauncherHead.model = Content.Load(
"Models\\launcher_head");
missileLauncherHead.scale = 0.2f;
missileLauncherHead.position = missileLauncherBase.position +
new Vector3(0.0f, 20.0f, 0.0f);

missiles = new GameObject[numMissiles];
for (int i = 0; i < numMissiles; i++)
{
missiles[i] = new GameObject();
missiles[i].model =
Content.Load("Models\\missile");
missiles[i].scale = 3.0f;
}

// TODO: use this.Content to load your game content here
}

///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///

protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///

/// Provides a snapshot of timing values.
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);

missileLauncherHead.rotation.Y -=
gamePadState.ThumbSticks.Left.X * 0.1f;

missileLauncherHead.rotation.X +=
gamePadState.ThumbSticks.Left.Y * 0.1f;

#if !XBOX
KeyboardState keyboardState = Keyboard.GetState();
if(keyboardState.IsKeyDown(Keys.Left))
{
missileLauncherHead.rotation.Y += 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Right))
{
missileLauncherHead.rotation.Y -= 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Up))
{
missileLauncherHead.rotation.X += 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Down))
{
missileLauncherHead.rotation.X -= 0.05f;
}
#endif

missileLauncherHead.rotation.Y = MathHelper.Clamp(
missileLauncherHead.rotation.Y,
-MathHelper.PiOver4, MathHelper.PiOver4);

missileLauncherHead.rotation.X = MathHelper.Clamp(
missileLauncherHead.rotation.X,
0, MathHelper.PiOver4);

if (gamePadState.Buttons.A == ButtonState.Pressed &&
previousState.Buttons.A == ButtonState.Released)
{
FireMissile();
}

#if !XBOX
//if(keyboardState.IsKeyDown(Keys.Space) &&
// previousKeyboardState.IsKeyUp(Keys.Space))
if(keyboardState.IsKeyDown(Keys.Space))
{
FireMissile();
}
#endif

UpdateMissiles();

audioEngine.Update();

previousState = gamePadState;
#if !XBOX
previousKeyboardState = keyboardState;
#endif

// TODO: Add your update logic here

base.Update(gameTime);
}

void FireMissile()
{
foreach (GameObject missile in missiles)
{
if (!missile.alive)
{
soundBank.PlayCue("missilelaunch");

missile.velocity = GetMissileMuzzleVelocity();
missile.position = GetMissileMuzzlePosition();
missile.rotation = missileLauncherHead.rotation;
missile.alive = true;
break;
}
}
}

Vector3 GetMissileMuzzleVelocity()
{
Matrix rotationMatrix =
Matrix.CreateFromYawPitchRoll(
missileLauncherHead.rotation.Y,
missileLauncherHead.rotation.X,
0);

return Vector3.Normalize(
Vector3.Transform(Vector3.Forward,
rotationMatrix)) * missilePower;
}

Vector3 GetMissileMuzzlePosition()
{
return missileLauncherHead.position +
(Vector3.Normalize(
GetMissileMuzzleVelocity()) *
launcherHeadMuzzleOffset);
}

void UpdateMissiles()
{
foreach (GameObject missile in missiles)
{
if (missile.alive)
{
missile.position += missile.velocity;
if (missile.position.Z < -1000.0f)
{
missile.alive = false;
}
}
}
}

///
/// This is called when the game should draw itself.
///

/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

DrawGameObject(terrain);
DrawGameObject(missileLauncherBase);
DrawGameObject(missileLauncherHead);

foreach (GameObject missile in missiles)
{
if (missile.alive)
{
DrawGameObject(missile);
}
}

// TODO: Add your drawing code here

base.Draw(gameTime);
}

void DrawGameObject(GameObject gameobject)
{
foreach (ModelMesh mesh in gameobject.model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;

effect.World =
Matrix.CreateFromYawPitchRoll(
gameobject.rotation.Y,
gameobject.rotation.X,
gameobject.rotation.Z) *

Matrix.CreateScale(gameobject.scale) *

Matrix.CreateTranslation(gameobject.position);

effect.Projection = cameraProjectionMatrix;
effect.View = cameraViewMatrix;
}
mesh.Draw();
}
}
}
}

//Program.cs
using System;

namespace BG_3D
{
static class Program
{
///
/// The main entry point for the application.
///

static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
}

XNA小塔射飛碟(V) 發射子彈



//Game1.cs
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;

namespace BG_3D
{
///
/// This is the main type for your game
///

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

GameObject terrain = new GameObject();
GameObject missileLauncherBase = new GameObject();
GameObject missileLauncherHead = new GameObject();

const int numMissiles = 2000;
GameObject[] missiles;

GamePadState previousState;
#if !XBOX
KeyboardState previousKeyboardState;
#endif

const float launcherHeadMuzzleOffset = 20.0f;
const float missilePower = 20.0f;

Vector3 cameraPosition = new Vector3(0.0f, 60.0f, 160.0f);
Vector3 cameraLookAt = new Vector3(0.0f, 50.0f, 0.0f);
Matrix cameraProjectionMatrix;
Matrix cameraViewMatrix;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

///
/// 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.
///

protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}

///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

cameraViewMatrix = Matrix.CreateLookAt(
cameraPosition,
cameraLookAt,
Vector3.Up);

cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f),
graphics.GraphicsDevice.Viewport.AspectRatio,
1.0f,
10000.0f);

terrain.model = Content.Load(
"Models\\terrain");

missileLauncherBase.model = Content.Load(
"Models\\launcher_base");
missileLauncherBase.scale = 0.2f;

missileLauncherHead.model = Content.Load(
"Models\\launcher_head");
missileLauncherHead.scale = 0.2f;
missileLauncherHead.position = missileLauncherBase.position +
new Vector3(0.0f, 20.0f, 0.0f);

missiles = new GameObject[numMissiles];
for (int i = 0; i < numMissiles; i++)
{
missiles[i] = new GameObject();
missiles[i].model =
Content.Load("Models\\missile");
missiles[i].scale = 3.0f;
}

// TODO: use this.Content to load your game content here
}

///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///

protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///

/// Provides a snapshot of timing values.
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);

missileLauncherHead.rotation.Y -=
gamePadState.ThumbSticks.Left.X * 0.1f;

missileLauncherHead.rotation.X +=
gamePadState.ThumbSticks.Left.Y * 0.1f;

#if !XBOX
KeyboardState keyboardState = Keyboard.GetState();
if(keyboardState.IsKeyDown(Keys.Left))
{
missileLauncherHead.rotation.Y += 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Right))
{
missileLauncherHead.rotation.Y -= 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Up))
{
missileLauncherHead.rotation.X += 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Down))
{
missileLauncherHead.rotation.X -= 0.05f;
}
#endif

missileLauncherHead.rotation.Y = MathHelper.Clamp(
missileLauncherHead.rotation.Y,
-MathHelper.PiOver4, MathHelper.PiOver4);

missileLauncherHead.rotation.X = MathHelper.Clamp(
missileLauncherHead.rotation.X,
0, MathHelper.PiOver4);

if (gamePadState.Buttons.A == ButtonState.Pressed &&
previousState.Buttons.A == ButtonState.Released)

{
FireMissile();
}

#if !XBOX
//if(keyboardState.IsKeyDown(Keys.Space) &&
// previousKeyboardState.IsKeyUp(Keys.Space))
if (keyboardState.IsKeyDown(Keys.Space))
{
FireMissile();
}
#endif

UpdateMissiles();

previousState = gamePadState;
#if !XBOX
previousKeyboardState = keyboardState;
#endif

// TODO: Add your update logic here

base.Update(gameTime);
}

void FireMissile()
{
foreach (GameObject missile in missiles)
{
if (!missile.alive)
{
missile.velocity = GetMissileMuzzleVelocity();
missile.position = GetMissileMuzzlePosition();
missile.rotation = missileLauncherHead.rotation;
missile.alive = true;
break;
}
}
}

Vector3 GetMissileMuzzleVelocity()
{
Matrix rotationMatrix =
Matrix.CreateFromYawPitchRoll(
missileLauncherHead.rotation.Y,
missileLauncherHead.rotation.X,
0);

return Vector3.Normalize(
Vector3.Transform(Vector3.Forward,
rotationMatrix)) * missilePower;
}

Vector3 GetMissileMuzzlePosition()
{
return missileLauncherHead.position +
(Vector3.Normalize(
GetMissileMuzzleVelocity()) *
launcherHeadMuzzleOffset);
}

void UpdateMissiles()
{
foreach (GameObject missile in missiles)
{
if (missile.alive)
{
missile.position += missile.velocity;
if (missile.position.Z < -1000.0f)
{
missile.alive = false;

}
}
}
}

///
/// This is called when the game should draw itself.
///

/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

DrawGameObject(terrain);
DrawGameObject(missileLauncherBase);
DrawGameObject(missileLauncherHead);

foreach (GameObject missile in missiles)
{
if (missile.alive)
{
DrawGameObject(missile);
}
}

// TODO: Add your drawing code here

base.Draw(gameTime);
}

void DrawGameObject(GameObject gameobject)
{
foreach (ModelMesh mesh in gameobject.model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;

effect.World =
Matrix.CreateFromYawPitchRoll(
gameobject.rotation.Y,
gameobject.rotation.X,
gameobject.rotation.Z) *

Matrix.CreateScale(gameobject.scale) *

Matrix.CreateTranslation(gameobject.position);

effect.Projection = cameraProjectionMatrix;
effect.View = cameraViewMatrix;
}
mesh.Draw();
}
}
}
}

//GameObject.cs
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;

namespace BG_3D
{
class GameObject
{
public Model model = null;
public Vector3 position = Vector3.Zero;
public Vector3 rotation = Vector3.Zero;
public float scale = 1.0f;
public Vector3 velocity = Vector3.Zero;
public bool alive = false;
}
}

//Program.cs
using System;

namespace BG_3D
{
static class Program
{
///
/// The main entry point for the application.
///

static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
}

XNA小塔射飛碟(IV) 把砲塔的頭加上去



//Game1.cs檔案
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;

namespace BG_3D
{
///
/// This is the main type for your game
///

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

GameObject terrain = new GameObject();
GameObject missileLauncherBase = new GameObject();
GameObject missileLauncherHead = new GameObject();

Vector3 cameraPosition = new Vector3(0.0f, 60.0f, 160.0f);
Vector3 cameraLookAt = new Vector3(0.0f, 50.0f, 0.0f);
Matrix cameraProjectionMatrix;
Matrix cameraViewMatrix;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

///
/// 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.
///

protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}

///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

cameraViewMatrix = Matrix.CreateLookAt(
cameraPosition,
cameraLookAt,
Vector3.Up);

cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f),
graphics.GraphicsDevice.Viewport.AspectRatio,
1.0f,
10000.0f);

terrain.model = Content.Load(
"Models\\terrain");

missileLauncherBase.model = Content.Load(
"Models\\launcher_base");
missileLauncherBase.scale = 0.2f;

missileLauncherHead.model = Content.Load(
"Models\\launcher_head");
missileLauncherHead.scale = 0.2f;
missileLauncherHead.position =
missileLauncherBase.position +
new Vector3(0.0f, 20.0f, 0.0f);

// TODO: use this.Content to load your game content here
}

///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///

protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///

/// Provides a snapshot of timing values.
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);

missileLauncherHead.rotation.Y -=
gamePadState.ThumbSticks.Left.X * 0.1f;

missileLauncherHead.rotation.X +=
gamePadState.ThumbSticks.Left.Y * 0.1f;

#if !XBOX
KeyboardState keyboardState = Keyboard.GetState();

if(keyboardState.IsKeyDown(Keys.Left))
{
missileLauncherHead.rotation.Y += 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Right))
{
missileLauncherHead.rotation.Y -= 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Up))
{
missileLauncherHead.rotation.X += 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Down))
{
missileLauncherHead.rotation.X -= 0.05f;
}
#endif

missileLauncherHead.rotation.Y = MathHelper.Clamp(
missileLauncherHead.rotation.Y,
-MathHelper.PiOver4, MathHelper.PiOver4);

missileLauncherHead.rotation.X = MathHelper.Clamp(
missileLauncherHead.rotation.X,
0, MathHelper.PiOver4);

// TODO: Add your update logic here

base.Update(gameTime);
}

///
/// This is called when the game should draw itself.
///

/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

DrawGameObject(terrain);
DrawGameObject(missileLauncherBase);
DrawGameObject(missileLauncherHead);

// TODO: Add your drawing code here

base.Draw(gameTime);
}

void DrawGameObject(GameObject gameobject)
{
foreach (ModelMesh mesh in gameobject.model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;

effect.World =
Matrix.CreateFromYawPitchRoll(
gameobject.rotation.Y,
gameobject.rotation.X,
gameobject.rotation.Z) *

Matrix.CreateScale(gameobject.scale) *

Matrix.CreateTranslation(gameobject.position);

effect.Projection = cameraProjectionMatrix;
effect.View = cameraViewMatrix;
}
mesh.Draw();
}
}
}
}

//GameObject.cs
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;

namespace BG_3D
{
class GameObject
{
public Model model = null;
public Vector3 position = Vector3.Zero;
public Vector3 rotation = Vector3.Zero;
public float scale = 1.0f;
}
}

//Program.cs
using System;

namespace BG_3D
{
static class Program
{
///
/// The main entry point for the application.
///

static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
}

XNA小塔射飛碟(III) 建立砲塔塔底



//Game1.cs 檔案
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;

namespace BG_3D
{
///
/// This is the main type for your game
///

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

GameObject terrain = new GameObject();
GameObject missileLauncherBase = new GameObject();

Vector3 cameraPosition = new Vector3(0.0f, 60.0f, 160.0f);
Vector3 cameraLookAt = new Vector3(0.0f, 50.0f, 0.0f);
Matrix cameraProjectionMatrix;
Matrix cameraViewMatrix;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

///
/// 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.
///

protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}

///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

cameraViewMatrix = Matrix.CreateLookAt(
cameraPosition,
cameraLookAt,
Vector3.Up);

cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f),
graphics.GraphicsDevice.Viewport.AspectRatio,
1.0f,
10000.0f);

terrain.model = Content.Load(
"Models\\terrain");

missileLauncherBase.model = Content.Load(
"Models\\launcher_base");
missileLauncherBase.scale = 0.2f;

// TODO: use this.Content to load your game content here
}

///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///

protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///

/// Provides a snapshot of timing values.
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}

///
/// This is called when the game should draw itself.
///

/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

DrawGameObject(terrain);
DrawGameObject(missileLauncherBase);

// TODO: Add your drawing code here

base.Draw(gameTime);
}

void DrawGameObject(GameObject gameobject)
{
foreach (ModelMesh mesh in gameobject.model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;

effect.World =
Matrix.CreateFromYawPitchRoll(
gameobject.rotation.Y,
gameobject.rotation.X,
gameobject.rotation.Z) *

Matrix.CreateScale(gameobject.scale) *

Matrix.CreateTranslation(gameobject.position);

effect.Projection = cameraProjectionMatrix;
effect.View = cameraViewMatrix;
}
mesh.Draw();
}
}
}
}

//GameObject.cs檔案
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;

namespace BG_3D
{
class GameObject
{
public Model model = null;
public Vector3 position = Vector3.Zero;
public Vector3 rotation = Vector3.Zero;
public float scale = 1.0f;
}
}
//Program.cs檔案
using System;

namespace BG_3D
{
static class Program
{
///
/// The main entry point for the application.
///

static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
}

XNA小塔射飛碟(II) 建立背景


//Game1.cs檔案

using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;

namespace BG_3D
{
///
/// This is the main type for your game
///

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Model terrainModel;
Vector3 terrainPosition = Vector3.Zero;

Vector3 cameraPosition = new Vector3(0.0f, 40.0f, 160.0f);
Vector3 cameraLookAt = new Vector3(0.0f, 8.0f, 0.0f);
Matrix cameraProjectionMatrix;
Matrix cameraViewMatrix;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

///
/// 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.
///

protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}

///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

cameraViewMatrix = Matrix.CreateLookAt(
cameraPosition,
cameraLookAt,
Vector3.Up);

cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f),
graphics.GraphicsDevice.Viewport.AspectRatio,
1.0f,
10000.0f);

terrainModel = Content.Load("Models\\terrain");

// TODO: use this.Content to load your game content here
}

///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///

protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///

/// Provides a snapshot of timing values.
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}

///
/// This is called when the game should draw itself.
///

/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

DrawModel(terrainModel, terrainPosition);

// TODO: Add your drawing code here

base.Draw(gameTime);
}

void DrawModel(Model model, Vector3 modelPosition)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;

effect.World = Matrix.CreateTranslation(modelPosition);
effect.Projection = cameraProjectionMatrix;
effect.View = cameraViewMatrix;
}
mesh.Draw();
}
}
}
}
//Program.cs檔案
namespace BG_3D
{
static class Program
{
///
/// The main entry point for the application.
///

static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
}

2009年9月9日 星期三

XNA小塔射飛碟(I) 建立程式


//Game1.cs檔案
namespace BG_3D
{
///
/// This is the main type for your game
///

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;


public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

///
/// 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.
///

protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}

///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

// TODO: use this.Content to load your game content here
}

///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///

protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///

/// Provides a snapshot of timing values.
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}

///
/// This is called when the game should draw itself.
///

/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here

base.Draw(gameTime);
}


}
}

//Program.cs檔案
namespace BG_3D
{
static class Program
{
///
/// The main entry point for the application.
///

static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
}

2009年9月4日 星期五

vb.net 表單的focus狀態

Protected Overloads Overrides Sub WndProc(ByRef m As Message)

if m.Msg= 6 //表單狀態
if m.WParam.ToInt32() = 1//表單建立

end if
if m.WParam.ToInt32() = 2//表單從後面移到前面

end if
if m.WParam.ToInt32() = 2097152//表單縮到最小
end if
if m.WParam.ToInt32() = 0 //表單從前面移到後面
end if
end if
MyBase.WndProc(m)
End Sub