連結~
-------------------------------------------------------------------------------------------------------------
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 WindowsGame6
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GameObject topWall;
GameObject bottomWall;
GameObject playerOne;
GameObject playerTwo;
GameObject ball;
KeyboardState keyboardState;
GameObject ufo;//加入飛碟
GameObject ufo2;//加入飛碟
GameObject ufo3;//加入飛碟
GameObject ufo4;//加入飛碟
GameObject ufo5;//加入飛碟
GameObject ufo6;//加入飛碟
GameObject rightWall;//右邊的牆
GameObject wlr;//右擋牆
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);
Texture2D wallTexture = Content.Load<Texture2D>("wall");
topWall = new GameObject(
wallTexture,
Vector2.Zero);
bottomWall = new GameObject(
wallTexture,
new Vector2(0, Window.ClientBounds.Height - wallTexture.Height));
Texture2D paddleTexture = Content.Load<Texture2D>("paddle");
Vector2 position;
position = new Vector2(
0,
(Window.ClientBounds.Height - paddleTexture.Height) / 2);
playerOne = new GameObject(paddleTexture, position);
position = new Vector2(
(Window.ClientBounds.Width - paddleTexture.Width),
(Window.ClientBounds.Height - paddleTexture.Height) / 2);
playerTwo = new GameObject(paddleTexture, position);
/*
Texture2D ballTexture = Content.Load<Texture2D>("ball");
position = new Vector2(
(Window.ClientBounds.Width / 2 - 2 * wallTexture.Height),
Window.ClientBounds.Height - wallTexture.Height);
ball = new GameObject(
ballTexture,
position,
new Vector2(6f, -6f));
*/
Texture2D ballTexture = Content.Load<Texture2D>("ball");
position = new Vector2(
playerOne.BoundingBox.Right + 1,
(Window.ClientBounds.Height - ballTexture.Height) / 2);
ball = new GameObject(
ballTexture,
position,
new Vector2(10f, 1f));//new Vector2(8f, -8f));本來是
Texture2D ufoTexture = Content.Load<Texture2D>("enemy");//加入飛碟
ufo = new GameObject(ufoTexture, new Vector2(350, (Window.ClientBounds.Height - wallTexture.Height) / 2));//加入飛碟
ufo2 = new GameObject(ufoTexture, new Vector2(150, (Window.ClientBounds.Height - wallTexture.Height) / 2));//加入飛碟
ufo3 = new GameObject(ufoTexture, new Vector2(550, (Window.ClientBounds.Height - wallTexture.Height) / 2));//加入飛碟
ufo4 = new GameObject(ufoTexture, new Vector2(350, (Window.ClientBounds.Height - wallTexture.Height) / 3));//加入飛碟
ufo5 = new GameObject(ufoTexture, new Vector2(150, (Window.ClientBounds.Height - wallTexture.Height) / 3));//加入飛碟
ufo6 = new GameObject(ufoTexture, new Vector2(550, (Window.ClientBounds.Height - wallTexture.Height) / 3));//加入飛碟
rightWall = new GameObject(wallTexture,
new Vector2(-300, 400));//右牆
Texture2D wlrTexture = Content.Load<Texture2D>("wlr");//右擋牆
wlr = new GameObject(
wlrTexture,
new Vector2((Window.ClientBounds.Width - wlrTexture.Width),
(Window.ClientBounds.Height - wlrTexture.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
}
/// <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();
ball.Position += ball.Velocity;
keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.W))
playerOne.Position.Y -= 10f;
if (keyboardState.IsKeyDown(Keys.S))
playerOne.Position.Y += 10f;
if (keyboardState.IsKeyDown(Keys.Up))
playerTwo.Position.Y -= 10f;
if (keyboardState.IsKeyDown(Keys.Down))
playerTwo.Position.Y += 10f;
CheckPaddleWallCollision();
CheckBallCollision();
base.Update(gameTime);
}
private void CheckBallCollision()
{
if (ball.BoundingBox.Intersects(topWall.BoundingBox))
{
ball.Velocity.Y *= -1;
ball.Position += ball.Velocity;
}
if (ball.BoundingBox.Intersects(bottomWall.BoundingBox))
{
ball.Velocity.Y *= -1;
ball.Position += ball.Velocity;
}
if (ball.BoundingBox.Intersects(playerOne.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
if (ball.BoundingBox.Intersects(playerTwo.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
if ((ball.Position.X < -ball.BoundingBox.Width)
|| (ball.Position.X > Window.ClientBounds.Width))
SetInStartPostion();
//飛碟反彈
if (ball.BoundingBox.Intersects(ufo.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
if (ball.BoundingBox.Intersects(ufo.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
//飛碟反彈
//飛碟反彈
if (ball.BoundingBox.Intersects(ufo2.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
if (ball.BoundingBox.Intersects(ufo2.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
//飛碟反彈
//飛碟反彈
if (ball.BoundingBox.Intersects(ufo3.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
if (ball.BoundingBox.Intersects(ufo3.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
//飛碟反彈
//飛碟反彈
if (ball.BoundingBox.Intersects(ufo4.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
if (ball.BoundingBox.Intersects(ufo4.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
//飛碟反彈
//飛碟反彈
if (ball.BoundingBox.Intersects(ufo5.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
if (ball.BoundingBox.Intersects(ufo5.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
//飛碟反彈
//飛碟反彈
if (ball.BoundingBox.Intersects(ufo6.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
if (ball.BoundingBox.Intersects(ufo6.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Position += ball.Velocity;
}
//飛碟反彈
//右擋牆
if (ball.BoundingBox.Intersects(wlr.BoundingBox))
{
ball.Velocity.X *= -1;
}
//右擋牆
//右牆
if (ball.BoundingBox.Intersects(rightWall.BoundingBox))
{
ball.Velocity.X *= -1;
ball.Velocity.Y *= -1;
ball.Position += ball.Velocity;
}
//右牆
}
private void SetInStartPostion()
{
playerOne.Position.X = -10;
playerOne.Position.Y = 520;
/*
playerOne.Position.Y = (
Window.ClientBounds.Height -
playerOne.BoundingBox.Height) / 2;*/
playerTwo.Position.Y = (
Window.ClientBounds.Height -
playerTwo.BoundingBox.Height) / 2;
ball.Position.X = 0;
ball.Position.Y = 520;
/*
ball.Position.X = playerOne.BoundingBox.Right + 1;
ball.Position.Y = (
Window.ClientBounds.Height -
ball.BoundingBox.Height) / 2;
*/
ball.Velocity = new Vector2(10f, 1f);//ball.Velocity = new Vector2(8f, -8f);
}
void CheckPaddleWallCollision()
{
if (playerOne.BoundingBox.Intersects(topWall.BoundingBox))
playerOne.Position.Y = topWall.BoundingBox.Bottom;
if (playerOne.BoundingBox.Intersects(bottomWall.BoundingBox))
playerOne.Position.Y = bottomWall.BoundingBox.Y
- playerOne.BoundingBox.Height;
if (playerTwo.BoundingBox.Intersects(topWall.BoundingBox))
playerTwo.Position.Y = topWall.BoundingBox.Bottom;
if (playerTwo.BoundingBox.Intersects(bottomWall.BoundingBox))
playerTwo.Position.Y = bottomWall.BoundingBox.Y
- playerTwo.BoundingBox.Height;
//右擋牆
if (playerOne.BoundingBox.Intersects(wlr.BoundingBox))
playerOne.Position.Y = wlr.BoundingBox.Bottom;
//右擋牆
if (playerTwo.BoundingBox.Intersects(wlr.BoundingBox))
playerTwo.Position.Y = wlr.BoundingBox.Bottom;
}
/// <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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
topWall.Draw(spriteBatch);
bottomWall.Draw(spriteBatch);
playerOne.Draw(spriteBatch);
playerTwo.Draw(spriteBatch);
ball.Draw(spriteBatch);
ufo.Draw(spriteBatch);//加入飛碟
ufo2.Draw(spriteBatch);//加入飛碟
ufo3.Draw(spriteBatch);//加入飛碟
ufo4.Draw(spriteBatch);//加入飛碟
ufo5.Draw(spriteBatch);//加入飛碟
ufo6.Draw(spriteBatch);//加入飛碟
rightWall.Draw(spriteBatch);//右牆
wlr.Draw(spriteBatch);//右擋牆
spriteBatch.End();
base.Draw(gameTime);
}
}
}
VC++作業
2011年1月7日 星期五
2010年12月24日 星期五
99/12/24 飛機打飛機(由BG 3D改的)
飛機打飛機
-------------------------------------------------------------------------------------------------------------
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
{
/// <summary>
/// This is the main type for your game
/// </summary>
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 = 200;
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;
Random r = new Random();
const int numEnemyShips = 1;
GameObject[] enemyShips;
Vector3 shipMinPosition = new Vector3(-2000.0f, 300.0f, -4000.0f);
Vector3 shipMaxPosition = new Vector3(2000.0f, 800.0f, -6000.0f);
/*
Vector3 shipMinPosition = new Vector3(-2000.0f, 300.0f, -6000.0f);
Vector3 shipMaxPosition = new Vector3(2000.0f, 800.0f, -4000.0f);
*/
const float shipMinVelocity = 5.0f;
const float shipMaxVelocity = 10.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";
}
/// <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);
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<Model>(
"Models\\terrain");
missileLauncherBase.model = Content.Load<Model>(
"Models\\launcher_base");
missileLauncherBase.scale = 0.0000002f;/*missileLauncherBase.scale = 0.2f;*/
missileLauncherHead.model = Content.Load<Model>(
"Models\\p1_wedge");
missileLauncherHead.scale = 0.02f;
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<Model>("Models\\missile");
missiles[i].scale = 3.0f;
}
enemyShips = new GameObject[numEnemyShips];
for (int i = 0; i < numEnemyShips; i++)
{
enemyShips[i] = new GameObject();
enemyShips[i].model = Content.Load<Model>(
"Models\\p1_wedge");
enemyShips[i].scale = 0.1f;
enemyShips[i].rotation = new Vector3(
-600.0f, MathHelper.Pi, 600.0f);
/*enemyShips[i].rotation = new Vector3(
0.0f, MathHelper.Pi, 0.0f);*/
}
// TODO: use this.Content to load your game content here
}
/// <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
}
/// <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();
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))
{
FireMissile();
}
#endif
UpdateMissiles();
audioEngine.Update();
UpdateEnemyShips();
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 < -6000.0f)
{
missile.alive = false;
}
else
{
TestCollision(missile);
}
}
}
}
void UpdateEnemyShips()
{
foreach (GameObject ship in enemyShips)
{
if (ship.alive)
{
ship.position -= ship.velocity;
if (ship.position.Z < -5000.0f)/*if (ship.position.Z > 500.0f)*/
{
ship.alive = false;
}
}
else
{
ship.alive = true;
ship.position = new Vector3(
MathHelper.Lerp(
shipMinPosition.X,
shipMaxPosition.X,
(float)r.NextDouble()),
MathHelper.Lerp(
shipMinPosition.Y,
shipMaxPosition.Y,
(float)r.NextDouble()),
MathHelper.Lerp(
shipMinPosition.Z,
shipMaxPosition.Z,
(float)r.NextDouble()));
ship.velocity = new Vector3(
0.0f,
5.0f,
MathHelper.Lerp(shipMinVelocity,
shipMaxVelocity, (float)r.NextDouble()));
/*ship.velocity = new Vector3(
0.0f,
0.0f,
MathHelper.Lerp(shipMinVelocity,
shipMaxVelocity, (float)r.NextDouble()));
*/
}
}
}
void TestCollision(GameObject missile)
{
BoundingSphere missilesphere =
missile.model.Meshes[0].BoundingSphere;
missilesphere.Center = missile.position;
missilesphere.Radius *= missile.scale;
foreach (GameObject ship in enemyShips)
{
if (ship.alive)
{
BoundingSphere shipsphere =
ship.model.Meshes[0].BoundingSphere;
shipsphere.Center = ship.position;
shipsphere.Radius *= ship.scale;
if (shipsphere.Intersects(missilesphere))
{
soundBank.PlayCue("explosion");
missile.alive = false;
ship.alive = false;
break;
}
}
}
}
/// <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);
DrawGameObject(terrain);
DrawGameObject(missileLauncherBase);
DrawGameObject(missileLauncherHead);
foreach (GameObject missile in missiles)
{
if (missile.alive)
{
DrawGameObject(missile);
}
}
foreach (GameObject enemyship in enemyShips)
{
if (enemyship.alive)
{
DrawGameObject(enemyship);
}
}
// 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();
}
}
}
}
-------------------------------------------------------------------------------------------------------------
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
{
/// <summary>
/// This is the main type for your game
/// </summary>
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 = 200;
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;
Random r = new Random();
const int numEnemyShips = 1;
GameObject[] enemyShips;
Vector3 shipMinPosition = new Vector3(-2000.0f, 300.0f, -4000.0f);
Vector3 shipMaxPosition = new Vector3(2000.0f, 800.0f, -6000.0f);
/*
Vector3 shipMinPosition = new Vector3(-2000.0f, 300.0f, -6000.0f);
Vector3 shipMaxPosition = new Vector3(2000.0f, 800.0f, -4000.0f);
*/
const float shipMinVelocity = 5.0f;
const float shipMaxVelocity = 10.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";
}
/// <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);
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<Model>(
"Models\\terrain");
missileLauncherBase.model = Content.Load<Model>(
"Models\\launcher_base");
missileLauncherBase.scale = 0.0000002f;/*missileLauncherBase.scale = 0.2f;*/
missileLauncherHead.model = Content.Load<Model>(
"Models\\p1_wedge");
missileLauncherHead.scale = 0.02f;
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<Model>("Models\\missile");
missiles[i].scale = 3.0f;
}
enemyShips = new GameObject[numEnemyShips];
for (int i = 0; i < numEnemyShips; i++)
{
enemyShips[i] = new GameObject();
enemyShips[i].model = Content.Load<Model>(
"Models\\p1_wedge");
enemyShips[i].scale = 0.1f;
enemyShips[i].rotation = new Vector3(
-600.0f, MathHelper.Pi, 600.0f);
/*enemyShips[i].rotation = new Vector3(
0.0f, MathHelper.Pi, 0.0f);*/
}
// TODO: use this.Content to load your game content here
}
/// <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
}
/// <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();
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))
{
FireMissile();
}
#endif
UpdateMissiles();
audioEngine.Update();
UpdateEnemyShips();
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 < -6000.0f)
{
missile.alive = false;
}
else
{
TestCollision(missile);
}
}
}
}
void UpdateEnemyShips()
{
foreach (GameObject ship in enemyShips)
{
if (ship.alive)
{
ship.position -= ship.velocity;
if (ship.position.Z < -5000.0f)/*if (ship.position.Z > 500.0f)*/
{
ship.alive = false;
}
}
else
{
ship.alive = true;
ship.position = new Vector3(
MathHelper.Lerp(
shipMinPosition.X,
shipMaxPosition.X,
(float)r.NextDouble()),
MathHelper.Lerp(
shipMinPosition.Y,
shipMaxPosition.Y,
(float)r.NextDouble()),
MathHelper.Lerp(
shipMinPosition.Z,
shipMaxPosition.Z,
(float)r.NextDouble()));
ship.velocity = new Vector3(
0.0f,
5.0f,
MathHelper.Lerp(shipMinVelocity,
shipMaxVelocity, (float)r.NextDouble()));
/*ship.velocity = new Vector3(
0.0f,
0.0f,
MathHelper.Lerp(shipMinVelocity,
shipMaxVelocity, (float)r.NextDouble()));
*/
}
}
}
void TestCollision(GameObject missile)
{
BoundingSphere missilesphere =
missile.model.Meshes[0].BoundingSphere;
missilesphere.Center = missile.position;
missilesphere.Radius *= missile.scale;
foreach (GameObject ship in enemyShips)
{
if (ship.alive)
{
BoundingSphere shipsphere =
ship.model.Meshes[0].BoundingSphere;
shipsphere.Center = ship.position;
shipsphere.Radius *= ship.scale;
if (shipsphere.Intersects(missilesphere))
{
soundBank.PlayCue("explosion");
missile.alive = false;
ship.alive = false;
break;
}
}
}
}
/// <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);
DrawGameObject(terrain);
DrawGameObject(missileLauncherBase);
DrawGameObject(missileLauncherHead);
foreach (GameObject missile in missiles)
{
if (missile.alive)
{
DrawGameObject(missile);
}
}
foreach (GameObject enemyship in enemyShips)
{
if (enemyship.alive)
{
DrawGameObject(enemyship);
}
}
// 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();
}
}
}
}
2010年12月17日 星期五
2010年12月10日 星期五
99/12/10 2D射擊遊戲
連結
-------------------------------------------------------------------------------------------------------------
#region File Description
//-----------------------------------------------------------------------------
// Game1.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace BeginnersGuide_2DGame_Windows
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
Texture2D backgroundTexture;
Rectangle viewportRect;
SpriteBatch spriteBatch;
GameObject cannon;
const int maxCannonBalls = 50;
GameObject[] cannonBalls;
GamePadState previousGamePadState = GamePad.GetState(PlayerIndex.One);
KeyboardState previousKeyboardState = Keyboard.GetState();
const int maxEnemies = 10;
const float maxEnemyHeight = 0.1f;
const float minEnemyHeight = 0.5f;
const float maxEnemyVelocity = 5.0f;
const float minEnemyVelocity = 1.0f;
Random random = new Random();
GameObject[] enemies;
int score;
SpriteFont font;
Vector2 scoreDrawPoint = new Vector2(0.1f, 0.1f);
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()
{
base.Initialize();
}
/// <summary>
/// Load your graphics content
/// </summary>
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
backgroundTexture =
Content.Load<Texture2D>("Sprites\\bk");
cannon = new GameObject(Content.Load<Texture2D>("Sprites\\cannon"));
cannon.position = new Vector2(520, graphics.GraphicsDevice.Viewport.Height - 80);
cannonBalls = new GameObject[maxCannonBalls];
for (int i = 0; i < maxCannonBalls; i++)
{
cannonBalls[i] = new GameObject(Content.Load<Texture2D>(
"Sprites\\cannonball"));
}
enemies = new GameObject[maxEnemies];
for (int i = 0; i < maxEnemies; i++)
{
enemies[i] = new GameObject(
Content.Load<Texture2D>("Sprites\\f"));
}
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);
base.LoadContent();
}
public void UpdateCannonBalls()
{
foreach (GameObject ball in cannonBalls)
{
if (ball.alive)
{
ball.position += ball.velocity;
if (!viewportRect.Contains(new Point(
(int)ball.position.X,
(int)ball.position.Y)))
{
ball.alive = false;
continue;
}
Rectangle cannonBallRect = new Rectangle(
(int)ball.position.X,
(int)ball.position.Y,
ball.sprite.Width,
ball.sprite.Height);
foreach (GameObject enemy in enemies)
{
Rectangle enemyRect = new Rectangle(
(int)enemy.position.X,
(int)enemy.position.Y,
enemy.sprite.Width,
enemy.sprite.Height);
if (cannonBallRect.Intersects(enemyRect))
{
ball.alive = true;
enemy.alive = false;
score += 10;
break;
}
}
}
}
}
/// <summary>
/// Unload any content not managed by the Content Manager
/// </summary>
protected override void UnloadContent()
{
base.UnloadContent();
}
/// <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();
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
cannon.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))
{
cannon.rotation -= 0.1f;
}
if(keyboardState.IsKeyDown(Keys.Right))
{
cannon.rotation += 0.1f;
}
if (keyboardState.IsKeyDown(Keys.Space) &&
previousKeyboardState.IsKeyUp(Keys.Space))
{
FireCannonBall();
}
#endif
cannon.rotation = MathHelper.Clamp(cannon.rotation, -MathHelper.PiOver2, 0);
UpdateCannonBalls();
UpdateEnemies();
// TODO: Add your update logic here
previousGamePadState = gamePadState;
#if !XBOX
previousKeyboardState = keyboardState;
#endif
base.Update(gameTime);
}
public void UpdateEnemies()
{
foreach (GameObject enemy in enemies)
{
if (enemy.alive)
{
enemy.position += enemy.velocity;
if (!viewportRect.Contains(new Point(
(int)enemy.position.X,
(int)enemy.position.Y)))
{
enemy.alive = false;
}
}
else
{
enemy.alive = true;
enemy.position = new Vector2(
viewportRect.Right,
MathHelper.Lerp(
(float)viewportRect.Height * minEnemyHeight,
(float)viewportRect.Height * maxEnemyHeight,
(float)random.NextDouble()));
enemy.velocity = new Vector2(
MathHelper.Lerp(
-minEnemyVelocity,
-maxEnemyVelocity,
(float)random.NextDouble()), 0);
}
}
}
public void FireCannonBall()
{
foreach (GameObject ball in cannonBalls)
{
if (!ball.alive)
{
ball.alive = true;
ball.position = cannon.position - ball.center;
ball.velocity = new Vector2(
(float)Math.Cos(cannon.rotation),
(float)Math.Sin(cannon.rotation)) * 50.0f;
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);
//Draw the backgroundTexture sized to the width
//and height of the screen.
spriteBatch.Draw(backgroundTexture, viewportRect,
Color.White);
foreach (GameObject ball in cannonBalls)
{
if (ball.alive)
{
spriteBatch.Draw(ball.sprite,
ball.position, Color.White);
}
}
spriteBatch.Draw(cannon.sprite,
cannon.position,
null,
Color.Green,
cannon.rotation,
cannon.center, 1.7f,
SpriteEffects.None, 0);
foreach (GameObject enemy in enemies)
{
if (enemy.alive)
{
spriteBatch.Draw(enemy.sprite,
enemy.position, Color.White);
}
}
spriteBatch.DrawString(font,
"Score: " + score.ToString(),
new Vector2(scoreDrawPoint.X * viewportRect.Width,
scoreDrawPoint.Y * viewportRect.Height),
Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
-------------------------------------------------------------------------------------------------------------
#region File Description
//-----------------------------------------------------------------------------
// Game1.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace BeginnersGuide_2DGame_Windows
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
Texture2D backgroundTexture;
Rectangle viewportRect;
SpriteBatch spriteBatch;
GameObject cannon;
const int maxCannonBalls = 50;
GameObject[] cannonBalls;
GamePadState previousGamePadState = GamePad.GetState(PlayerIndex.One);
KeyboardState previousKeyboardState = Keyboard.GetState();
const int maxEnemies = 10;
const float maxEnemyHeight = 0.1f;
const float minEnemyHeight = 0.5f;
const float maxEnemyVelocity = 5.0f;
const float minEnemyVelocity = 1.0f;
Random random = new Random();
GameObject[] enemies;
int score;
SpriteFont font;
Vector2 scoreDrawPoint = new Vector2(0.1f, 0.1f);
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()
{
base.Initialize();
}
/// <summary>
/// Load your graphics content
/// </summary>
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
backgroundTexture =
Content.Load<Texture2D>("Sprites\\bk");
cannon = new GameObject(Content.Load<Texture2D>("Sprites\\cannon"));
cannon.position = new Vector2(520, graphics.GraphicsDevice.Viewport.Height - 80);
cannonBalls = new GameObject[maxCannonBalls];
for (int i = 0; i < maxCannonBalls; i++)
{
cannonBalls[i] = new GameObject(Content.Load<Texture2D>(
"Sprites\\cannonball"));
}
enemies = new GameObject[maxEnemies];
for (int i = 0; i < maxEnemies; i++)
{
enemies[i] = new GameObject(
Content.Load<Texture2D>("Sprites\\f"));
}
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);
base.LoadContent();
}
public void UpdateCannonBalls()
{
foreach (GameObject ball in cannonBalls)
{
if (ball.alive)
{
ball.position += ball.velocity;
if (!viewportRect.Contains(new Point(
(int)ball.position.X,
(int)ball.position.Y)))
{
ball.alive = false;
continue;
}
Rectangle cannonBallRect = new Rectangle(
(int)ball.position.X,
(int)ball.position.Y,
ball.sprite.Width,
ball.sprite.Height);
foreach (GameObject enemy in enemies)
{
Rectangle enemyRect = new Rectangle(
(int)enemy.position.X,
(int)enemy.position.Y,
enemy.sprite.Width,
enemy.sprite.Height);
if (cannonBallRect.Intersects(enemyRect))
{
ball.alive = true;
enemy.alive = false;
score += 10;
break;
}
}
}
}
}
/// <summary>
/// Unload any content not managed by the Content Manager
/// </summary>
protected override void UnloadContent()
{
base.UnloadContent();
}
/// <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();
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
cannon.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))
{
cannon.rotation -= 0.1f;
}
if(keyboardState.IsKeyDown(Keys.Right))
{
cannon.rotation += 0.1f;
}
if (keyboardState.IsKeyDown(Keys.Space) &&
previousKeyboardState.IsKeyUp(Keys.Space))
{
FireCannonBall();
}
#endif
cannon.rotation = MathHelper.Clamp(cannon.rotation, -MathHelper.PiOver2, 0);
UpdateCannonBalls();
UpdateEnemies();
// TODO: Add your update logic here
previousGamePadState = gamePadState;
#if !XBOX
previousKeyboardState = keyboardState;
#endif
base.Update(gameTime);
}
public void UpdateEnemies()
{
foreach (GameObject enemy in enemies)
{
if (enemy.alive)
{
enemy.position += enemy.velocity;
if (!viewportRect.Contains(new Point(
(int)enemy.position.X,
(int)enemy.position.Y)))
{
enemy.alive = false;
}
}
else
{
enemy.alive = true;
enemy.position = new Vector2(
viewportRect.Right,
MathHelper.Lerp(
(float)viewportRect.Height * minEnemyHeight,
(float)viewportRect.Height * maxEnemyHeight,
(float)random.NextDouble()));
enemy.velocity = new Vector2(
MathHelper.Lerp(
-minEnemyVelocity,
-maxEnemyVelocity,
(float)random.NextDouble()), 0);
}
}
}
public void FireCannonBall()
{
foreach (GameObject ball in cannonBalls)
{
if (!ball.alive)
{
ball.alive = true;
ball.position = cannon.position - ball.center;
ball.velocity = new Vector2(
(float)Math.Cos(cannon.rotation),
(float)Math.Sin(cannon.rotation)) * 50.0f;
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);
//Draw the backgroundTexture sized to the width
//and height of the screen.
spriteBatch.Draw(backgroundTexture, viewportRect,
Color.White);
foreach (GameObject ball in cannonBalls)
{
if (ball.alive)
{
spriteBatch.Draw(ball.sprite,
ball.position, Color.White);
}
}
spriteBatch.Draw(cannon.sprite,
cannon.position,
null,
Color.Green,
cannon.rotation,
cannon.center, 1.7f,
SpriteEffects.None, 0);
foreach (GameObject enemy in enemies)
{
if (enemy.alive)
{
spriteBatch.Draw(enemy.sprite,
enemy.position, Color.White);
}
}
spriteBatch.DrawString(font,
"Score: " + score.ToString(),
new Vector2(scoreDrawPoint.X * viewportRect.Width,
scoreDrawPoint.Y * viewportRect.Height),
Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
2010年11月26日 星期五
2010年11月25日 星期四
2010年11月12日 星期五
99/11/12期中考完成!!!!!!!!!!!
產生亂數(案件自己拉)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int i, bno;
int[] arry = new int[9];
public Form1()
{
InitializeComponent();
}
private void button8_Click(object sender, EventArgs e)
{
if (button4.Text == "0")
{
String temp;
temp = button4.Text;
button4.Text = button8.Text;
button8.Text = temp;
}
else if (button9.Text == "0")
{
String temp;
temp = button9.Text;
button9.Text = button8.Text;
button8.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button5_Click_1(object sender, EventArgs e)
{
int rdno;
int temp;
arry[0] = 0;
arry[1] = 1;
arry[2] = 2;
arry[3] = 3;
arry[4] = 4;
arry[5] = 5;
arry[6] = 6;
arry[7] = 7;
arry[8] = 8;
for (int i = 0; i < 9; i++)
{
Random rd = new Random();
rdno = rd.Next(0, 9);
button5.Text = Convert.ToString(rdno);
temp = arry[rdno];
arry[rdno] = arry[arry.Length - (i + 1)];
arry[arry.Length - (i + 1)] = temp;
}
//output
button1.Text = Convert.ToString(arry[0]);
button2.Text = Convert.ToString(arry[1]);
button3.Text = Convert.ToString(arry[2]);
button4.Text = Convert.ToString(arry[3]);
button6.Text = Convert.ToString(arry[4]);
button7.Text = Convert.ToString(arry[5]);
button8.Text = Convert.ToString(arry[6]);
button9.Text = Convert.ToString(arry[7]);
button10.Text = Convert.ToString(arry[8]);
}
private void button1_Click(object sender, EventArgs e)
{
if (button2.Text == "0")
{
String temp;
temp = button2.Text;
button2.Text = button1.Text;
button1.Text = temp;
}
else if (button4.Text == "0")
{
String temp;
temp = button4.Text;
button4.Text = button1.Text;
button1.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button2_Click(object sender, EventArgs e)
{
if (button1.Text == "0")
{
String temp;
temp = button1.Text;
button1.Text = button2.Text;
button2.Text = temp;
}
else if (button3.Text == "0")
{
String temp;
temp = button3.Text;
button3.Text = button2.Text;
button2.Text = temp;
}
else if (button6.Text == "0")
{
String temp;
temp = button6.Text;
button6.Text = button2.Text;
button2.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button3_Click(object sender, EventArgs e)
{
if (button2.Text == "0")
{
String temp;
temp = button2.Text;
button2.Text = button3.Text;
button3.Text = temp;
}
else if (button7.Text == "0")
{
String temp;
temp = button7.Text;
button7.Text = button3.Text;
button3.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button4_Click(object sender, EventArgs e)
{
if (button1.Text == "0")
{
String temp;
temp = button1.Text;
button1.Text = button4.Text;
button4.Text = temp;
}
else if (button6.Text == "0")
{
String temp;
temp = button6.Text;
button6.Text = button4.Text;
button4.Text = temp;
}
else if (button8.Text == "0")
{
String temp;
temp = button8.Text;
button8.Text = button4.Text;
button4.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button6_Click(object sender, EventArgs e)
{
if (button2.Text == "0")
{
String temp;
temp = button2.Text;
button2.Text = button6.Text;
button6.Text = temp;
}
else if (button4.Text == "0")
{
String temp;
temp = button4.Text;
button4.Text = button6.Text;
button6.Text = temp;
}
else if (button7.Text == "0")
{
String temp;
temp = button7.Text;
button7.Text = button6.Text;
button6.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button7_Click(object sender, EventArgs e)
{
if (button3.Text == "0")
{
String temp;
temp = button3.Text;
button3.Text = button7.Text;
button7.Text = temp;
}
else if (button6.Text == "0")
{
String temp;
temp = button6.Text;
button6.Text = button7.Text;
button7.Text = temp;
}
else if (button10.Text == "0")
{
String temp;
temp = button10.Text;
button10.Text = button7.Text;
button7.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button9_Click(object sender, EventArgs e)
{
if (button6.Text == "0")
{
String temp;
temp = button6.Text;
button6.Text = button9.Text;
button9.Text = temp;
}
else if (button8.Text == "0")
{
String temp;
temp = button8.Text;
button8.Text = button9.Text;
button9.Text = temp;
}
else if (button10.Text == "0")
{
String temp;
temp = button10.Text;
button10.Text = button9.Text;
button9.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button10_Click(object sender, EventArgs e)
{
if (button7.Text == "0")
{
String temp;
temp = button7.Text;
button7.Text = button10.Text;
button10.Text = temp;
}
else if (button9.Text == "0")
{
String temp;
temp = button9.Text;
button9.Text = button10.Text;
button10.Text = temp;
}
else
MessageBox.Show("不能換");
}
}
}
-------------------------------------------------------------------------------------------------------------
小算盤(按鍵自己拉)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public string str = ""; //此字串用於存取使用者點擊的數字鍵
public double all = 0;//用於計算結果暫存變數
public int how = 0;//用於判斷使用者按下的運算元
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "2";
textBox1.Text = str;
}
private void button1_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "0";
textBox1.Text = str;
}
private void button2_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }//若字串內容只有,則可視為沒有內容
str += "1"; //字串內容+"1"
textBox1.Text = str;//顯示字串內容
}
private void button4_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "3";
textBox1.Text = str;
}
private void button5_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "4";
textBox1.Text = str;
}
private void button6_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "5";
textBox1.Text = str;
}
private void button7_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "6";
textBox1.Text = str;
}
private void button8_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "7";
textBox1.Text = str;
}
private void button9_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "8";
textBox1.Text = str;
}
private void button10_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "9";
textBox1.Text = str;
}
private void button16_Click(object sender, EventArgs e)
{
str = ""; //讓變數都初始化
textBox1.Text = "0";
}
private void button11_Click(object sender, EventArgs e)
{
if (how == 1)
{ if (str == "") { str = "0"; };all += Convert.ToDouble(str); }
else if (how == 2)
{ if (str == "") { str = "0"; };all -= Convert.ToDouble(str); }
else if (how == 3)
{ if (str == "") { str = "1"; };all *= Convert.ToDouble(str); }
else if (how == 4)
{ if (str == "") { str = "1"; };all /= Convert.ToDouble(str); }
else if (how == 0)
{ if (str == "") { str = "0"; } all = Convert.ToDouble(str); }
textBox1.Text = Convert.ToString(all);
how = 1;
label1.Text = " +";
str = "";
}
private void button15_Click(object sender, EventArgs e)
{
if (str != "")//點擊=鍵還需判斷使用者是否輸入了數字內容
{ //how用於判斷使用者按壓了哪一個運算字元.1:+ 2:- 3:* 4:/ 0:=,代表歸零
//先執行上一步使用者所選擇的運算元
if (how == 1) { all += Convert.ToDouble(str); }
else if (how == 2) { all -= Convert.ToDouble(str); }
else if (how == 3) { all *= Convert.ToDouble(str); }
else if (how == 4) { all /= Convert.ToDouble(str); }
else if (all == 0) { all = Convert.ToDouble(str); }
else all = Convert.ToDouble(str);
}
how = 0;//再儲存使用者所按下的運算元
label1.Text = "=";
str = Convert.ToString(all);
textBox1.Text = str;
}
private void button12_Click(object sender, EventArgs e)
{
if (how == 1)
{ if (str == "") { str = "0"; };all += Convert.ToDouble(str); }
else if (how == 2)
{ if (str == "") { str = "0"; };all -= Convert.ToDouble(str); }
else if (how == 3)
{ if (str == "") { str = "1"; };all *= Convert.ToDouble(str); }
else if (how == 4)
{ if (str == "") { str = "1"; };all /= Convert.ToDouble(str); }
else if (how == 0)
{ if (str == "") { str = "0"; } all = Convert.ToDouble(str); }
textBox1.Text = Convert.ToString(all);
how = 2;
label1.Text = " -";
str = "";
}
private void button13_Click(object sender, EventArgs e)
{
if (how == 1)
{ if (str == "") { str = "0"; };all += Convert.ToDouble(str); }
else if (how == 2)
{ if (str == "") { str = "0"; };all -= Convert.ToDouble(str); }
else if (how == 3)
{ if (str == "") { str = "1"; };all *= Convert.ToDouble(str); }
else if (how == 4)
{ if (str == "") { str = "1"; };all /= Convert.ToDouble(str); }
else if (how == 0)
{ if (str == "") { str = "0"; } all = Convert.ToDouble(str); }
textBox1.Text = Convert.ToString(all);
how = 3;
label1.Text = " *";
str = "";
}
private void button14_Click(object sender, EventArgs e)
{
if (how == 1)
{ if (str == "") { str = "0"; };all += Convert.ToDouble(str); }
else if (how == 2)
{ if (str == "") { str = "0"; };all -= Convert.ToDouble(str); }
else if (how == 3)
{ if (str == "") { str = "1"; };all *= Convert.ToDouble(str); }
else if (how == 4)
{ if (str == "") { str = "1"; };all /= Convert.ToDouble(str); }
else if (how == 0)
{ if (str == "") { str = "0"; } all = Convert.ToDouble(str); }
textBox1.Text = Convert.ToString(all);
how = 4;
label1.Text = " /";
str = "";
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int i, bno;
int[] arry = new int[9];
public Form1()
{
InitializeComponent();
}
private void button8_Click(object sender, EventArgs e)
{
if (button4.Text == "0")
{
String temp;
temp = button4.Text;
button4.Text = button8.Text;
button8.Text = temp;
}
else if (button9.Text == "0")
{
String temp;
temp = button9.Text;
button9.Text = button8.Text;
button8.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button5_Click_1(object sender, EventArgs e)
{
int rdno;
int temp;
arry[0] = 0;
arry[1] = 1;
arry[2] = 2;
arry[3] = 3;
arry[4] = 4;
arry[5] = 5;
arry[6] = 6;
arry[7] = 7;
arry[8] = 8;
for (int i = 0; i < 9; i++)
{
Random rd = new Random();
rdno = rd.Next(0, 9);
button5.Text = Convert.ToString(rdno);
temp = arry[rdno];
arry[rdno] = arry[arry.Length - (i + 1)];
arry[arry.Length - (i + 1)] = temp;
}
//output
button1.Text = Convert.ToString(arry[0]);
button2.Text = Convert.ToString(arry[1]);
button3.Text = Convert.ToString(arry[2]);
button4.Text = Convert.ToString(arry[3]);
button6.Text = Convert.ToString(arry[4]);
button7.Text = Convert.ToString(arry[5]);
button8.Text = Convert.ToString(arry[6]);
button9.Text = Convert.ToString(arry[7]);
button10.Text = Convert.ToString(arry[8]);
}
private void button1_Click(object sender, EventArgs e)
{
if (button2.Text == "0")
{
String temp;
temp = button2.Text;
button2.Text = button1.Text;
button1.Text = temp;
}
else if (button4.Text == "0")
{
String temp;
temp = button4.Text;
button4.Text = button1.Text;
button1.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button2_Click(object sender, EventArgs e)
{
if (button1.Text == "0")
{
String temp;
temp = button1.Text;
button1.Text = button2.Text;
button2.Text = temp;
}
else if (button3.Text == "0")
{
String temp;
temp = button3.Text;
button3.Text = button2.Text;
button2.Text = temp;
}
else if (button6.Text == "0")
{
String temp;
temp = button6.Text;
button6.Text = button2.Text;
button2.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button3_Click(object sender, EventArgs e)
{
if (button2.Text == "0")
{
String temp;
temp = button2.Text;
button2.Text = button3.Text;
button3.Text = temp;
}
else if (button7.Text == "0")
{
String temp;
temp = button7.Text;
button7.Text = button3.Text;
button3.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button4_Click(object sender, EventArgs e)
{
if (button1.Text == "0")
{
String temp;
temp = button1.Text;
button1.Text = button4.Text;
button4.Text = temp;
}
else if (button6.Text == "0")
{
String temp;
temp = button6.Text;
button6.Text = button4.Text;
button4.Text = temp;
}
else if (button8.Text == "0")
{
String temp;
temp = button8.Text;
button8.Text = button4.Text;
button4.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button6_Click(object sender, EventArgs e)
{
if (button2.Text == "0")
{
String temp;
temp = button2.Text;
button2.Text = button6.Text;
button6.Text = temp;
}
else if (button4.Text == "0")
{
String temp;
temp = button4.Text;
button4.Text = button6.Text;
button6.Text = temp;
}
else if (button7.Text == "0")
{
String temp;
temp = button7.Text;
button7.Text = button6.Text;
button6.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button7_Click(object sender, EventArgs e)
{
if (button3.Text == "0")
{
String temp;
temp = button3.Text;
button3.Text = button7.Text;
button7.Text = temp;
}
else if (button6.Text == "0")
{
String temp;
temp = button6.Text;
button6.Text = button7.Text;
button7.Text = temp;
}
else if (button10.Text == "0")
{
String temp;
temp = button10.Text;
button10.Text = button7.Text;
button7.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button9_Click(object sender, EventArgs e)
{
if (button6.Text == "0")
{
String temp;
temp = button6.Text;
button6.Text = button9.Text;
button9.Text = temp;
}
else if (button8.Text == "0")
{
String temp;
temp = button8.Text;
button8.Text = button9.Text;
button9.Text = temp;
}
else if (button10.Text == "0")
{
String temp;
temp = button10.Text;
button10.Text = button9.Text;
button9.Text = temp;
}
else
MessageBox.Show("不能換");
}
private void button10_Click(object sender, EventArgs e)
{
if (button7.Text == "0")
{
String temp;
temp = button7.Text;
button7.Text = button10.Text;
button10.Text = temp;
}
else if (button9.Text == "0")
{
String temp;
temp = button9.Text;
button9.Text = button10.Text;
button10.Text = temp;
}
else
MessageBox.Show("不能換");
}
}
}
-------------------------------------------------------------------------------------------------------------
小算盤(按鍵自己拉)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public string str = ""; //此字串用於存取使用者點擊的數字鍵
public double all = 0;//用於計算結果暫存變數
public int how = 0;//用於判斷使用者按下的運算元
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "2";
textBox1.Text = str;
}
private void button1_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "0";
textBox1.Text = str;
}
private void button2_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }//若字串內容只有,則可視為沒有內容
str += "1"; //字串內容+"1"
textBox1.Text = str;//顯示字串內容
}
private void button4_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "3";
textBox1.Text = str;
}
private void button5_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "4";
textBox1.Text = str;
}
private void button6_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "5";
textBox1.Text = str;
}
private void button7_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "6";
textBox1.Text = str;
}
private void button8_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "7";
textBox1.Text = str;
}
private void button9_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "8";
textBox1.Text = str;
}
private void button10_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "9";
textBox1.Text = str;
}
private void button16_Click(object sender, EventArgs e)
{
str = ""; //讓變數都初始化
textBox1.Text = "0";
}
private void button11_Click(object sender, EventArgs e)
{
if (how == 1)
{ if (str == "") { str = "0"; };all += Convert.ToDouble(str); }
else if (how == 2)
{ if (str == "") { str = "0"; };all -= Convert.ToDouble(str); }
else if (how == 3)
{ if (str == "") { str = "1"; };all *= Convert.ToDouble(str); }
else if (how == 4)
{ if (str == "") { str = "1"; };all /= Convert.ToDouble(str); }
else if (how == 0)
{ if (str == "") { str = "0"; } all = Convert.ToDouble(str); }
textBox1.Text = Convert.ToString(all);
how = 1;
label1.Text = " +";
str = "";
}
private void button15_Click(object sender, EventArgs e)
{
if (str != "")//點擊=鍵還需判斷使用者是否輸入了數字內容
{ //how用於判斷使用者按壓了哪一個運算字元.1:+ 2:- 3:* 4:/ 0:=,代表歸零
//先執行上一步使用者所選擇的運算元
if (how == 1) { all += Convert.ToDouble(str); }
else if (how == 2) { all -= Convert.ToDouble(str); }
else if (how == 3) { all *= Convert.ToDouble(str); }
else if (how == 4) { all /= Convert.ToDouble(str); }
else if (all == 0) { all = Convert.ToDouble(str); }
else all = Convert.ToDouble(str);
}
how = 0;//再儲存使用者所按下的運算元
label1.Text = "=";
str = Convert.ToString(all);
textBox1.Text = str;
}
private void button12_Click(object sender, EventArgs e)
{
if (how == 1)
{ if (str == "") { str = "0"; };all += Convert.ToDouble(str); }
else if (how == 2)
{ if (str == "") { str = "0"; };all -= Convert.ToDouble(str); }
else if (how == 3)
{ if (str == "") { str = "1"; };all *= Convert.ToDouble(str); }
else if (how == 4)
{ if (str == "") { str = "1"; };all /= Convert.ToDouble(str); }
else if (how == 0)
{ if (str == "") { str = "0"; } all = Convert.ToDouble(str); }
textBox1.Text = Convert.ToString(all);
how = 2;
label1.Text = " -";
str = "";
}
private void button13_Click(object sender, EventArgs e)
{
if (how == 1)
{ if (str == "") { str = "0"; };all += Convert.ToDouble(str); }
else if (how == 2)
{ if (str == "") { str = "0"; };all -= Convert.ToDouble(str); }
else if (how == 3)
{ if (str == "") { str = "1"; };all *= Convert.ToDouble(str); }
else if (how == 4)
{ if (str == "") { str = "1"; };all /= Convert.ToDouble(str); }
else if (how == 0)
{ if (str == "") { str = "0"; } all = Convert.ToDouble(str); }
textBox1.Text = Convert.ToString(all);
how = 3;
label1.Text = " *";
str = "";
}
private void button14_Click(object sender, EventArgs e)
{
if (how == 1)
{ if (str == "") { str = "0"; };all += Convert.ToDouble(str); }
else if (how == 2)
{ if (str == "") { str = "0"; };all -= Convert.ToDouble(str); }
else if (how == 3)
{ if (str == "") { str = "1"; };all *= Convert.ToDouble(str); }
else if (how == 4)
{ if (str == "") { str = "1"; };all /= Convert.ToDouble(str); }
else if (how == 0)
{ if (str == "") { str = "0"; } all = Convert.ToDouble(str); }
textBox1.Text = Convert.ToString(all);
how = 4;
label1.Text = " /";
str = "";
}
}
}
訂閱:
文章 (Atom)