0

我目前正在做一个任务,其中一个要求是随机对象出现在屏幕上并移动。作为 XNA 的新手,我什至不知道从哪里开始在游戏中实施此类行为,因此如果有人能帮我推动正确的方向,我将不胜感激。

我只是真的习惯于在按下键时调用某些东西,但是对于完全随机的东西,这是无法做到的。据我所知。

谢谢你。

4

1 回答 1

0

您需要首先为 UFO 创建和设置精灵。在受保护的覆盖无效更新(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);
        }
 }

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

于 2012-04-22T00:07:16.180 回答