在線演示地址:Silverlight+WCF 新手實例 象棋 在線演示 算法
上一系列四十篇索引:Silverlight+WCF 新手實例 象棋 專題索引網絡
本篇緊接着上一篇:Silverlight+WCF 實戰-網絡象棋最終篇之房間裝修-WCF端(二)ide
繼續爲房間進行以下的裝修:函數
代碼實現[Silverlight端]google
說明:spa
1:Silverlight端:GameRoom類大調整[被註釋的是原來的代碼,未註釋的是修改的代碼].net
因爲房間本次裝修的變化較大,成本也是不低的。code
a:增長的全局變量htm
TextBlock blackText; // 黑色玩家文字
private SolidColorBrush defaultBrush = new SolidColorBrush(Color.FromArgb( 0 , 0 , 0 , 0 )); // 默認透明的背景色
b:增長的屬性[一樣刪除兩個字段RedPlayerInChair/BlackPlayerInChair]
/// // 紅色座位有人
/// // </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:調整構造函數,增長背景圖傳入[因爲背景圖片引入,原來的長方形房間變成正方形,由此改變了部分算法]
{
RoomHeight = RoomWidth = width;
RoomID = roomID;
InitPoint = location;
BackgroundBrush = background;
GamingBrush = gaming;
}
d:繪房間和重繪房間函數,直接把之前的註釋掉,寫個新了
{
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:新增長的取用戶頭像圖片的函數
{
ImageBrush headBrush = new ImageBrush()
{
ImageSource = new BitmapImage( new Uri( " images/head/ " + head, UriKind.Relative))
};
return headBrush;
}
f:點擊黑色座位事件:blackChair_MouseLeftButtonDown
// {
// 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
// {
// 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 );
}
說明
2:Silverlight端:Game類調整
代碼很少,把原來的函數註釋掉,換個新的以下:
{
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調整
房間更新接收經過時,因爲字段變化,須要調整一下:
{
// room.RedPlayerInChair = gsRoom.RedInChair;
// room.BlackPlayerInChair = gsRoom.BlackInChair;
room.RedPlayer = gsRoom.RedPlayer;
room.BlackPlayer = gsRoom.BlackPlayer;
room.IsGaming = gsRoom.IsGaming;
room.ReDraw();
}
還有構造函數原來建立了30個房間,爲兼容高度,改爲20個房間了。
4:Silverlight端:Login.xaml調整
登錄的時候,給用戶設置一個默認頭像,固然也能夠改爲選擇頭像的方式:
{
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);
}
第九階段源碼:點擊下載
結言: