2010年12月24日 星期五

遊戲

  
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;

namespace WindowsGame2
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GameObject topWall;
        GameObject bottomWall;
        GameObject playerOne;
        GameObject playerTwo;
        KeyboardState keyboardState;
        GameObject ball;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            base.Initialize();
        }
        protected override void LoadContent()
        {
            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));
            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);

            topWall = new GameObject(
                wallTexture,
                Vector2.Zero);
            bottomWall = new GameObject(
                wallTexture,
                new Vector2(0, Window.ClientBounds.Height - wallTexture.Height));


            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(
                playerOne.BoundingBox.Right + 1,
                (Window.ClientBounds.Height - ballTexture.Height) / 2);

            ball = new GameObject(
                ballTexture,
                position,
                new Vector2(8f, -8f));
        }
        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            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();

           
            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;
            }
        }

        private 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;
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            topWall.Draw(spriteBatch);
            bottomWall.Draw(spriteBatch);

            topWall.Draw(spriteBatch);
            bottomWall.Draw(spriteBatch);

            playerOne.Draw(spriteBatch);
            playerTwo.Draw(spriteBatch);
           
            topWall.Draw(spriteBatch);
            bottomWall.Draw(spriteBatch);

            playerOne.Draw(spriteBatch);
            playerTwo.Draw(spriteBatch);

            ball.Draw(spriteBatch);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}


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;

namespace WindowsGame2
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GameObject topWall;
        GameObject player;
        KeyboardState keyboardState;
        GameObject ball;
        GameObject wl;
        GameObject wlr;
        GameObject three;
        GameObject three1;
        GameObject four;
        GameObject four1;
        SpriteFont font;
        int score;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            base.Initialize();
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Texture2D threeTexture = Content.Load<Texture2D>("three");
            Texture2D three1Texture = Content.Load<Texture2D>("three");
            Texture2D fourTexture = Content.Load<Texture2D>("four");
            Texture2D four1Texture = Content.Load<Texture2D>("four");
            Texture2D wallTexture = Content.Load<Texture2D>("wall");
            Texture2D wlrTexture = Content.Load<Texture2D>("wlr");
            topWall = new GameObject(
                wallTexture,
                Vector2.Zero);
            three = new GameObject(
                threeTexture,
                new Vector2((Window.ClientBounds.Width/3*2),
                (Window.ClientBounds.Height/2-70)));
            three1 = new GameObject(
                threeTexture,
                new Vector2((Window.ClientBounds.Width / 3-125 ),
                (Window.ClientBounds.Height / 2+50)));
            four = new GameObject(
                fourTexture,
                new Vector2((Window.ClientBounds.Width / 3+115),
                (Window.ClientBounds.Height / 2-50)));
            four1 = new GameObject(
                fourTexture,
                new Vector2((Window.ClientBounds.Width / 3-125),
                (Window.ClientBounds.Height / 2-140)));
            topWall = new GameObject(
                wallTexture,
                Vector2.Zero);
            wlr = new GameObject(
                wlrTexture,
                new Vector2((Window.ClientBounds.Width - wlrTexture.Width),
                (Window.ClientBounds.Height - wlrTexture.Height)));
            wl = new GameObject(
                wlrTexture,
                Vector2.Zero);

            Texture2D pTexture = Content.Load<Texture2D>("p");
            Vector2 position;
            position = new Vector2(
                            (Window.ClientBounds.Width / 2 - 2*wallTexture.Height),
                            Window.ClientBounds.Height - wallTexture.Height);
            player= new GameObject(pTexture, position);

            topWall = new GameObject(
                wallTexture,
                Vector2.Zero);

            Texture2D ballTexture = Content.Load<Texture2D>("ball");
            ball = new GameObject(
                ballTexture,
                position,
                new Vector2(6f, -6f));

            font = Content.Load<SpriteFont>("GameFont");
        }

        protected override void UnloadContent()
        {
        }
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            keyboardState = Keyboard.GetState();
            if (keyboardState.IsKeyDown(Keys.Left))
                player.Position.X -= 15f;

            if (keyboardState.IsKeyDown(Keys.Right))
                player.Position.X += 15f;

            /*   if (keyboardState.IsKeyDown(Keys.W))
                playerOne.Position.Y -= 10f;

            if (keyboardState.IsKeyDown(Keys.S))
                playerOne.Position.Y += 10f;*/


            ball.Position += ball.Velocity;

            CheckBallCollision();
            base.Update(gameTime);
        }

        private void CheckBallCollision()
        {
            if (ball.BoundingBox.Intersects(topWall.BoundingBox))
            {
                ball.Velocity.Y *= -1;
            }
            if (ball.BoundingBox.Intersects(three.BoundingBox))
            {
                ball.Velocity.Y *= -1;
                score += 1;
            }
            if (ball.BoundingBox.Intersects(three1.BoundingBox))
            {
                ball.Velocity.Y *= -1;
                score += 1;
            }
            if (ball.BoundingBox.Intersects(four.BoundingBox))
            {
                ball.Velocity.Y *= -1;
                score += 1;
            }
            if (ball.BoundingBox.Intersects(four1.BoundingBox))
            {
                ball.Velocity.Y *= -1;
                score += 1;
            }
            if (ball.BoundingBox.Intersects(wlr.BoundingBox))
            {
                ball.Velocity.X *= -1;
            }
            if (ball.BoundingBox.Intersects(wl.BoundingBox))
            {
                ball.Velocity.X *= -1;
            }
            if (ball.BoundingBox.Intersects(player.BoundingBox))
            {
                ball.Velocity.Y *= -1;
                ball.Position += ball.Velocity;
            }
        }


        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();

            wlr.Draw(spriteBatch);
            wl.Draw(spriteBatch);
            three.Draw(spriteBatch);
            three1.Draw(spriteBatch);
            four.Draw(spriteBatch);
            four1.Draw(spriteBatch);
            topWall.Draw(spriteBatch);
            player.Draw(spriteBatch);
            ball.Draw(spriteBatch);

            spriteBatch.DrawString(font,"Score: " + score.ToString(),
                new Vector2(50,50 ),Color.Red);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}



1 則留言: