提升遊戲效能關鍵在於地圖優化。Web 遊戲可將地圖切成同尺寸片,按主角位置按需載入。例:20000×20000 地圖切 400×400 成 2500 片,視窗 800×600 每次僅載主角中心周圍 9 片(1200×900),比一次性載大圖且移動切割效能優越太多。
第三十四節用 Grid.ShowGridLines 雖簡單但效能極低。用 Line 繪製優化:
private void SetGridLines(Canvas carrier, double totalWidth, double totalHeight, double singleWidth, double singleHeight, Color lineColor, double dashWidth, double dashSpace)
{
carrier.Children.Clear();
int sectionXNum = (int)(totalWidth / singleWidth);
int sectionYNum = (int)(totalHeight / singleHeight);
for (int x = 1; x <= sectionXNum; x++)
{
Line line = new Line() { X1 = x * singleWidth, X2 = x * singleWidth, Y1 = 0, Y2 = totalHeight, Stroke = new SolidColorBrush(lineColor), StrokeDashArray = new DoubleCollection() { dashWidth, dashSpace } };
carrier.Children.Add(line);
}
for (int y = 1; y <= sectionYNum; y++)
{
Line line = new Line() { X1 = 0, X2 = totalWidth, Y1 = y * singleHeight, Y2 = y * singleHeight, Stroke = new SolidColorBrush(lineColor), StrokeDashArray = new DoubleCollection() { dashWidth, dashSpace } };
carrier.Children.Add(line);
}
}
Stroke 設線色(Fill 無效),StrokeDashArray 定虛線寬與間距。同法用於切片分割。
WindowsFormsIntegration、System.Windows.FormsWindowsFormsHost 包裹 NumericUpDown<WinFormHost:WindowsFormsHost Canvas.Left="820" Canvas.Top="170" Height="20" Width="101">
<WinForm:NumericUpDown x:Name="SectionWidth" Maximum="500" Minimum="100" Increment="10" Value="300" ValueChanged="SectionSize_ValueChanged" />
</WinFormHost:WindowsFormsHost>
<WinFormHost:WindowsFormsHost Canvas.Left="820" Canvas.Top="196" Height="20" Width="101">
<WinForm:NumericUpDown x:Name="SectionHeight" Maximum="500" Minimum="100" Increment="10" Value="300" ValueChanged="SectionSize_ValueChanged" />
</WinFormHost:WindowsFormsHost>
設計器僅見描述,運行時見實體:
