c# - 移动键不按怎么让小船漂流几秒
问题描述
所以我正在尝试制作一个 2D 游戏,其中玩家从头顶控制一艘船。我有它,所以当按下“A”和“D”键时船会旋转,当按下“W”和“S”键时会向前/向后移动。我想让船在释放前进和后退键后以降低的速度漂移几秒钟,但我不知道该怎么做。这是我的 Ship 类代码。我正在考虑以某种方式使用“While”循环,但我不太确定如何。我还希望船头不断更新以朝着创建的旋转方向移动。
我在 main 方法中有一个枚举,这是“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);
解决方案
一个简单的解决方案是拥有 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
}
类似的东西 - 如果行进距离对于独立于帧率很重要,你必须考虑经过的时间 - 也许使用物理引擎为你做
推荐阅读
- ios - iOS:获取动态数量的子视图以在水平和垂直方向上占用superview中的最大空间
- c# - 在点网核心中初始化后台服务的正确方法
- python - 如何在windows中并行执行多个python代码
- regex - 正则表达式在 openrefine 中搜索和替换表情符号
- loops - 在 for-each 循环中进行类型转换
- nginx - Nginx upd 负载平衡请求仅重定向到一台服务器
- javascript - JavaScript - 在从 JSON 数组中获取数据时用锚标记替换字符串
- javascript - 在 JavaScript 对象中按值查找
- python - 3D 协方差矩阵 - 向量化 python
- javascript - 为什么 consumer.py 中的事件处理函数被多次调用,然后 web-socket 连接被强制终止?