热到飚血了。。。
上次创建了一房间了,现在要创建多个房间了。
我们像添加一个Chess类来创建很多棋子一样,现在创建一个Game类来创建很多房间:
Code文件夹右键,添加新建项->类->Game.cs,同样的,名称空间去掉[.Code]
namespace
NewChessProject
{
/// <summary>
/// 游戏 by 路过秋天
/// </summary>
public class Game
{
}
}
{
/// <summary>
/// 游戏 by 路过秋天
/// </summary>
public class Game
{
}
}
先上一个房间列表的属性:
public
List
<
GameRoom
>
GameRoomList
//
房间列表
{
get ;
set ;
}
{
get ;
set ;
}
接下来就是一个创建房间的方法了:
public
void
CreateGameRoom(
int
count)
{
GameRoomList = new List < GameRoom > ();
int margin = 40 ;
int GameRoomWidth = 120 ;
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);
GameRoomList.Add(GameRoom);
}
}
public void DrawIn(Panel control)
{
for ( int i = 0 ; i < GameRoomList.Count; i ++ )
{
GameRoomList[i].DrawIn(control);
}
}
{
GameRoomList = new List < GameRoom > ();
int margin = 40 ;
int GameRoomWidth = 120 ;
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);
GameRoomList.Add(GameRoom);
}
}
public void DrawIn(Panel control)
{
for ( int i = 0 ; i < GameRoomList.Count; i ++ )
{
GameRoomList[i].DrawIn(control);
}
}
由于我们设置了页面总宽为800,我们需要到Room.xaml设置一下Canvas的宽和高:
<
Grid
x:Name
="LayoutRoot"
Background
="White"
Width
="800"
Height
="600"
HorizontalAlignment
="Left"
VerticalAlignment
="Top"
Margin
="0,0,0,0"
>
</ Grid >
</ Grid >
OK,现在我们回到Room.xaml.cs代码里,创建30个房间:
public
partial
class
Room : UserControl
{
public Room()
{
InitializeComponent();
// GameRoom gameRoom = new GameRoom(1, new Point(0,0), 120);
// gameRoom.DrawIn(LayoutRoot);
Game game = new Game();
game.CreateGameRoom( 30 );
game.DrawIn(LayoutRoot);
}
}
{
public Room()
{
InitializeComponent();
// GameRoom gameRoom = new GameRoom(1, new Point(0,0), 120);
// gameRoom.DrawIn(LayoutRoot);
Game game = new Game();
game.CreateGameRoom( 30 );
game.DrawIn(LayoutRoot);
}
}
运行F5,看下效果:
怎么全挤在一堆了,我查了查,调了调,又比较了一下,发现:
原来Grid布局和Canvas布局的区别。
因此,把Grid标签换成Canvas标签就行了:
<
Canvas
Background
="White"
Height
="600"
HorizontalAlignment
="Left"
Margin
="0,0,0,0"
Name
="LayoutRoot"
VerticalAlignment
="Top"
Width
="800"
>
</ Canvas >
</ Canvas >
OK,再F5运行,房间正常了:
OK,列表出来了。
下一节开始要讲WCF通讯,同时后面章节会对之前的很多代码会做调整与补充。
现在提供第3部分代码:点此下载
版权声明:本文原创发表于博客园,作者为路过秋天,原文链接:http://www.cnblogs.com/cyq1162/archive/2010/07/12/1775774.html