//2014.04.14
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static int cnt=0;
static int time = 100;//初始化时间为100,递减到0
private void button1_Click(object sender, EventArgs e)
{
//记录点击按钮式次数,label3 show出来
timer1.Enabled = true;//开始计时
cnt++;//记录点击次数
label3.Text = cnt.ToString();//label3show出点击的对应数字
}
private void timer1_Tick(object sender, EventArgs e)
{
if(time>=0)//时间>=0的,进度条继续递减
{
progressBar1.Value = time;
time--;
}
else//进度条<0,执行动作
{
timer1.Enabled=false;
button1.Enabled = false;
string str = "";
StreamWriter sw = new StreamWriter("D:\\score.txt", true);
str = label3.Text;
sw.WriteLine(str);
sw.Close();
if (MessageBox.Show("Play again?", "One_Touch Game", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == System.Windows.Forms.DialogResult.OK)
{
//继续开始,初始化初值
cnt = 0;
time = 100;
label3.Text = "0";
button1.Enabled = true;
//继续开始,show出top5的排行榜
StreamReader sr = new StreamReader("D:\\score.txt");
int num = 0; int i = 0;
while (!sr.EndOfStream) // 每次讀取一行,直到檔尾
{
num=GetRows("D:\\score.txt");//path存进GetRows方法
int[] j = new int[num-1];//定义一个数组,存i的数
for (int k = 0; k < j.Length; k++)//把文档中的数字存进数组
{
string line = sr.ReadLine();// 讀取文字到 line 變數
i = int.Parse(line);//转成int
j[k] += i;
i = 0;
}
Array.Sort(j);//递增排序
Array.Reverse(j);//递减排序
label4.Text = "TOP 1-->" + j[0].ToString();
label5.Text = "TOP 2-->" + j[1].ToString();
label6.Text = "TOP 3-->" + j[2].ToString();
label7.Text = "TOP 4-->" + j[3].ToString();
label8.Text = "TOP 5-->" + j[4].ToString();
}
sr.Close();// 關閉串流
}
else
Application.Exit();//退出程式
}
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();//游戏暂停按键
// timer1.Enabled = false;
}
public int GetRows(string FilePath)//读取文档,获得文档的行数,减去1为真实的行数
{
using (StreamReader read = new StreamReader(FilePath, Encoding.Default))
{
return read.ReadToEnd().Split('\n').Length;
}
}
}
}
程式码运行结果: