如何在移动键未按下后让船漂移几秒钟

时间:2018-05-20 00:36:50

标签: c# monogame

所以我试图制作一个2D游戏,其中玩家从头顶控制船只。我有它,所以当船的旋转时,船会旋转。和" D"键被按下,并且当" W"和" S"键被按下了。在向前和向后键释放后,我想让船在减速时漂移几秒钟,但我不知道该怎么做。这是我的Ship类的代码。我正在考虑使用"而#34;以某种方式循环,但我不太确定如何。我也希望船头能够不断更新,以便朝着创造的旋转方向前进。

我在主要方法中有一个枚举,这是" Dir"来自。

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;


//width = 62 pixels
//height = 179 pixels

namespace my_first_game
{
class Ship
{

    private Vector2 position = new Vector2(300, 300);
    private int radius = 50;
    private bool ismoving = false;
    private Dir direction = Dir.down;
    private int health = 3;
    private float speed = 200;
    public float angle = 0;
    public double turntime = .01d;
    private float drifting = 0;
    private bool isdrifting = false;

    Vector2 Boat_Tip = new Vector2(0, 0);



   // Constructor for the ship class
   public Ship(int shiphealth)
    {
        shiphealth = health;
    }

    /* public float Get_angle
     {
         get { return angle; }

     }
     */

    //get and set health
    public int Health
    {
        get { return health; }
        set { value = health; }
    }

    // get the ship's position
    public Vector2 Position
    {
        get{ return position; }
    }

    //update loop for the ship's state
    public void update_ship(GameTime gameTime)
    {
        KeyboardState kstate = Keyboard.GetState();
        float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
        Boat_Tip.X = position.X;
        Boat_Tip.Y = position.Y;

        //if keys are pressed, the ship moves until the keys are unpressed

        if (kstate.IsKeyDown(Keys.W))
        {
            direction = Dir.up;
            ismoving = true;
            isdrifting = true;
        }
        if (kstate.IsKeyDown(Keys.A))
        {
            direction = Dir.left;
            ismoving = true;
        }
        if (kstate.IsKeyDown(Keys.D))
        {
            direction = Dir.right;
            ismoving = true;
        }
        if (kstate.IsKeyDown(Keys.S))
        {
            direction = Dir.down;
            ismoving = true;
        }


        //"ismoving" is used as a flag to move the ship if keys are held
        //angle also rotates the boat when "A" and "D" are pressed
        if (ismoving) {

            switch (direction) {
                case Dir.up:
                    position.Y -= speed * dt;
                    ismoving = false;
                    break;
                case Dir.down:

                    position.Y += speed * dt;
                    ismoving = false;
                    break;
                case Dir.left:
                    position.X -= speed *(float)turntime * dt;
                    angle -= (float).01;
                    ismoving = false;
                    break;
                case Dir.right:
                    position.X += speed *(float)turntime * dt;
                    angle += (float).01;
                    ismoving = false;
                    break;
                default: break;
            }
        }
    }
}

}

不确定这是否有帮助,但这是我的船舶绘制方法:

spriteBatch.Draw(ship_sprite, ship.Position, ship_rectangle, Color.White, 
ship.angle, origin, 1.0f, SpriteEffects.None, 1);

1 个答案:

答案 0 :(得分:0)

一个简单的解决方案是具有current_speed和target_speed。您的按键仅设置了target_speed,然后在更新中将current_speed设置为target_speed

float cur_speed;
float trg_speed;

void Update()
{
    if( keypressed) trg_speed = 200f;
    else trg_speed = 0f;

    cur_speed = lerp(cur_speed,trg_speed,0.2f);

    // do your other movement here depending on cur_speed
}

类似的东西-如果行进距离对于保持帧速率很重要,则必须考虑经过的时间-也许可以使用物理引擎为您完成