C#三匹马赛跑-使用进度条和Timer实现(共5页).doc
精选优质文档-倾情为你奉上C#实现三匹马赛跑,使用进度条和Timer实现编写比赛代码:3匹马比赛,随机产生胜者,使用进度条和Timer实现:在Visual Studio 2005 中新建WinForm应用程序:界面如下:用label1,label2,label3来显示三匹马的速度 窗体还有3个Timer控件,分别作为三匹马的时钟:源代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace WinApp public partial class Form赛马 : Form public Form赛马() InitializeComponent(); int horse = new int3; /马的速度 decimal secondCount1 = 0; decimal secondCount2 = 0; decimal secondCount3 = 0; / <summary> / 窗体Load事件 设置时间间隔为0.1秒 为进度条赋值最大值 / </summary> / <param name="sender"></param> / <param name="e"></param> private void Form赛马_Load(object sender, EventArgs e) progressBar1.Maximum = 100; progressBar2.Maximum = 100; progressBar3.Maximum = 100; timer1.Interval = 100;/间隔 0.1秒 timer2.Interval = 100; timer3.Interval = 100; / <summary> / 开始比赛事件:文本提示框恢复默认 进度条归零, / 重新随机出三匹马的速度 先停止时钟 再启动计时器 / </summary> / <param name="sender"></param> / <param name="e"></param> private void btnBegin_Click(object sender, EventArgs e) richTextBox1.Text = "" richTextBox2.Text = "" richTextBox3.Text = "" progressBar1.Value = 0; progressBar2.Value = 0; progressBar3.Value = 0; secondCount1 = 0; secondCount2 = 0; secondCount3 = 0; /清空重新赋值 bool b = new bool21; Random ran = new Random(); for (int i = 0; i < horse.Length; i+) int number = ran.Next(1, 21);/假设马的速度为120之间 各不相同 bool bx = bnumber; while (bx) /如果bnumber 为true 说明 number被取到 请重新取数 number = ran.Next(1, 21); bx = bnumber; bnumber = true; horsei = number; label1.Text = string.Format("马1的速度:0米每秒", horse0); label2.Text = string.Format("马2的速度:0米每秒", horse1); label3.Text = string.Format("马3的速度:0米每秒", horse2); timer1.Stop();/停止计时器 timer2.Stop(); timer3.Stop(); timer1.Start();/启动计时器 比赛开始 timer2.Start(); timer3.Start(); / <summary> / 时钟事件 当马已跑完时,计时器停止运行 / </summary> / <param name="sender"></param> / <param name="e"></param> private void timer1_Tick(object sender, EventArgs e) secondCount1 += 0.1M; decimal x = horse0 * secondCount1; if (x >= progressBar1.Maximum) progressBar1.Value = progressBar1.Maximum; richTextBox1.Text = string.Format("马1已跑完全程0米,速度:1米每秒,用时2秒", 100, horse0, secondCount1); timer1.Enabled = false; else progressBar1.Value = (int)x; Application.DoEvents(); private void timer2_Tick(object sender, EventArgs e) secondCount2 += 0.1M; decimal y = horse1 * secondCount2; if (y >= progressBar2.Maximum) progressBar2.Value = progressBar2.Maximum; richTextBox2.Text = string.Format("马2已跑完全程0米,速度:1米每秒,用时2秒", 100, horse1, secondCount2); timer2.Enabled = false; else progressBar2.Value = (int)y; Application.DoEvents(); private void timer3_Tick(object sender, EventArgs e) secondCount3 += 0.1M; decimal z = horse2 * secondCount3; if (z >= progressBar3.Maximum) progressBar3.Value = progressBar3.Maximum; richTextBox3.Text = string.Format("马3已跑完全程0米,速度:1米每秒,用时2秒", 100, horse2, secondCount3); timer3.Enabled = false; else progressBar3.Value = (int)z; Application.DoEvents(); 程序运行结果如图:再次随机:专心-专注-专业