




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、精選優質文檔-傾情為你奉上精選優質文檔-傾情為你奉上專心-專注-專業專心-專注-專業精選優質文檔-傾情為你奉上專心-專注-專業C#實現三匹馬賽跑,使用進度條和Timer實現編寫比賽代碼:3匹馬比賽,隨機產生勝者,使用進度條和Timer實現:在Visual Studio 2005 中新建WinForm應用程序:界面如下:用label1,label2,label3來顯示三匹馬的速度 窗體還有3個Timer控件,分別作為三匹馬的時鐘:源代碼如下:using System;using System.Collections.Generic;using System.ComponentModel;usin
2、g 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; / / 窗體Load事件 設置時間間隔為0.1
3、秒 為進度條賦值最大值 / / / 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; / / 開始比賽事件:文本提示框恢復默認 進度條歸零, / 重新隨機出三匹馬的速度 先停止時鐘 再啟動計時器 / / / private
4、 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
5、 (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的
6、速度:0米每秒, horse1); label3.Text = string.Format(馬3的速度:0米每秒, horse2); timer1.Stop();/停止計時器 timer2.Stop(); timer3.Stop(); timer1.Start();/啟動計時器 比賽開始 timer2.Start(); timer3.Start(); / / 時鐘事件 當馬已跑完時,計時器停止運行 / / / private void timer1_Tick(object sender, EventArgs e) secondCount1 += 0.1M; decimal x = horse0
7、 * 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, Ev
8、entArgs 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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 目視化管理與產品開發考核試卷
- 銻冶煉生產數據分析與應用技巧考核試卷
- 管道工程法律法規政策深度研究與探討考核試卷
- 鐵路軌道電路調試技術考核試卷
- 航空航天器材料與工藝考核試卷
- 新生兒黃疸檢測的臨床意義
- 肺部真菌感染診斷與治療進展
- 轉運呼吸機操作規范
- 防呼吸道傳染病科普講座
- 視網膜動脈阻塞性疾病
- 糖尿病老年人飲食護理
- 解憂雜貨鋪ppt讀書分享
- 銀行對公業務培訓課件
- 廈門大學2021年826物理化學考研真題
- 汽修企業安全生產雙重預防機制培訓資料
- 信息安全、網絡安全和隱私保護-信息安全控制風險清單(雷澤佳編制2024A0)
- 農村財會培訓課件
- 建筑施工企業特殊過程確認記錄
- 2024年A證(安全員)作業模擬考試題庫全套
- 簡單咨詢費合同范本英文版
- 廣東省深圳市羅湖區螺嶺外國語實驗學校小學五年級下冊期末語文試題
評論
0/150
提交評論