ここで要求されたように、再び私はあなたがラップタイムについて上記提起質問に答えるのです、あなたの例の空XNAプロジェクトで必要とされるであろう実装です:
/*
NEW MEMBERS FOR YOU TO IMAGINE YOU HAVE SOME MEANINGFUL RACING GAME
*/
private Vector2 yourPosition;
private Texture2D yourCar;
private Texture2D yourTrack;
private Rectangle yourStartingLine;
private DateTime currentLapTimestamp = DateTime.Now();
private TimeSpan currentLapTime = new TimeSpan();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// Put your starting line somewhere
yourStartingLine = new Rectangle(100,100,10,100)
base.Initialize();
}
protected override void LoadContent()
{
yourCar = Content.Load<Texture2D>("YourCarAssetName")
yourTrack = Content.Load<Texture2D>("YourTrackAssetName")
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Check your keyboard input
UpdatePosition(Keyboard.GetState());
if(HasPassedStartingLine())
{ UpdateLapTime(); }
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawTrack();
DrawCar();
DrawLapTime();
base.Draw(gameTime);
}
/*
NEW METHODS FOR YOU TO PRETEND DO STUFF
*/
private void UpdateLapTime()
{
var newTimestamp = DateTime.Now;
currentLapTime = newTimestamp - currentLapTimestamp;
}
private bool HasPassedStartingLine()
{
// Work out if you have passed the starting line
// and make sure you dont register it again until
// they have passed the finish line
}
private void UpdatePosition(KeyboardState keyboardState)
{
// Assumes you work out how to handle your players
// controls to move the position of the car
}
private void DrawTrack()
{
// Your rendering logic for the track
}
private void DrawCar()
{
// Your rendering logic for the car
}
private void DrawLapTime()
{
// your rendering logic for the lap time
}
を私はあなたの質問を変更する必要があると思います現在の質問は、どのくらいラップが取られたかをどのように見出すかです。上のコードから、ゲームの実装が欠落しています...あなたがレーシングゲームをどのように組み立てるかを知りたければ、問題は1)画面に車を描くにはどうすればいいですか?2)画面上で車を動かすにはどうしたらいいですか?3)動く物体の衝突をどうやって計算するのですか?これらの3つの質問に対する回答が得られたら、以下の答えを使用することができます。 – Grofit