使对象看起来随机移动到屏幕上

时间:2012-04-21 22:43:52

标签: xna

我正在进行一项任务,其中一项要求是随机对象出现在屏幕上并移动。作为XNA的新手,我不知道在游戏中何处开始实施此类行为,因此如果有人能够向我推进正确的方向,我将非常感激。

我只是习惯于在按下某个键时调用某些东西,但是如果有一些完全随机的东西,则无法完成。据我所知。

谢谢。

1 个答案:

答案 0 :(得分:0)

您需要先为UFO创建并设置一个精灵。在受保护的覆盖无效Update(GameTime gameTime)代码中,您只需获取当前时间并将其与您希望应用的规则进行比较。如果你想绘制并“移动”精灵,那么更新。这是一个例子:

#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
 public class Game : Microsoft.Xna.Framework.Game {
        #region Game Settings
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GraphicsDevice device;
        int screenWidth         = 800;
        int screenHeight        = 600;
        bool fullscreen         = false;
        string title            = "MyGame";
        string origionaltitle;
        float frames            = 0;
        float framesPerSecond   = 0;
        int startTime;
        int currentTime;
        int nextTime;
        #endregion
        struct sprite {

            public string TextureName;
            public Texture2D Texture;
            public Vector2 Position;
            public Vector2 Speed;
            public Color[] TextureData;
        };
        bool DrawUFO = false;
        sprite ufo = new sprite();
        public Game() {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void LoadContent() {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            device = graphics.GraphicsDevice;
            // TODO: use this.Content to load your game content here
            ufo.TextureName = "ufo";
            ufo.Texture = Content.Load<Texture2D>(ball.TextureName);
            ufo.TextureData = new Color[ufo.Texture.Width *ufo.Texture.Height];
            ufo.Texture.GetData(ball.TextureData); 
        }
         protected override void Initialize() {
            // TODO: Add your initialization logic here
            ufo.Position = new Vector2(10f, 10.0f);
            ufo.Speed = new Vector2(0.0f, 10.0f);
            //
            // Set up game window
            graphics.PreferredBackBufferWidth = screenWidth;
            graphics.PreferredBackBufferHeight = screenHeight;
            graphics.IsFullScreen = fullscreen;
            graphics.ApplyChanges();
            origionaltitle = title;
            Window.Title = title;
            //
            // Set the initial time
            startTime = DateTime.Now.Second;
            //
            // Set "random"/next time for ufo to be rendered
            nextTime = startTime + rand.Next(2);
            //
            base.Initialize();
        }
        protected override void Update(GameTime gameTime) {
            //
            // Set the current time
            currentTime = DateTime.Now.Second;
            //
            // if not drawing ufo then
            if(!DrawURO) {
                // 
                // check current time and compare it with the next time
                if( currentTime == nextTime ) {
                    DrawURO = true;
                }
            } else {
                //
                // Update UFO position (aka move it)
                ufo.Posistion += ball.Speed *(float)gameTime.ElapsedGameTime.TotalSeconds;
                //
                // if ufo goes of the screen then
                if(ufo.Position.Y > screenHeight) {
                    //
                    // Reset ufo
                    DrawURO = false;
                    ufo.Position.X = 10.0f;
                    ufo.Position.Y = 10.0f;
                    //
                    // set next time to render
                    nextTime = currentTime + rand.Next(2);
                }

            }
        }
        protected override void Draw(GameTime gameTime) {
            graphics.GraphicsDevice.Clear(Color.Black);
            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            if(DrawUFO == true) {
                spriteBatch.Draw(ufo.Texture, ufo.Position, Color.White);
            }
            spriteBatch.End();
            //
            base.Draw(gameTime);
        }
 }

我从几年前在大学做过的一些代码中复制了这个,所以对任何错误道歉。