首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

WPF/Silverlight 遊戲開發教程(三十六之一):地圖自定義切片與導出架構設計

     編輯:關於C語言

提升遊戲效能關鍵在於地圖優化。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 定虛線寬與間距。同法用於切片分割。

WPF 整合 WinForm NumericUpDown

  1. 引用 WindowsFormsIntegration、System.Windows.Forms
  2. XAML 定義命名空間
  3. WindowsFormsHost 包裹 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>

設計器僅見描述,運行時見實體:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved