I have declared the following variables so I can move my player around:
private double player1Y = 0;
private const int PLAYER_SPEED = 200;
In my Update
function, I move the player like so:
protected override void Update(GameTime gameTime)
{
double deltaTime = gameTime.ElapsedGameTime.TotalSeconds;
if (Keyboard.GetState().IsKeyDown(Keys.Up))
player1Y += -PLAYER_SPEED * deltaTime;
else if (Keyboard.GetState().IsKeyDown(Keys.Down))
player1Y += PLAYER_SPEED * deltaTime;
base.Update(gameTime);
}
I then draw my player based on their position:
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
// NOTE: `whiteRectangle` is just a simple `Texture2D`.
spriteBatch.Draw(whiteRectangle, new Rectangle(0, Convert.ToInt32(player1Y), PLAYER_WIDTH, PLAYER_HEIGHT), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
While everything seems to work as intended, I have a feeling of unease that I am doing something wrong, particularly with how I am converting my player’s Y position (a double
) into an int
at Draw time, and how I am using double
instead of int
for my player’s position.
I only intend to draw my player’s position in whole numbers because I’m trying to make a simple pixel Pong clone, but since deltaTime
isn’t a whole number I thought using double
would make things easier.
Is there something wrong with what I am doing, or is everything just fine the way they are?