Silverlight+WCF 實戰-網絡象棋最終篇之房間裝修-Silverlight端[帶第九階段源碼](三)...

在線演示地址:Silverlight+WCF 新手實例 象棋 在線演示 算法

上一系列四十篇索引:Silverlight+WCF 新手實例 象棋 專題索引網絡

 

 

本篇緊接着上一篇:Silverlight+WCF 實戰-網絡象棋最終篇之房間裝修-WCF端(二)ide

繼續爲房間進行以下的裝修:函數

 

代碼實現[Silverlight端]google

說明:spa

因爲更換背景引入圖片,房間的屬性發生了較大的變化,由此引起了客戶端房間類較大的改動。

 

 

1:Silverlight端:GameRoom類大調整[被註釋的是原來的代碼,未註釋的是修改的代碼].net

 

因爲房間本次裝修的變化較大,成本也是不低的。code

a:增長的全局變量htm

TextBlock redText; // 紅色玩家文字
TextBlock blackText; // 黑色玩家文字
private  SolidColorBrush defaultBrush  =   new  SolidColorBrush(Color.FromArgb( 0 0 0 0 )); // 默認透明的背景色

 

b:增長的屬性[一樣刪除兩個字段RedPlayerInChair/BlackPlayerInChair]

05233633_16qJ.gif
         /// //  <summary>
        
/// // 紅色座位有人
        
/// //  </summary>
         // public bool RedPlayerInChair
        
// {
        
//     get;
        
//     set;
        
// }
         /// //  <summary>
        
/// // 黑色座位有人
        
/// //  </summary>
         // public bool BlackPlayerInChair
        
// {
        
//     get;
        
//     set;
        
// }
         public   bool  IsGaming
        {
            
get ;
            
set ;
        }
        
public  GameService.Player RedPlayer
        {
            
get ;
            
set ;
        }
        
public  GameService.Player BlackPlayer
        {
            
get ;
            
set ;
        }
        
///   <summary>
        
///  背景圖片[還沒玩遊戲]
        
///   </summary>
         public  ImageBrush BackgroundBrush
        {
            
get ;
            
set ;
        }
        
///   <summary>
        
///  背景圖片[遊戲中]
        
///   </summary>
         public  ImageBrush GamingBrush
        {
            
get ;
            
set ;
        }

 

c:調整構造函數,增長背景圖傳入[因爲背景圖片引入,原來的長方形房間變成正方形,由此改變了部分算法]

05233633_16qJ.gif
         public  GameRoom( int  roomID, Point location,  int  width, ImageBrush background, ImageBrush gaming)
        {
            RoomHeight
= RoomWidth  =  width;
            RoomID 
=  roomID;
            InitPoint 
=  location;
            BackgroundBrush 
=  background;
            GamingBrush 
=  gaming;
        }

 

d:繪房間和重繪房間函數,直接把之前的註釋掉,寫個新了

05233633_16qJ.gif Draw/ReDraw函數
         private   void  Draw()
        {
            redChair 
=   new  Rectangle()
            {
                Width 
=   32 ,
                Height 
=   32 ,
                Fill 
=  defaultBrush,
                Margin 
=   new  Thickness( 12 58 0 0 )
            };
            redText 
=   new  TextBlock()
            {
                FontFamily 
=   new  FontFamily( " 宋體 " ),
                FontSize 
=   12 ,
                Margin 
=   new  Thickness( 10 100 0 0 )
            };
            spectatorChair 
=   new  Rectangle()
            {
                Width 
=  RoomWidth  /   3 ,
                Height 
=  RoomWidth  /   3 ,
                Fill 
=  defaultBrush,
                Margin 
=   new  Thickness(RoomWidth  /   3 , RoomHeight  /   3 0 0 )
            };
            blackChair 
=   new  Rectangle()
            {
                Width 
=  redChair.Width,
                Height 
=  redChair.Height,
                Fill 
=  defaultBrush,
                Margin 
=   new  Thickness( 106 58 0 0 )
            };
            blackText 
=   new  TextBlock()
            {
                FontFamily 
=   new  FontFamily( " 宋體 " ),
                FontSize 
=   12 ,
                Margin 
=   new  Thickness( 84 20 0 0 )
            };
            TextBlock text 
=   new  TextBlock()
            {
                Foreground 
=   new  SolidColorBrush(Colors.White),
                Text 
=   " "   +  RoomID  +   "  - " ,
                FontFamily 
=   new  FontFamily( " 宋體 " ),
                FontSize 
=   12 ,
                Margin 
=   new  Thickness( 54 130 0 0 )
            };


            room 
=   new  Canvas()
            {
                Width 
=  RoomWidth,
                Height 
=  RoomHeight,
                Margin 
=   new  Thickness(InitPoint.X, InitPoint.Y,  0 0 ),
                Background 
=  BackgroundBrush,
                Opacity 
=   0.8
            };
            redChair.MouseLeftButtonDown 
+=   new  MouseButtonEventHandler(redChair_MouseLeftButtonDown);
            blackChair.MouseLeftButtonDown 
+=   new  MouseButtonEventHandler(blackChair_MouseLeftButtonDown);
            spectatorChair.MouseLeftButtonDown 
+=   new  MouseButtonEventHandler(spectatorChair_MouseLeftButtonDown);

            room.Children.Add(redChair);
            room.Children.Add(blackChair);
            room.Children.Add(spectatorChair);

            room.Children.Add(text);
            room.Children.Add(redText);
            room.Children.Add(blackText);
            container.Children.Add(room);
        }
        
