2011年1月7日 星期五

100/1/7 彈珠台~

連結~
-------------------------------------------------------------------------------------------------------------
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);
        }
    }
}

沒有留言:

張貼留言