IT项目之旅(二)篮球计分器(分析、设计、实现)
一.???? 總體設(shè)計(jì)
?? 籃球計(jì)分器的實(shí)現(xiàn)主要包括客戶端和服務(wù)器兩部分,按照客戶/服務(wù)器的模式進(jìn)行工作,提供交互式的訪問(wèn),在INTERNET使用廣泛。通信協(xié)議主要采用TCP協(xié)議。主要實(shí)現(xiàn)了籃球計(jì)分器的幾個(gè)關(guān)鍵功能:服務(wù)器段與客戶端的通信、七段碼、斷電保存、廣告、動(dòng)畫(huà)、聲音。這是07年5月份課余時(shí)間完成的微機(jī)實(shí)踐項(xiàng)目,其主要技術(shù)還是在于Socket編程。
?
二、開(kāi)發(fā)環(huán)境及主要技術(shù)說(shuō)明
VS2005,主要利用Socket編程,其中還涉及到線程方面的內(nèi)容
?? Socket套接字的工作原理:通過(guò)局域網(wǎng)互聯(lián)網(wǎng)進(jìn)行通信,至少采用一對(duì)套接字,其中一個(gè)運(yùn)行于客戶端,另一個(gè)運(yùn)行于服務(wù)器端。根據(jù)連接啟動(dòng)的方式以及本地套接字要連接的對(duì)象連接過(guò)程分為三個(gè)步驟:服務(wù)端接聽(tīng)、客戶端接聽(tīng)和連接請(qǐng)求
?三、具體實(shí)現(xiàn)過(guò)程?
實(shí)現(xiàn)的具體過(guò)程:
?
(1)聲音的實(shí)現(xiàn):
主要設(shè)計(jì)思想:根據(jù)服務(wù)器端傳過(guò)來(lái)的控制信息,調(diào)用API的接口來(lái)實(shí)現(xiàn)聲音的播放
實(shí)現(xiàn)過(guò)程:??
聲音的實(shí)現(xiàn)?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
??
?參數(shù)Path為聲音存放的路徑。
具體應(yīng)用: playSound("sound""歡呼.WAV");存在缺陷只支持WAV的音樂(lè)格式。
(二)廣告的實(shí)現(xiàn)
?? 主要設(shè)計(jì)思想:首先在客戶端設(shè)置廣告的內(nèi)容,然后把把它傳回給客戶端,客戶端在經(jīng)過(guò)處理,使其能過(guò)“飄”起來(lái)。主要用一個(gè)Timer控件讓廣告的內(nèi)容一定時(shí)間內(nèi)向左移動(dòng)。
?? 實(shí)現(xiàn)過(guò)程:?????
?
廣告動(dòng)態(tài)顯示?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?=?"北京時(shí)間:???"?+?DateTime.Now.ToLongTimeString();?//顯示當(dāng)前時(shí)間
????????????
????????}
?
??????(3)?動(dòng)畫(huà)的實(shí)現(xiàn)
主要設(shè)計(jì)思想:將每個(gè)動(dòng)作(2分,3分,1分,犯規(guī)等)動(dòng)畫(huà)分割成100張圖片,根據(jù)服務(wù)器傳過(guò)來(lái)的信息,100毫秒顯示一張,整個(gè)看起來(lái)就形成了動(dòng)畫(huà)。
實(shí)現(xiàn)過(guò)程:
???????
動(dòng)畫(huà)實(shí)現(xiàn)?動(dòng)畫(huà)顯示效果#region???動(dòng)畫(huà)顯示效果
????????public?int?i?=?0;
????????private?void?timer2_Tick(object?sender,?EventArgs?e)
????????{
????????????if?(label4.Text?!=?"")???????????/?/記錄傳過(guò)來(lái)的信號(hào)
????????????{
????????????????if?(i?<?100)
????????????????{
????????????????????string?path?=?@"""?+?label4.Text.Trim()?+?@"""?+?i.ToString()?+?".JPG";
????????????????????pictureBox1.ImageLocation?=?Environment.CurrentDirectory?+?path;
????????????????}
????????????????else
????????????????{
????????????????????pictureBox1.ImageLocation?=?"";
????????????????}
????????????????i++;
????????????}
????????}
????????#endregion
(4)LED七段碼設(shè)計(jì):
1、?? 主要設(shè)計(jì)思想:
使用GDI+ 繪圖,顯示數(shù)字時(shí)填充特定區(qū)域。
2、?? 詳細(xì)設(shè)計(jì):
(1)、采用雙緩沖,以減少或避免顯示時(shí)的閃爍
this.DoubleBuffered = true;
??? ???(2)、屬性:
???????? Color 顯示顏色
???????? Length 顯示數(shù)字位數(shù)
???????? Num 顯示值
??????? (3)、主要方法:
???????? DrawHBar() 畫(huà)橫向顯示碼
???????? DrawVBar() 畫(huà)豎向顯示碼
???????? showNum()?按照輸入num值七段碼顯示
???????? OnPaint() 重寫(xiě)onpaint事件
3、?? 顯示實(shí)例:
顯示一位數(shù)字(length=1)
顯示兩位數(shù)字(length=2)
顯示三位數(shù)字(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函數(shù)
????????{
????????????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
????????}
????????//?畫(huà)橫向的顯示條
????????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();
????????}
四、程序運(yùn)行結(jié)果
客戶端:
??
?????
?
服務(wù)器端:
?
(五)總結(jié)
《籃球計(jì)分器》主要涉及Socket編程、GDI+自定義控件、XML序列化以及二進(jìn)制序列化與反序列化、還有一些多媒體的簡(jiǎn)單的聲音與圖片的播放。
?
轉(zhuǎn)載于:https://www.cnblogs.com/wakerobin/archive/2009/07/02/1381569.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的IT项目之旅(二)篮球计分器(分析、设计、实现)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Function in loop and
- 下一篇: 四门专业课,有点困难哈~