-1

如果我按一次 A 按钮,我希望我的角色跳高 40 像素。但不知道40f是否等于40像素?你觉得我的代码怎么样?怎么了?另外,我希望我的程序在每台计算机上以相同的速度运行。

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D image, water;
float Gravity = 5.0F;
float Acceleration = 20.0F;
Vector2 Position = new Vector2(1200,720);
Vector2 Velocity;
float rotation = 0;
SpriteEffects flip;
Vector2 Speed = new Vector2(0, 0);

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
    graphics.PreferredBackBufferWidth = 1280;
    graphics.PreferredBackBufferHeight = 720;
}

protected override void Initialize()
{
    base.Initialize();
}

protected override void LoadContent()
{   
    spriteBatch = new SpriteBatch(GraphicsDevice);
    image = Content.Load<Texture2D>("cartoondolphin");
    water = Content.Load<Texture2D>("background");
    flip = SpriteEffects.None;
}

protected override void Update(GameTime gameTime)
{
    float VelocityX = 0f;
    float VelocityY = 0f;

    float time = (float)gameTime.ElapsedGameTime.TotalSeconds;
    KeyboardState kbState = Keyboard.GetState();
    if(kbState.IsKeyDown(Keys.Left)) 
    {
        rotation = 0;
        flip = SpriteEffects.None;
        VelocityX += -5f;
    } 

    if(kbState.IsKeyDown(Keys.Right)) 
    {
        rotation = 0;
        flip = SpriteEffects.FlipHorizontally;
        VelocityX += 5f;
    } 

    // jump if the dolphin is under water
    if(Position.Y >= 670)
    {
        if (kbState.IsKeyDown(Keys.A))
        {     
            if (flip == SpriteEffects.None)
            { 
              VelocityY += 40f;
            }
            else
            { 
              VelocityY += 40f;
            }
        }
    } 
    else 
    {
        VelocityY += -10f;
    }

    float deltaY = 0;
    float deltaX = 0;

    deltaY = Gravity * (float)gameTime.ElapsedGameTime.TotalSeconds;

    deltaX += VelocityX * (float)gameTime.ElapsedGameTime.TotalSeconds * Acceleration;
    deltaY += -VelocityY * (float)gameTime.ElapsedGameTime.TotalSeconds * Acceleration;

    Speed = new Vector2(Speed.X + deltaX, Speed.Y + deltaY);

    Position += Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;

    Velocity.X = 0;

    if (Position.Y + image.Height/2 > graphics.PreferredBackBufferHeight)
        Position.Y = graphics.PreferredBackBufferHeight - image.Height/2;

    base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    spriteBatch.Begin();
    spriteBatch.Draw(water, new Rectangle(0, graphics.PreferredBackBufferHeight -100, graphics.PreferredBackBufferWidth, 100), Color.White);
    spriteBatch.Draw(image, Position, null, Color.White, MathHelper.ToRadians(rotation), new Vector2(image.Width / 2, image.Height / 2), 1, flip, 1);
    spriteBatch.End();           

    base.Draw(gameTime);
}
}
4

1 回答 1

0

问题是你没有考虑gameTime到。如果具有快速 CPU 的计算机执行代码的速度是速度较慢的 PC 的两倍,那么速度值将增加两倍(相对于较慢的 PC)。

当您有如下代码时:

VelocityX += 5f;

您需要跟踪 last gameTime,并获取帧之间的差异,然后将其乘以您的常数。(从Xna抓取的一些代码将重力添加到 2d 精灵)

int updateTime = gt.ElapsedGameTime.TotalMilliseconds - oldgt.ElapsedGameTime.TotalMilliseconds;

float timeScalar = updateTime / AVG_FRAME_TIME;

VelocityX += 5f * timeScalar;
于 2012-12-08T23:09:49.507 回答