движение спрайта в одноигровой игре не плавное с использованием дельты - PullRequest
0 голосов
/ 27 марта 2019

Я новичок в моногам и c #

Я обновлял спрайт X для движения, однако он не плавный, поэтому я попытался использовать дельта-игровое время, однако это не выглядит немного лучше, я, без сомнения, делаю ошибку новичка, но не могу найти ее, любая помощь будет Буду очень признателен, спасибо

Мое обновление выглядит следующим образом

var delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds;


            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                vecPlayerPostion.X -= 1  * delta;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
            {
                vecPlayerPostion.X += 1 * delta;
            }

и ничья

spriteBatch.Draw(texPlayerTexture, vecPlayerPostion, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);

и при необходимости весь мой код выглядит следующим образом

Game1.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SpaceShip
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        PlayerShip PlayerShipUser1;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

            graphics.PreferredBackBufferWidth = 1920;  // set this value to the desired width of your window
            graphics.PreferredBackBufferHeight = 1080;   // set this value to the desired height of your window
            graphics.IsFullScreen = true;
            graphics.ApplyChanges();

            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


            PlayerShipUser1 = new PlayerShip();
            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);

            PlayerShipUser1.texPlayerTexture = Content.Load<Texture2D>("player");

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific 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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here
            PlayerShipUser1.Update(gameTime);

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

            spriteBatch.Begin();

            PlayerShipUser1.Draw(spriteBatch);

            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

playershipclass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
namespace SpaceShip
{
    class PlayerShip
    {
        public Texture2D texPlayerTexture;

        private Vector2 vecPlayerPostion;

        public PlayerShip()
        {
            vecPlayerPostion.X = 800;
            vecPlayerPostion.Y = 900;

        }

        public void Update(GameTime gameTime)
        {

            var delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds;


            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                vecPlayerPostion.X -= 1  * delta;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
            {
                vecPlayerPostion.X += 1 * delta;
            }

        }


        public void Draw(SpriteBatch spriteBatch)
        {

            spriteBatch.Draw(texPlayerTexture, vecPlayerPostion, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);


        }


    }
}

1 Ответ

2 голосов
/ 27 марта 2019

Единственная проблема с вашим кодом - слишком высокая скорость движения.При 60 кадрах в секунду gameTime.ElapsedGameTime.TotalMilliseconds равно 16. Это приводит к скачку 16 пикселей на шаг (или 1000 пикселей в секунду), что нарушает визуальную непрерывность.

Обычно я ограничиваю скорость объектов до 4-8 пикселей за шаг.Чтобы реализовать это, измените множитель дельты с 1 на 0.25f или 0.5f.

// Set speed to 4 pixels per step
vecPlayerPostion.X -= 0.25f  * delta;

Если вам нужна скорость, чтобы оставаться неизменной (быстрой), вы можете нарисовать промежуточные кадрыв том же шаге с уменьшенной альфа.Этот метод вызывает размытие при движении, но уменьшает появление скачков.

Обновление playershipclass.cs выглядит следующим образом:

  public Texture2D texPlayerTexture;

  private Vector2 vecPlayerPostion;

  // Store the previous position
  private Vector2 vecPlayerLastPostion;

  public PlayerShip()
  // ...

  public void Update(GameTime gameTime)
  {
     // Store the value
     vecPlayerLastPostion = vecPlayerPostion;

     var delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds;

// ...

  public void Draw(SpriteBatch spriteBatch)
  {
     // Draw the texture halfway between the previous position and current at .4f alpha
     spriteBatch.Draw(texPlayerTexture, (vecPlayerPostion + vecPlayerLastPostion) / 2, null, new Color (1,1,1,0.4f), 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);

     spriteBatch.Draw(texPlayerTexture, vecPlayerPostion, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);

  }


Третьим решением было бы ограничение частоты кадров:

В конструкторе для игры1:

// ...
 public Game1()
 {
    graphics = new GraphicsDeviceManager(this);
    graphics.PreferredBackBufferWidth = 1920;  // set this value to the desired width of your window
    graphics.PreferredBackBufferHeight = 1080;   // set this value to the desired height of your window
    graphics.IsFullScreen = true;
    graphics.ApplyChanges();

    // Remove 60 fps target
    IsFixedTimeStep = false;

    // don't wait on vsync(will limit to 60 fps)
    graphics.SynchronizeWithVerticalRetrace = false;

    Content.RootDirectory = "Content";
        }
...