2010年12月30日 星期四

遊戲 XNA期末

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 WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GameObject CD1; // CD宣告
        GameObject CD2;
        GameObject CD3;
        GameObject CD4;
        GameObject CD5;
        GameObject CD6;
        GameObject CD7;
        GameObject CD8;
        GameObject CD9;
        GameObject myplane;//飛機宣告
        int score;//分數宣告
        SpriteFont font;//字體
        Vector2 scoreDrawPoint = new Vector2(0.1f, 0.1f);//SCORE放的地方
        
        
       
       // SpriteFont font;
        Texture2D backgroundTexture;//背景
        Rectangle viewportRect;

        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);
               
            // TODO: use this.Content to load your game content here
            Texture2D texture = Content.Load<Texture2D>("CD2");//載入CD圖形
            Texture2D texture2 = Content.Load<Texture2D>("plane");//載入飛機圖形
            font = Content.Load<SpriteFont>("GameFont");
            backgroundTexture =Content.Load<Texture2D>("images");//背景圖
            myplane = new GameObject(texture2, new Vector2(700f,500f),
                                          new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            //宣告飛機產生(圖片,位置,範圍)
            CD1 = new GameObject(texture,new Vector2(0f, 0f),
                                          new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD1.velocity = new Vector2(-9, 2); // 設位移速度
          
            CD2 = new GameObject(texture,new Vector2(70f, 70f),
                                      new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD2.velocity = new Vector2(1, -4);
            CD3 = new GameObject(texture,new Vector2(140f, 140f),
                                     new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD3.velocity = new Vector2(-2, 4);
            CD4 = new GameObject(texture,new Vector2(210f, 210f),
                                     new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD4.velocity = new Vector2(-3, -8);
            CD5 = new GameObject(texture,new Vector2(300f, 300f),
                                     new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD5.velocity = new Vector2(4,- 3);
            CD6 = new GameObject(texture, new Vector2(370f, 370f),
                                     new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD6.velocity = new Vector2(-5, 3);
            CD7 = new GameObject(texture, new Vector2(460f, 460f),
                                     new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD7.velocity = new Vector2(6, -5);
            CD8 = new GameObject(texture, new Vector2(520f, 520f),
                                    new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD8.velocity = new Vector2(-7, -9);
            CD9 = new GameObject(texture, new Vector2(480f, 80f),
                                     new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD9.velocity = new Vector2(8, -7);
            viewportRect = new Rectangle(0, 0,
                graphics.GraphicsDevice.Viewport.Width,
                graphics.GraphicsDevice.Viewport.Height);

        }
        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }
        /// <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();
           
          
            // TODO: Add your update logic here
            CD1.Move(); //叫出用於移動的指令
            CD2.Move();
            CD3.Move();
            CD4.Move();
            CD5.Move();
            CD6.Move();
            CD7.Move();
            CD8.Move();
            CD9.Move();
            myplane.Move();

            if (CD1.Collides(CD2))//當CD1碰到CD2時...移動速度方向對調
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD2.velocity;
                CD2.velocity = tempVelocity;
            }
            if (CD1.Collides(CD3))//同上
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD3.velocity;
                CD3.velocity = tempVelocity;
            }
            if (CD1.Collides(CD4))
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD4.velocity;
                CD4.velocity = tempVelocity;
            }
            if (CD1.Collides(CD5))
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD5.velocity;
                CD5.velocity = tempVelocity;
            }
            if (CD1.Collides(CD6))
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD6.velocity;
                CD6.velocity = tempVelocity;
            }
            if (CD1.Collides(CD7))
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD7.velocity;
                CD7.velocity = tempVelocity;
            }
            if (CD1.Collides(CD8))
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD1.Collides(CD9))
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
           
            if (CD2.Collides(CD3))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD3.velocity;
                CD3.velocity = tempVelocity;
            }
            if (CD2.Collides(CD4))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD4.velocity;
                CD4.velocity = tempVelocity;
            }
            if (CD2.Collides(CD5))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD5.velocity;
                CD5.velocity = tempVelocity;
            }
            if (CD2.Collides(CD6))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD6.velocity;
                CD6.velocity = tempVelocity;
            }
            if (CD2.Collides(CD7))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD7.velocity;
                CD7.velocity = tempVelocity;
            }
            if (CD2.Collides(CD8))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD2.Collides(CD9))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
           
           
            if (CD3.Collides(CD4))
            {
                Vector2 tempVelocity = CD3.velocity;
                CD3.velocity = CD4.velocity;
                CD4.velocity = tempVelocity;
            }
            if (CD3.Collides(CD5))
            {
                Vector2 tempVelocity = CD3.velocity;
                CD3.velocity = CD5.velocity;
                CD5.velocity = tempVelocity;
            }
            if (CD3.Collides(CD6))
            {
                Vector2 tempVelocity = CD3.velocity;
                CD3.velocity = CD6.velocity;
                CD6.velocity = tempVelocity;
            }
            if (CD3.Collides(CD7))
            {
                Vector2 tempVelocity = CD3.velocity;
                CD3.velocity = CD7.velocity;
                CD7.velocity = tempVelocity;
            }
            if (CD3.Collides(CD8))
            {
                Vector2 tempVelocity = CD3.velocity;
                CD3.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD3.Collides(CD9))
            {
                Vector2 tempVelocity = CD3.velocity;
                CD3.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
           
          
           
            if (CD4.Collides(CD5))
            {
                Vector2 tempVelocity = CD4.velocity;
                CD4.velocity = CD5.velocity;
                CD5.velocity = tempVelocity;
            }
            if (CD4.Collides(CD6))
            {
                Vector2 tempVelocity = CD4.velocity;
                CD4.velocity = CD6.velocity;
                CD6.velocity = tempVelocity;
            }
            if (CD4.Collides(CD7))
            {
                Vector2 tempVelocity = CD4.velocity;
                CD4.velocity = CD7.velocity;
                CD7.velocity = tempVelocity;
            }
            if (CD4.Collides(CD8))
            {
                Vector2 tempVelocity = CD4.velocity;
                CD4.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD4.Collides(CD9))
            {
                Vector2 tempVelocity = CD4.velocity;
                CD4.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }

            if (CD5.Collides(CD6))
            {
                Vector2 tempVelocity = CD5.velocity;
                CD5.velocity = CD6.velocity;
                CD6.velocity = tempVelocity;
            }
            if (CD5.Collides(CD7))
            {
                Vector2 tempVelocity = CD5.velocity;
                CD5.velocity = CD7.velocity;
                CD7.velocity = tempVelocity;
            }
            if (CD5.Collides(CD8))
            {
                Vector2 tempVelocity = CD5.velocity;
                CD5.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD5.Collides(CD9))
            {
                Vector2 tempVelocity = CD5.velocity;
                CD5.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
            if (CD6.Collides(CD7))
            {
                Vector2 tempVelocity = CD6.velocity;
                CD6.velocity = CD7.velocity;
                CD7.velocity = tempVelocity;
            }
            if (CD6.Collides(CD8))
            {
                Vector2 tempVelocity = CD6.velocity;
                CD6.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD6.Collides(CD9))
            {
                Vector2 tempVelocity = CD6.velocity;
                CD6.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
            if (CD7.Collides(CD8))
            {
                Vector2 tempVelocity = CD7.velocity;
                CD7.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD7.Collides(CD9))
            {
                Vector2 tempVelocity = CD7.velocity;
                CD7.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
            if (CD8.Collides(CD9))
            {
                Vector2 tempVelocity = CD8.velocity;
                CD8.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
            if (myplane.Collides(CD1))//飛機碰到CD1時..CD1速度方向變反方向..同時分數+1
            {
                CD1.velocity *= -1;
                score += 1;
               
            }
            if (myplane.Collides(CD2))
            {
                CD2.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD3))
            {
                CD3.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD4))
            {
                CD4.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD5))
            {
                CD5.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD6))
            {
                CD6.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD7))
            {
                CD7.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD8))
            {
                CD8.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD9))
            {
                CD9.velocity *= -1;
                score += 1;
            }
            KeyboardState keyboardState = Keyboard.GetState();
            if (keyboardState.IsKeyDown(Keys.Left))//鍵盤案Left時...飛機位置往x軸-10.0f前進
            {
                myplane.position.X -= 5.0f;
            }
            if (keyboardState.IsKeyDown(Keys.Right))
            {
                myplane.position.X += 5.0f;
            }
            if (keyboardState.IsKeyDown(Keys.Up))
            {
                myplane.position.Y -= 5.0f;
            }
            if (keyboardState.IsKeyDown(Keys.Down))
            {
                myplane.position.Y += 5.0f;
            }
           
          
            base.Update(gameTime);
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            spriteBatch.Draw(backgroundTexture, viewportRect,
                Color.White);
            spriteBatch.Draw(CD1.texture, CD1.position, Color.White);//圖.位置.顏色
            spriteBatch.Draw(CD2.texture, CD2.position, Color.White);
            spriteBatch.Draw(CD3.texture, CD3.position, Color.White);
            spriteBatch.Draw(CD4.texture, CD4.position, Color.White);
            spriteBatch.Draw(CD5.texture, CD5.position, Color.White);
            spriteBatch.Draw(CD6.texture, CD6.position, Color.White);
            spriteBatch.Draw(CD7.texture, CD7.position, Color.White);
            spriteBatch.Draw(CD8.texture, CD8.position, Color.White);
            spriteBatch.Draw(CD9.texture, CD9.position, Color.White);
            spriteBatch.Draw(myplane.texture, myplane.position, Color.White);
            spriteBatch.DrawString(font,
                "You're already dead " + score.ToString()+"  times",
                new Vector2(scoreDrawPoint.X * viewportRect.Width,
                scoreDrawPoint.Y * viewportRect.Height),
                Color.Red);//字體.文字.字元轉換.位置.顏色
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

