XNA 学习做的第一个小游戏 精灵追逐 (一)

 

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 AnimatedSprites

{

    /// <summary>

    /// This is the main type for your game

    /// </summary>

    public class Game1 : Microsoft.Xna.Framework.Game

    {

        GraphicsDeviceManager graphics;

        SpriteBatch spriteBatch;



        //threerings

        Texture2D ringTexture;

        Point ringCurrentFrame = new Point(0, 0);

        Point ringFrameSize = new Point(75, 75);

        Point ringSheetFrame = new Point(6, 8);



        int ringSincePerTime = 0;

        int ringWishDoTime = 50;



        Vector2 ringPosition = Vector2.Zero;

        //skullball

        Texture2D skullTexture;

        Point skullCurrentFrame = new Point(0, 0);

        Point skullFrameSize = new Point(75, 75);

        Point skullSheetFrame = new Point(6, 8);



        int skullSincePerTime = 0;

        int skullWishDoTime = 50;



        //plus

        Texture2D plusTexture;

        Point plusCurrentFrame = new Point(0, 0);

        Point plusFrameSize = new Point(75, 75);

        Point plusSheetFrame = new Point(6, 4);

        Vector2 plusPosition = Vector2.Zero;



        int plusSincePerTime = 0;

        int plusWishDoTime = 50;



        const int plusSpeed = 3;

        int plusSpeedX = plusSpeed;

        int plusSpeedY = plusSpeed;





        //mouse postion

        Point mousePerPosition = new Point(0,0);

        //move speed

        int ringSpeed = 6;



        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

            ringTexture = Content.Load<Texture2D>(@"Images/threerings");

            skullTexture = Content.Load<Texture2D>(@"Images/skullball");

            plusTexture = Content.Load<Texture2D>(@"Images/plus");

        }



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

            //threerings

            ringSincePerTime += gameTime.ElapsedGameTime.Milliseconds;

            if (ringSincePerTime > ringWishDoTime)

            {

                ringSincePerTime = ringSincePerTime - ringWishDoTime;

                ++ringCurrentFrame.X;

                if (ringCurrentFrame.X > ringSheetFrame.X)

                {

                    ringCurrentFrame.X = 0;

                    ++ringCurrentFrame.Y;

                    if (ringCurrentFrame.Y > ringSheetFrame.Y)

                    {

                        ringCurrentFrame.Y = 0;

                    }

                }

            }

            //skullball

            skullSincePerTime += gameTime.ElapsedGameTime.Milliseconds;

            if (skullSincePerTime > skullWishDoTime)

            {

                skullSincePerTime = skullSincePerTime - skullWishDoTime;

                ++skullCurrentFrame.X;

                if (skullCurrentFrame.X > skullSheetFrame.X)

                {

                    skullCurrentFrame.X = 0;

                    ++skullCurrentFrame.Y;

                    if (skullCurrentFrame.Y > skullSheetFrame.Y)

                    {

                        skullCurrentFrame.Y = 0;

                    }

                }

            }



            //plus

            plusSincePerTime += gameTime.ElapsedGameTime.Milliseconds;

            if (plusSincePerTime > plusWishDoTime)

            {

                plusSincePerTime = plusSincePerTime - plusWishDoTime;

                ++plusCurrentFrame.X;

                if (plusCurrentFrame.X > plusSheetFrame.X)

                {

                    plusCurrentFrame.X = 0;

                    ++plusCurrentFrame.Y;

                    if (plusCurrentFrame.Y > plusSheetFrame.Y)

                    {

                        plusCurrentFrame.Y = 0;

                    }

                }



                //第二个精灵无规则运动

                //假如碰撞到右边

                if (plusPosition.X > plusFrameSize.X * plusSheetFrame.X)

                {

                    //设置当前位置为右边的临界点

                    plusPosition.X = plusFrameSize.X * plusSheetFrame.X;

                    //设置X横向运动速度为plusSpeed

                    plusSpeedX = plusSpeed;

                    //设置一个4到6的随机数,用于随机改变Y方向的速度

                    Random rd = new Random();

                    double sjs = Convert.ToInt32(rd.Next(4, 7)) * 0.1;

                    //zf用于设置Y轴运动的方向

                    bool zf = false;

                    if (sjs == 0.5)

                    {

                        zf = true;

                    }

                    if (sjs > 0.5)

                    {

                        if (zf)

                        {

                            plusSpeedY = (int)(plusSpeedX * sjs);

                        }

                        else

                        {

                            plusSpeedY = (int)(plusSpeedX / sjs);

                        }

                    }

                    else

                    {

                        if (zf)

                        {

                            plusSpeedY= -(int)(plusSpeedX * sjs);

                        }

                        else

                        {

                            plusSpeedY = -(int)(plusSpeedX / sjs);

                        }

                    }

                    //由于X轴右边碰撞,所以位置要加负值

                    plusSpeedX = -plusSpeedX;

                }

                else if (plusPosition.X < 0)

                {

                    plusPosition.X = 0;

                    plusSpeedX = plusSpeed;

                    Random rd = new Random();

                    double sjs = Convert.ToInt32(rd.Next(4, 7)) * 0.1;

                    bool zf = false;

                    if (sjs == 0.5)

                    {

                        zf = true;

                    }

                    if (sjs > 0.5)

                    {

                        if (zf)

                        {

                            plusSpeedY = (int)(plusSpeedX * sjs);

                        }

                        else

                        {

                            plusSpeedY = (int)(plusSpeedX / sjs);

                        }

                    }

                    else

                    {

                        if (zf)

                        {

                            plusSpeedY = -(int)(plusSpeedX * sjs);

                        }

                        else

                        {

                            plusSpeedY = -(int)(plusSpeedX / sjs);

                        }

                    }

                }

                else if (plusPosition.Y > plusFrameSize.Y * plusSheetFrame.Y)

                {

                    plusPosition.Y = plusFrameSize.Y * plusSheetFrame.Y;

                    plusSpeedY = plusSpeed;

                    Random rd = new Random();

                    double sjs = Convert.ToInt32(rd.Next(4, 7)) * 0.1;

                    bool zf = false;

                    if (sjs == 0.5)

                    {

                        zf = true;

                    }

                    if (sjs > 0.5)

                    {

                        if (zf)

                        {

                            plusSpeedX = (int)(plusSpeedY * sjs);

                        }

                        else

                        {

                            plusSpeedX = (int)(plusSpeedY / sjs);

                        }

                    }

                    else

                    {

                        if (zf)

                        {

                            plusSpeedX = -(int)(plusSpeedY * sjs);

                        }

                        else

                        {

                            plusSpeedX = -(int)(plusSpeedY / sjs);

                        }

                    }

                    plusSpeedY = -plusSpeedY;

                }

                else if (plusPosition.Y < 0)

                {

                    plusPosition.Y = 0;

                    plusSpeedY = plusSpeed;

                    Random rd = new Random();

                    double sjs = Convert.ToInt32(rd.Next(4, 7)) * 0.1;

                    bool zf = false;

                    if (sjs == 0.5)

                    {

                        zf = true;

                    }

                    if (sjs > 0.5)

                    {

                        if (zf)

                        {

                            plusSpeedX = (int)(plusSpeedY * sjs);

                        }

                        else

                        {

                            plusSpeedX = (int)(plusSpeedY / sjs);

                        }

                    }

                    else

                    {

                        if (zf)

                        {

                            plusSpeedX = -(int)(plusSpeedY * sjs);

                        }

                        else

                        {

                            plusSpeedX = -(int)(plusSpeedY / sjs);

                        }

                    }

                }



                plusPosition.X += plusSpeedX;

                plusPosition.Y += plusSpeedY;               





                //move skull

                KeyboardState keyboardState = Keyboard.GetState();

                if (keyboardState.IsKeyDown(Keys.Left))

                {

                    ringPosition.X -= ringSpeed;

                }

                if (keyboardState.IsKeyDown(Keys.Right))

                {

                    ringPosition.X += ringSpeed;

                }

                if (keyboardState.IsKeyDown(Keys.Up))

                {

                    ringPosition.Y -= ringSpeed;

                }

                if (keyboardState.IsKeyDown(Keys.Down))

                {

                    ringPosition.Y += ringSpeed;

                }



                MouseState mouseState = Mouse.GetState();

                Point mouseCurPosition = new Point(mouseState.X, mouseState.Y);

                if (mousePerPosition.X != mouseCurPosition.X || mousePerPosition.Y != mouseCurPosition.Y)

                {

                    ringPosition = new Vector2(mouseCurPosition.X, mouseCurPosition.Y);

                    mousePerPosition = mouseCurPosition;

                }



                if (ringPosition.X < 0)

                {

                    ringPosition.X = 0;

                }

                if (ringPosition.Y < 0)

                {

                    ringPosition.Y = 0;

                }

                if (ringPosition.X > ringFrameSize.X * ringSheetFrame.X)

                {

                    ringPosition.X = ringFrameSize.X * ringSheetFrame.X;

                }

                if (ringPosition.Y > ringFrameSize.Y * ringSheetFrame.Y)

                {

                    ringPosition.Y = ringFrameSize.Y * ringSheetFrame.Y;

                }

                //碰撞后退出

                if (collide() || collide2())

                {

                    this.Exit();

                }



                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)

        {

            GraphicsDevice.Clear(Color.CornflowerBlue);



            // TODO: Add your drawing code here

            spriteBatch.Begin();

            spriteBatch.Draw(ringTexture, ringPosition,

                new Rectangle(ringCurrentFrame.X * ringFrameSize.X, ringCurrentFrame.Y * ringFrameSize.Y,

                    ringFrameSize.X, ringFrameSize.Y), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

            spriteBatch.Draw(skullTexture, new Vector2(100,100),

                new Rectangle(skullCurrentFrame.X * skullFrameSize.X, skullCurrentFrame.Y * skullFrameSize.Y, skullFrameSize.X, skullFrameSize.Y),

                Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

            spriteBatch.Draw(plusTexture, plusPosition,

                new Rectangle(plusCurrentFrame.X * plusFrameSize.X, plusCurrentFrame.Y * plusFrameSize.Y, plusFrameSize.X, plusFrameSize.Y),

                Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

            spriteBatch.End();

            base.Draw(gameTime);

        }



        protected bool collide()

        {

            //精灵于骷髅碰撞检测

            Rectangle ringRect = new Rectangle((int)ringPosition.X , (int)ringPosition.Y , (int)ringFrameSize.X - 20, (int)ringFrameSize.Y - 20);

            Rectangle skullRect = new Rectangle(100 ,100 , (int)skullFrameSize.X - 20, (int)skullFrameSize.Y - 20);

            return ringRect.Intersects(skullRect);

        }

        //检测动态精灵碰撞

        protected bool collide2()

        {

            Rectangle ringRect = new Rectangle((int)ringPosition.X, (int)ringPosition.Y, (int)ringFrameSize.X - 20, (int)ringFrameSize.Y - 20);

            Rectangle plusRect = new Rectangle((int)plusPosition.X, (int)plusPosition.Y, (int)plusFrameSize.X, (int)plusFrameSize.Y);

            return ringRect.Intersects(plusRect);

        }

    }

}

  plus.png:

XNA 学习做的第一个小游戏 精灵追逐 (一)

threerings.png:

XNA 学习做的第一个小游戏 精灵追逐 (一)

skullball.png

XNA 学习做的第一个小游戏 精灵追逐 (一)

运行效果:plus随机运动,碰到墙壁后返回。

thrrerings可以使用鼠标和键盘的方向键控制运动,碰到追逐它的精灵和炸弹后,游戏退出。

XNA 学习做的第一个小游戏 精灵追逐 (一)

你可能感兴趣的:(游戏)