1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| KeyboardState keyboardState = Keyboard.GetState();
playerSpeed = Vector2.Zero;
if (keyboardState.IsKeyDown(Keys.Up))
{
playerSpeed.Y--;
direction.Y--;
}
if (keyboardState.IsKeyDown(Keys.Down))
{
playerSpeed.Y++;
direction.Y++;
}
if (keyboardState.IsKeyDown(Keys.Left))
{
playerSpeed.X--;
direction.X--;
}
if (keyboardState.IsKeyDown(Keys.Right))
{
playerSpeed.X++;
direction.X++;
}
if (playerSpeed.Length() > 0f)
{
playerSpeed.Normalize();
}
if (playerSpeed.X != 0)
{
directionTemp.X = playerSpeed.X;
}
if (playerSpeed.Y != 0)
{
directionTemp.Y = playerSpeed.Y;
}
direction.Y = MathHelper.Clamp(direction.Y, -PLAYER_SPEED_MAX, PLAYER_SPEED_MAX);
direction.X = MathHelper.Clamp(direction.X, -PLAYER_SPEED_MAX, PLAYER_SPEED_MAX);
position.X += direction.X * Math.Abs(directionTemp.X);
position.Y += direction.Y * Math.Abs(directionTemp.Y);
position.X = MathHelper.Clamp(position.X, 0, ScreenWidth - texturePlayer.Width);
position.Y = MathHelper.Clamp(position.Y, ScreenHeight / 2, ScreenHeight - texturePlayer.Height);
if (keyboardState.IsKeyUp(Keys.Up) && direction.Y < 0)
{
direction.Y += 0.1f;
if (direction.Y > 0)
direction.Y = 0;
}
if (keyboardState.IsKeyUp(Keys.Down) && direction.Y > 0)
{
direction.Y -= 0.1f;
if (direction.Y < 0)
direction.Y = 0;
}
if (keyboardState.IsKeyUp(Keys.Left) && direction.X < 0)
{
direction.X += 0.1f;
if (direction.X > 0)
direction.X = 0;
}
if (keyboardState.IsKeyUp(Keys.Right) && direction.X > 0)
{
direction.X -= 0.1f;
if (direction.X < 0)
direction.X = 0;
}
joueur.Position = position;
currentBulletTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (keyboardState.IsKeyDown(Keys.Space))
{
if (currentBulletTime >= TIME_FOR_NEXT_BULLET)
{
currentBulletTime = 0f;
ballesJoueur.Add(new BulletPlayer(textureBulletPlayer, new Vector2(joueur.Position.X + texturePlayer.Width / 2 - textureBulletPlayer.Width / 2, joueur.Position.Y - textureBulletPlayer.Height)));
}
} |
Partager