----------------------------------------------------------------------------------


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;
using System.Text;

namespace WindowsGame1
{
    class GameObject
    {
       
        public Texture2D texture;  // 圖
        public Vector2 position;   // 圖 的位置
        private Vector2 screenSize; // 視窗寬高
        public bool alive;
        public float scale = 1.0f;
      
        public Vector2 velocity = Vector2.Zero;   // 圖 的位移速度
        public GameObject(Texture2D texture, Vector2 position, Vector2 screenSize)
        {
            this.texture = texture;
            this.position = position;
            this.screenSize = screenSize;
            alive = false;
           
        }
     
         // 移動
        public void Move()
        {
            // 圖右邊 碰到 視窗右邊了
            if (position.X + texture.Width + velocity.X > screenSize.X)
                velocity.X = -velocity.X;
            // 圖下面 碰到 視窗底邊了
            if (position.Y + texture.Height + velocity.Y > screenSize.Y)
                velocity.Y = -velocity.Y;
            // 圖左邊 碰到 視窗左邊了
            if (position.X + velocity.X < 0)
                velocity.X = -velocity.X;
            // 圖上面 碰到 視窗上邊了
            if (position.Y + velocity.Y < 0)
                velocity.Y = -velocity.Y;
            position += velocity;
        }
        public bool Collides(GameObject other)
        {
            // 檢查有沒有碰撞
            return (this.position.X + texture.Width > other.position.X &&
                    this.position.X < other.position.X + other.texture.Width &&
                    this.position.Y + texture.Height > other.position.Y &&
                    this.position.Y < other.position.Y + other.texture.Height);
        }
    }
  
      
           
        }