繁体   English   中英

游戏完成时,如何保存在文本框中输入的名称和计时器上的时间

[英]How to save name that is entered in text box and the time that is on the timer when the game is completed

在那一刻,我在c#窗口形式中有一个比赛图片游戏,带有一个计时器,当所有比赛完成时,计时器就会停止。 我想知道如何节省时间和在文本文件中使用btn_player输入的名称。 文本文件存储在(@“ E:\\ Match Picture \\ Public \\ Files \\ playerdetails.txt”);

using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace Match_Picture
{
public partial class Form1 : Form
{
    string Temp_Tag = "";
    int Number_of_Images = 0;
    Label Clicked_Picture = new Label();
    Label Temp_Pic = new Label();
    int Num_Correct = 0;




    public Form1()
    {
        InitializeComponent();
    }

    private void Picture_Click(object sender, EventArgs e)
    {
        Clicked_Picture = (Label)sender;

        Number_of_Images++;



        if (Number_of_Images < 3)
        {
            Clicked_Picture.Text = Clicked_Picture.Tag.ToString();


            if (Number_of_Images == 1)
            {
                Temp_Tag = Clicked_Picture.Tag.ToString();
                Temp_Pic = Clicked_Picture;



            }
            else
            {
                if (Temp_Tag != Clicked_Picture.Tag.ToString())
                {
                    tmr_Delay.Enabled = true;
                    scorecounter.Text = Convert.ToString(Convert.ToInt32(scorecounter.Text) - 20);
                }
                else
                {
                    Num_Correct++;
                    scorecounter.Text = Convert.ToString(Convert.ToInt32(scorecounter.Text) + 50);
                    lbl_Matches.Text = Num_Correct.ToString();
                    if (Num_Correct == 8)
                    {
                        tmr_1.Stop();
                        MessageBox.Show("Congratulations, all matches complete");
                        MessageBox.Show("Number of seconds to match all the pictures: " + i);


                        this.Close();
                    }
                }
                Number_of_Images = 0;
            }
        }
    }

    private void tmr_Delay_Tick(object sender, EventArgs e)
    {
        Clicked_Picture.Text = "R";
        Temp_Pic.Text = "R";

        tmr_Delay.Enabled = false;
    }

    int i = 0;
    private void timer1_Tick(object sender, EventArgs e)
    {
        i++;
        lbl_time.Text = i.ToString() ;
    }




    private void btn_player_Click(object sender, EventArgs e)
    {

    }

您可以使用StreamWriter类来执行所需的操作。

using (StreamWriter writer = new StreamWriter(@"E:\Match Picture\Public\Files\playerdetails.txt"))
{
    writer.WriteLine("") // This is where you will write what you want to that file     
}

作为MethodMan,您可以使用EndTime-StartTime以及任何其他必要的转换,以便以所需的格式(分钟,秒等)获取它。

这段代码将简单地写入文本文件。 如果它不存在,它将创建它;如果文件不存在,它将覆盖文件。 您需要进行适当的检查以确保不覆盖它,或者可以使用

new StreamWriter(@"E:\Match Picture\Public\Files\playerdetails.txt", true)

如果要附加文件而不是覆盖文件。 有关StreamWriter类的完整文档,请参见http://msdn.microsoft.com/zh-cn/library/system.io.streamwriter%28v=vs.110%29.aspx

顺便说一句,如果您想保留高分的记录,则还应该研究StreamReader类。 您可以将文件读入PlayerTime的词典中,并检查文件中是否存在玩家,并且只有在其得分超过最高Time才调整其得分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM