IT项目之旅(二)篮球计分器(分析、设计、实现)
一.???? 總體設計
?? 籃球計分器的實現主要包括客戶端和服務器兩部分,按照客戶/服務器的模式進行工作,提供交互式的訪問,在INTERNET使用廣泛。通信協議主要采用TCP協議。主要實現了籃球計分器的幾個關鍵功能:服務器段與客戶端的通信、七段碼、斷電保存、廣告、動畫、聲音。這是07年5月份課余時間完成的微機實踐項目,其主要技術還是在于Socket編程。
?
二、開發環境及主要技術說明
VS2005,主要利用Socket編程,其中還涉及到線程方面的內容
?? Socket套接字的工作原理:通過局域網互聯網進行通信,至少采用一對套接字,其中一個運行于客戶端,另一個運行于服務器端。根據連接啟動的方式以及本地套接字要連接的對象連接過程分為三個步驟:服務端接聽、客戶端接聽和連接請求
?三、具體實現過程?
實現的具體過程:
?
(1)聲音的實現:
主要設計思想:根據服務器端傳過來的控制信息,調用API的接口來實現聲音的播放
實現過程:??
聲音的實現?1?public?enum?PlaySoundFlags?:?int
?2
?3????????{
?4
?5????????????SND_SYNC?=?0x0000,????/**//*?play?synchronously?(default)?*/?//同步?
?6
?7????????????SND_ASYNC?=?0x0001,????/**//*?play?asynchronously?*/?//異步?
?8
?9????????????SND_NODEFAULT?=?0x0002,????/**//*?silence?(!default)?if?sound?not?found?*/
10
11????????????SND_MEMORY?=?0x0004,????/**//*?pszSound?points?to?a?memory?file?*/
12
13????????????SND_LOOP?=?0x0008,????/**//*?loop?the?sound?until?next?sndPlaySound?*/
14
15????????????SND_NOSTOP?=?0x0010,????/**//*?don't?stop?any?currently?playing?sound?*/
16
17????????????SND_NOWAIT?=?0x00002000,?/**//*?don't?wait?if?the?driver?is?busy?*/
18
19????????????SND_ALIAS?=?0x00010000,?/**//*?name?is?a?registry?alias?*/
20
21????????????SND_ALIAS_ID?=?0x00110000,?/**//*?alias?is?a?predefined?ID?*/
22
23????????????SND_FILENAME?=?0x00020000,?/**//*?name?is?file?name?*/
24
25????????????SND_RESOURCE?=?0x00040004????/**//*?name?is?resource?name?or?atom?*/
26
27????????}
28
29????????[DllImport("winmm")]
30
31????????public?static?extern?bool?PlaySound(string?szSound,?IntPtr?hMod,?PlaySoundFlags?flags);
32
33????????public?static?void?playSound(string?path)
34
35????????{
36
37????????????PlaySound(path,?IntPtr.Zero,?PlaySoundFlags.SND_ASYNC);
38
39????????}
40
41
??
?參數Path為聲音存放的路徑。
具體應用: playSound("sound""歡呼.WAV");存在缺陷只支持WAV的音樂格式。
(二)廣告的實現
?? 主要設計思想:首先在客戶端設置廣告的內容,然后把把它傳回給客戶端,客戶端在經過處理,使其能過“飄”起來。主要用一個Timer控件讓廣告的內容一定時間內向左移動。
?? 實現過程:?????
?
廣告動態顯示?private?void?timer1_Tick(object?sender,?EventArgs?e)
????????{
????????????pos?=?new?Point(lbl_word.Location.X,?lbl_word.Location.Y);
????????????if?(pos.X?>?-lbl_word.Width)
????????????{
????????????????lbl_word.Location?=?new?Point(lbl_word.Location.X?-?2,?lbl_word.Location.Y);
????????????}
????????????else
????????????{
?
????????????????lbl_word.Location?=?new?Point(panel1.Width+lbl_word.Width,?30);
????????????}
?
????????????lbl_NowTime.Text?=?"北京時間:???"?+?DateTime.Now.ToLongTimeString();?//顯示當前時間
????????????
????????}
?
??????(3)?動畫的實現
主要設計思想:將每個動作(2分,3分,1分,犯規等)動畫分割成100張圖片,根據服務器傳過來的信息,100毫秒顯示一張,整個看起來就形成了動畫。
實現過程:
???????
動畫實現?動畫顯示效果#region???動畫顯示效果
????????public?int?i?=?0;
????????private?void?timer2_Tick(object?sender,?EventArgs?e)
????????{
????????????if?(label4.Text?!=?"")???????????/?/記錄傳過來的信號
????????????{
????????????????if?(i?<?100)
????????????????{
????????????????????string?path?=?@"""?+?label4.Text.Trim()?+?@"""?+?i.ToString()?+?".JPG";
????????????????????pictureBox1.ImageLocation?=?Environment.CurrentDirectory?+?path;
????????????????}
????????????????else
????????????????{
????????????????????pictureBox1.ImageLocation?=?"";
????????????????}
????????????????i++;
????????????}
????????}
????????#endregion
(4)LED七段碼設計:
1、?? 主要設計思想:
使用GDI+ 繪圖,顯示數字時填充特定區域。
2、?? 詳細設計:
(1)、采用雙緩沖,以減少或避免顯示時的閃爍
this.DoubleBuffered = true;
??? ???(2)、屬性:
???????? Color 顯示顏色
???????? Length 顯示數字位數
???????? Num 顯示值
??????? (3)、主要方法:
???????? DrawHBar() 畫橫向顯示碼
???????? DrawVBar() 畫豎向顯示碼
???????? showNum()?按照輸入num值七段碼顯示
???????? OnPaint() 重寫onpaint事件
3、?? 顯示實例:
顯示一位數字(length=1)
顯示兩位數字(length=2)
顯示三位數字(length=3)
4、?? 部分代碼:
????????
public?NumBar()?????//
????????{
????????????this.DoubleBuffered?=?true;
????????}
????????//color屬性
????????private?Color?_color=Color.Red;
????????[
????????Category("my"),
????????Description("color")
????????]
????????public?Color?color
????????{
????????????get?{?return?_color;?}
????????????set
????????????{
????????????????_color?=?value;
????????????????Invalidate();
????????????}
????????}
?????????protected?override?void?OnPaint(PaintEventArgs?e)?//重載onpaint函數
????????{
????????????base.OnPaint(e);
????????????Graphics?g?=?e.Graphics;
????????????int?xStart?=?0;
????????????int?width?=?0;
????????????int?numValue?=?0;
????????????if?(this.length?<?1?||?this.length?>?3)
????????????{
????????????????return;
????????????}
????????????switch?(this.length)
????????????{
????????????????case?1:
???????????????????/*…*/
????????????????case?2:
????????????????????/*…*/
????????????????case?3:
???????????????????/*…*/
????????????????default:
????????????????????break;
????????????}
????????}
????????private?void?showNum(Graphics?g,?int?xStart,?int?width,?int?numValue)
????????{
????????????this.DrawHBar(g,?pStart[0],?h[0],?w[0],?Color.DimGray);
????????????this.DrawVBar(g,?pStart[1],?h[1],?w[1],?Color.DimGray);
????????????this.DrawVBar(g,?pStart[2],?h[2],?w[2],?Color.DimGray);
????????????this.DrawHBar(g,?pStart[3],?h[3],?w[3],?Color.DimGray);
????????????this.DrawVBar(g,?pStart[4],?h[4],?w[4],?Color.DimGray);
????????????this.DrawVBar(g,?pStart[5],?h[5],?w[5],?Color.DimGray);
????????????this.DrawHBar(g,?pStart[6],?h[6],?w[6],?Color.DimGray);
????????????#region
????????????switch?(numValue)
????????????{
???????????????/*…*/
????????????}
????????????#endregion
????????}
????????//?畫橫向的顯示條
????????private?void?DrawHBar(Graphics?g,?Point?start,?int?length,?int?width,?Color?color)
????????{
????????????int?h?=?length;
????????????int?w?=?width;
????????????int?x?=?start.X;
????????????int?y?=?start.Y;
????????????h?=?h?-?4;
????????????w?=?w?-?4;
????????????Point[]?ps?=?new?Point[6];
????????????ps[0]?=?new?Point(x?+?2,?h?/?2?+?2?+?y);
????????????ps[1]?=?new?Point(x?+?2?+?(h?/?2),?2?+?y);
????????????ps[2]?=?new?Point(x?+?w?+?2?-?(h?/?2),?2?+?y);
????????????ps[3]?=?new?Point(x?+?w?+?2,?h?/?2?+?2?+?y);
????????????ps[4]?=?new?Point(x?+?w?+?2?-?(h?/?2),?2?+?h?+?y);
????????????ps[5]?=?new?Point(x?+?2?+?h?/?2,?h?+?2?+?y);
????????????Pen?pen?=?new?Pen(Color.Gray,?1);
????????????SolidBrush?br?=?new?SolidBrush(color);
????????????g.FillPolygon(br,?ps);
????????????g.DrawPolygon(pen,?ps);
????????????br.Dispose();
????????????pen.Dispose();
????????}
四、程序運行結果
客戶端:
??
?????
?
服務器端:
?
(五)總結
《籃球計分器》主要涉及Socket編程、GDI+自定義控件、XML序列化以及二進制序列化與反序列化、還有一些多媒體的簡單的聲音與圖片的播放。
?
轉載于:https://www.cnblogs.com/wakerobin/archive/2009/07/02/1381569.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的IT项目之旅(二)篮球计分器(分析、设计、实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Function in loop and
- 下一篇: 四门专业课,有点困难哈~