我正在创建一个可以加载和保存的游戏。我想要详细说明用户玩了多长时间。我有类似以下内容来确定他们在游戏上花费的时间。如何保存和加载总时间?我知道我想把这个值加到Properties.Settings
来加载和保存,但我不知道如何执行它。
private void Form1_Load(object sender, EventArgs e)
{
//Load previous time
stopWatch.Start();
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && pbPlayer.Bounds.IntersectsWith(pnlSave.Bounds))
{
string timePlayed;
stopWatch.Stop();
TimeSpam timeSpan = stopWatch.Elapsed; //+ Their previous time
timePlayed = string.Format("{0:D2}:{1:D2}:{2:D2}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
MessageBox.Show("You've played for" + timePlayed + ". Game saved!"
}
//Save their total time
}
答案 0 :(得分:0)
我明白了,这解决了我的答案;
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && pbPlayer.Bounds.IntersectsWith(pnlSave.Bounds))
{
string timePlayed;
stopWatch.Stop();
TimeSpam totalTime = stopWatch.Elapsed + Properties.Settings.savedPlayTime;
timePlayed = string.Format("{0:D2}:{1:D2}:{2:D2}", Properties.Settings.Default.savedPlayTime.Hours, Properties.Settings.Default.savedPlayTime.Minutes, Properties.Settings.Default.savedPlayTime.Seconds);
if (MessageBox.Show("Are you sure you wish to overwrite your last save of " + timePlayed + "?")== DialogResult.OK)
{
Properties.Settings.Default.savedPlayTime = totalTime;
Properties.Settings.Default.Save();
stopWatch.Reset();
MessageBox.Show("Game saved!");
}
stopWatch.Start();
}
}
答案 1 :(得分:0)
干得好。我可能是错的,但我认为目的是显示他们保存时的总时间。 timePlayed不应该是totalTime的字符串格式吗?
timePlayed = string.Format(“{0:D2}:{1:D2}:{2:D2}”,totalTime.Hours,totalTime.Minutes,totalTime.Seconds);