public   void  ReDraw()
        {
            
if  (RedPlayer  !=   null )
            {
                redChair.Fill 
=  GetPlayerBrush(RedPlayer.Head);
                redText.Text 
=  RedPlayer.NickName;
                
// redChair.Effect= new System.Windows.Media.Effects.Effect().
            }
            
else
            {
                redChair.Fill 
=  defaultBrush;
                redText.Text 
=   "" ;
            }
            
if  (BlackPlayer  !=   null )
            {
                blackChair.Fill 
=  GetPlayerBrush(BlackPlayer.Head);
                blackText.Text 
=  BlackPlayer.NickName;
            }
            
else
            {
                blackChair.Fill 
=  defaultBrush;
                blackText.Text 
=   "" ;
            }

            room.Background 
=  IsGaming  ?  GamingBrush : BackgroundBrush;
        }

 

e:新增長的取用戶頭像圖片的函數

05233633_16qJ.gif
         private  ImageBrush GetPlayerBrush( string  head)
        {
            ImageBrush headBrush 
=   new  ImageBrush()
            {
                ImageSource 
=   new  BitmapImage( new  Uri( " images/head/ "   +  head, UriKind.Relative))
            };
            
return  headBrush;
        }

 

f:點擊黑色座位事件:blackChair_MouseLeftButtonDown

05233633_16qJ.gif
             // if (!BlackPlayerInChair)
            
// {
            
//     BlackPlayerInChair = true;
            
//     blackChair.Fill = new RadialGradientBrush(Colors.Blue, Colors.White);
            
//     Enter(2);
            
// }
             if  (BlackPlayer  ==   null )
            {
                BlackPlayer 
=  App.player;
                blackChair.Fill 
=  GetPlayerBrush(App.player.Head);
                blackText.Text 
=  App.player.NickName;
                Enter(
2 );
            }

 

g:點擊紅色座位事件:redChair_MouseLeftButtonDown

05233633_16qJ.gif
             // if (!RedPlayerInChair)
            
// {
            
//     RedPlayerInChair = true;
            
//     redChair.Fill = new RadialGradientBrush(Colors.Blue, Colors.White);
            
//     Enter(1);
            
// }
             if  (RedPlayer  ==   null )
            {
                RedPlayer 
=  App.player;
                redChair.Fill 
=  GetPlayerBrush(App.player.Head);
                redText.Text 
=  App.player.NickName;
                Enter(
1 );
            }

說明

GameRoom類的代碼到此就改完了,差很少把整類都重寫了,看,成本多高~~~房子啊房子~

 

 

2:Silverlight端:Game類調整

代碼很少,把原來的函數註釋掉,換個新的以下:

05233633_16qJ.gif CreateGameRoom 建立房間
public   void  CreateGameRoom( int  count)
        {
            GameRoomList 
=   new  List < GameRoom > ();
            ImageBrush bgBrush 
=   new  ImageBrush()
            {
                ImageSource 
=   new  BitmapImage( new  Uri( " images/chess/tablen.png " , UriKind.Relative))
            };
            ImageBrush gamingBrush 
=   new  ImageBrush()
            {
                ImageSource 
=   new  BitmapImage( new  Uri( " images/chess/tables.png " , UriKind.Relative))
            };
            
int  GameRoomWidth  =   150 ;
            
int  pageWidth  =   800 ;
            
int  x  =   0 , y  =   0 ;
            Point location;
            
for  ( int  i  =   0 ; i  <  count; i ++ )
            {
                
// 計算房間位置
                x  =  (i  %  (pageWidth  /  GameRoomWidth))  *  GameRoomWidth; //   + margin+ i % (pageWidth / GameRoomWidth) * 20;
                y  =  (i  /  (pageWidth  /  GameRoomWidth))  *  GameRoomWidth; //  + margin;
                location  =   new  Point(x, y);
                GameRoom GameRoom 
=   new  GameRoom(i  +   1 , location, GameRoomWidth, bgBrush, gamingBrush);
                GameRoomList.Add(GameRoom);
            }
        }

 

 

3:Silverlight端:Room.xaml調整

房間更新接收經過時,因爲字段變化,須要調整一下:

05233633_16qJ.gif
         void  UpdateRoomState(GameService.Room gsRoom, GameRoom room)
        {
            
// room.RedPlayerInChair = gsRoom.RedInChair;
            
// room.BlackPlayerInChair = gsRoom.BlackInChair;
            room.RedPlayer  =  gsRoom.RedPlayer;
            room.BlackPlayer 
=  gsRoom.BlackPlayer;
            room.IsGaming 
=  gsRoom.IsGaming;
            room.ReDraw();
        }

還有構造函數原來建立了30個房間,爲兼容高度,改爲20個房間了。

game.CreateGameRoom(20);

 

 

 

4:Silverlight端:Login.xaml調整

登錄的時候,給用戶設置一個默認頭像,固然也能夠改爲選擇頭像的方式:

05233633_16qJ.gif btnLogin_Click
private   void  btnLogin_Click( object  sender, RoutedEventArgs e)
        {
            nickName 
=  txtNickName.Text.Trim();
            
if  (nickName  ==   "" )
            {
                MessageBox.Show(
" 請輸入暱稱! " );
                
return ;
            }
            
if  (nickName.Contains( " , " ))
            {
                MessageBox.Show(
" 暱稱不能包含非法字符! " );
                
return ;
            }
            btnLogin.IsEnabled 
=   false ;
              App.player.ID 
=  userID;
            App.player.NickName 
=  nickName;
            App.player.Head 
=   " 1.png " ; // 只是加了這一行
            App.client.LoginAsync(App.player);
        }

 

第九階段源碼:點擊下載

 

 

結言:

爲了裝修房間,大動干戈了一場,不過看到最後結果,相信仍是值的。

 

 

轉載於:https://my.oschina.net/secyaher/blog/274286