C# 40f等于40像素?

C# 40f等于40像素?,c#,xna,C#,Xna,如果我按一次a按钮,我想让我的角色进行40像素的高度跳跃。但我不知道40f是否等于40像素? 你觉得我的代码怎么样?怎么了? 此外,我希望我的程序在每台计算机上以相同的速度运行 public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D image, water; float Gravity = 5.0F; float

如果我按一次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);
}
}
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
纹理2D图像,水;
浮子重力=5.0F;
浮子加速度=20.0F;
矢量2位置=新矢量2(1200720);
矢量2速度;
浮动旋转=0;
精灵效果翻转;
矢量2速度=新矢量2(0,0);
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
graphics.PreferredBackBufferWidth=1280;
graphics.PreferredBackBufferHeight=720;
}
受保护的覆盖无效初始化()
{
base.Initialize();
}
受保护的覆盖void LoadContent()
{   
spriteBatch=新spriteBatch(图形设备);
图像=Content.Load(“cartondolphin”);
水=含量。负荷(“背景”);
翻转=精灵效果。无;
}
受保护覆盖无效更新(游戏时间游戏时间)
{
浮动速度x=0f;
浮动速度y=0f;
浮动时间=(浮动)gameTime.ElapsedGameTime.TotalSeconds;
KeyboardState kbState=Keyboard.GetState();
if(kbState.IsKeyDown(Keys.Left))
{
旋转=0;
翻转=精灵效果。无;
速度x+=-5f;
} 
if(kbState.IsKeyDown(Keys.Right))
{
旋转=0;
翻转=SpriteEffects.flip水平;
速度x+=5f;
} 
//如果海豚在水下就跳
如果(位置Y>=670)
{
if(kbState.IsKeyDown(Keys.A))
{     
如果(翻转==SpriteEffects.None)
{ 
速度y+=40f;
}
其他的
{ 
速度y+=40f;
}
}
} 
其他的
{
速度y+=-10f;
}
浮动三角洲=0;
浮动deltaX=0;
deltaY=重力*(浮动)gameTime.ElapsedGameTime.TotalSeconds;
deltaX+=VelocityX*(浮点)gameTime.ElapsedGameTime.TotalSeconds*加速度;
deltaY+=-VelocityY*(float)gameTime.ElapsedGameTime.TotalSeconds*加速度;
速度=新矢量2(速度.X+deltaX,速度.Y+deltaY);
位置+=速度*(浮动)gameTime.ElapsedGameTime.TotalSeconds;
速度X=0;
if(Position.Y+image.Height/2>graphics.PreferredBackBufferHeight)
Position.Y=graphics.PreferredBackBufferHeight-image.Height/2;
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
spriteBatch.Draw(水,新矩形(0,graphics.PreferredBackBufferHeight-100,graphics.PreferredBackBufferWidth,100),Color.White);
spriteBatch.Draw(图像、位置、空值、颜色、白色、MathHelper.ToRadians(旋转)、新矢量2(图像宽度/2、图像高度/2)、1、翻转、1);
spriteBatch.End();
基础。抽签(游戏时间);
}
}

问题在于你没有考虑
游戏时间。如果CPU速度快的计算机执行该代码的速度是速度慢的PC的两倍,则速度值的增加速度将是速度慢的PC的两倍

当您有如下代码时:

VelocityX += 5f;
您需要跟踪上一次的
游戏时间
,获得帧之间的差异,并将其乘以常数。(从中抓取一些代码)


代码有什么问题?如果你想让别人简单地看一下,我建议你把这个问题带到codereview.stackexchange.com。如果你的代码中有
40f
,那就意味着数字40被视为浮点数。这类似于写40.0,数字保持不变,但现在很清楚,它也可以是非整数(例如40.5)。如果你的
VelocityY
是以像素为单位的,那么添加
40f
将改变40像素(每次迭代/更新)。我假设单位是像素,因为它似乎是这里使用的最小任意长度单位,但我不完全理解这个问题。40f等于40像素?对
int updateTime = gt.ElapsedGameTime.TotalMilliseconds - oldgt.ElapsedGameTime.TotalMilliseconds;

float timeScalar = updateTime / AVG_FRAME_TIME;

VelocityX += 5f * timeScalar;