1

我目前正在制作一个使用Graphics 类来绘制大部分游戏对象的游戏(在 vb.net 中),但我遇到了老的性能问题。为了解决滞后问题,我添加了一些代码来自动降低游戏质量,这样我就可以尝试提高游戏的速度,这在一定程度上是有效的,但是当更多的“敌人”被添加到更高的水平。我对图形类很陌生(几天前才开始使用它),所以我知道的唯一有助于加快游戏速度的事情是:关闭抗锯齿,并降低计时器间隔(这几乎控制了我的游戏刷新率),那么除了前面的,还有其他方法可以提高我的游戏性能吗?

游戏截图... 截屏

到目前为止,这是我的代码(质量调整位在底部),

Public Class Form1

Private keysPressed As New HashSet(Of Keys)
Private firstRun As Boolean = True
Private mouseDownB As Boolean = False
Private newLevel As Boolean = True
Private randomColour As New Random
Private RandomEnemyX As New Random
Private RandomEnemyY As New Random
Private RandomEnemySide As New Random
Private life As Integer = 5
Private score As String = 0
Private playerX As Integer = 403
Private playerY As Integer = 206
Private totaltEnemyCount As Integer = 10
Private enemyCount As Integer = 0
Private enemyX(0) As Integer
Private enemyY(0) As Integer
Private enemyID(0) As Integer
Private EX As Integer = 0
Private EY As Integer = 0
Private ES As Integer = 0
Private enemyMoved As Integer
Private enemySpeed As Integer = 1
Private enemyAttacked As Integer = 0
Private enemyActive As Integer = 0
Private levelNumber As Long = 0
Private FPS As Integer = 40
Private g1 As Graphics
Private enemyCountIncrease As Boolean = True
Private enemySpeedIncrease As Boolean = False

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    g1 = Me.CreateGraphics
    g1.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias                           
End Sub

Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
    mouseDownB = True
End Sub

Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
    mouseDownB = False
End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.Left Then
        keysPressed.Add(Keys.Left)
    ElseIf e.KeyCode = Keys.Up Then
        keysPressed.Add(Keys.Up)
    ElseIf e.KeyCode = Keys.Right Then
        keysPressed.Add(Keys.Right)
    ElseIf e.KeyCode = Keys.Down Then
        keysPressed.Add(Keys.Down)
    End If
End Sub

Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
    keysPressed.Remove(e.KeyCode)
End Sub
'''''''''''''''''''''''''Random colour generator''''''''''''''''''''''''''''
Private Function R() As String
    Return randomColour.Next(0, 255)
End Function

Private Function G() As String
    Return randomColour.Next(0, 255)
End Function

Private Function B() As String
    Return randomColour.Next(0, 255)
End Function
'''''''''''''''''''''''''Random location generator''''''''''''''''''''''''''''
Private Function X() As String
    Return RandomEnemyX.Next(0, 494)
End Function

Private Function Y() As String
    Return RandomEnemyY.Next(24, 348)
End Function
'''''''''''''''''''''''''Main display & player generation''''''''''''''''''''''''''''''''
Private Sub mainPaint(sender As Object, e As EventArgs) Handles Timer1.Tick
    Timer1.Interval = FPS
    Dim ST As New Stopwatch
    ST.Start()
    If firstRun = True Then
        levelNumber = levelNumber + 1
        g1.DrawString("Level " & levelNumber, New Font("DigifaceWide", 64, GraphicsUnit.Pixel), New SolidBrush(Color.FromArgb(191, R, G, B)), X, Y)
        g1.DrawRectangle(New Pen(Color.FromArgb(128, R, G, B)), 0, 0, 884, 24)
        g1.FillEllipse(New SolidBrush(Color.FromArgb(128, R, G, B)), playerX, playerY, 24, 24)
        g1.FillRectangle(New SolidBrush(Color.FromArgb(128, R, G, B)), 0, 0, 884, 24)
        g1.DrawString("Life: 5", New Font("DigifaceWide", 20, GraphicsUnit.Pixel), New SolidBrush(Color.FromArgb(191, R, G, B)), 2, 0)
        g1.DrawString("Score: 0", New Font("DigifaceWide", 20, GraphicsUnit.Pixel), New SolidBrush(Color.FromArgb(191, R, G, B)), 100, 0)
        firstRun = False
    End If
    '''''''''''''''''''''''''''''''Move player''''''''''''''''''''''''''''''
    If keysPressed.Contains(Keys.Left) Then
        If playerX > 0 Then
            playerX = playerX - 5
            g1.FillEllipse(New SolidBrush(Color.FromArgb(128, R, G, B)), playerX, playerY, 24, 24)
        End If
    ElseIf keysPressed.Contains(Keys.Up) Then
        If playerY > 24 Then
            playerY = playerY - 5
            g1.FillEllipse(New SolidBrush(Color.FromArgb(128, R, G, B)), playerX, playerY, 24, 24)
        End If
    ElseIf keysPressed.Contains(Keys.Right) Then
        If playerX < 860 Then
            playerX = playerX + 5
            g1.FillEllipse(New SolidBrush(Color.FromArgb(128, R, G, B)), playerX, playerY, 24, 24)
        End If
    ElseIf keysPressed.Contains(Keys.Down) Then
        If playerY < 388 Then
            playerY = playerY + 5
            g1.FillEllipse(New SolidBrush(Color.FromArgb(128, R, G, B)), playerX, playerY, 24, 24)
        End If
    End If
    '''''''''''''''''''''''''''''''Laser generation'''''''''''''''''''''''''''''''''''''
    If mouseDownB = True Then
        g1.DrawLine(New Pen(Color.FromArgb(102, R, G, B)), playerX + 12, playerY + 12, Control.MousePosition.X - Me.Bounds.X, Control.MousePosition.Y - Me.Bounds.Y)
    End If
    ''''''''''''''''''''''''''''''''Enemy generation - New level''''''''''''''''''''''''''''''''''''
    If newLevel = True Then
        levelNumber = levelNumber + 1
        Do While enemyCount < totaltEnemyCount
            Dim i As Integer = 0
            ReDim enemyID(0)
            Do While enemyID.Length <= totaltEnemyCount
                EX = RandomEnemyX.Next(0, 872)
                EY = RandomEnemyY.Next(24, 400)
                ES = RandomEnemySide.Next(0, 4)
                ReDim Preserve enemyID(i + 1)
                enemyID(i) = 1
                ReDim Preserve enemyX(i + 1)
                ReDim Preserve enemyY(i + 1)
                If ES = 0 Then 'Left side
                    enemyX(i) = 0
                    enemyY(i) = EY
                ElseIf ES = 1 Then 'Top side
                    enemyX(i) = EX
                    enemyY(i) = 24
                ElseIf ES = 2 Then 'Right side
                    enemyX(i) = 872
                    enemyY(i) = EY
                ElseIf ES = 3 Then 'Bottom side
                    enemyX(i) = EX
                    enemyY(i) = 400
                End If
                g1.FillEllipse(New SolidBrush(Color.FromArgb(128, Color.Red)), enemyX(i), enemyY(i), 12, 12)
                enemyCount = enemyID.Length
                enemyActive = enemyActive + 1
                i = i + 1
            Loop
        Loop
        newLevel = False
    End If
    ''''''''''''''''''''''''''''''''Enemy attacked''''''''''''''''''''''''''''''''''''''''
    Dim iii As Integer = 0
    Do While iii < totaltEnemyCount
        If (((Control.MousePosition.X - Me.Location.X) > enemyX(iii))) And ((Control.MousePosition.X - Me.Location.X) < (enemyX(iii) + 16)) And ((Control.MousePosition.Y - Me.Location.Y) > enemyY(iii)) And ((Control.MousePosition.Y - Me.Location.Y) < (enemyY(iii) + 16)) Then
            If mouseDownB = True Then
                enemyID(iii) = -1
                enemyX(iii) = -1
                enemyY(iii) = -1
                enemyActive = enemyActive - 1
                enemyCount = enemyCount - 1
            End If
        End If
        iii = iii + 1
    Loop
    ''''''''''''''''''''''''''''''''Move enemy'''''''''''''''''''''''''''''
    Dim ii As Integer = 0
    Do While enemyMoved < totaltEnemyCount
        If enemyActive < 1 Then
            enemyCount = 0
            newLevel = True
            If enemyCountIncrease = True Then
                totaltEnemyCount = totaltEnemyCount + 50
            ElseIf enemySpeedIncrease = True Then
                enemySpeed = enemySpeed + 1
            End If
            g1.DrawString("Level " & levelNumber, New Font("DigifaceWide", 64, GraphicsUnit.Pixel), New SolidBrush(Color.FromArgb(191, R, G, B)), X, Y)
            Exit Sub
        End If
        If enemyID(ii) = -1 Then
            GoTo skipMove
        End If
        If (enemyX(ii) - (playerX + 4)) < 0 Then 'Enemy on left
            enemyX(ii) = enemyX(ii) + enemySpeed
        End If
        If (enemyX(ii) - (playerX + 4)) > 0 Then 'Enemy on right
            enemyX(ii) = enemyX(ii) - enemySpeed
        End If
        If (enemyY(ii) - (playerY + 4)) < 0 Then 'Enemy above
            enemyY(ii) = enemyY(ii) + enemySpeed
        End If
        If (enemyY(ii) - (playerY + 4)) > 0 Then 'Enemy below
            enemyY(ii) = enemyY(ii) - enemySpeed
        End If
        g1.FillEllipse(New SolidBrush(Color.FromArgb(51, Color.Red)), enemyX(ii), enemyY(ii), 12, 12)
skipMove:
        enemyMoved = enemyMoved + 1
        ii = ii + 1
    Loop
    enemyMoved = 0
    ''''''''''''''''''''''QDSA - Quality Depentant Speed Adjustment''''''''''''''''''''
    ST.Stop()
        Dim elapTick As Decimal = ST.ElapsedTicks
        If elapTick < 5000 Then 'Slow down
            FPS = FPS + 1
            enemySpeedIncrease = False
            g1.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            enemyCountIncrease = True
        ElseIf elapTick > 5000 Then 'Speed up
            If FPS > 1 Then
                FPS = FPS - 1
            End If
            g1.SmoothingMode = Drawing2D.SmoothingMode.None
            enemyCountIncrease = False
            If enemySpeed < 4 Then
                enemySpeedIncrease = True
            Else
                enemySpeedIncrease = False
            End If
        End If
End Sub
End Class
4

1 回答 1

4

我已经制作了一个类似于您需要的示例 WPF 项目,只是为了让您了解 WPF 与古代技术相比是多么强大、美观和快速。

它看起来像这样:

在此处输入图像描述

在这里完全发布它的代码有点太多了,所以这里是一个指向带有完整源代码的 RAR 文件的链接。

主要的 UI XAML 是这样的:

<Window x:Class="SamsGameSample.MainWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SamsGameSample"
        Title="SamsGameSample"
        PreviewKeyDown="Window_PreviewKeyDown"
        PreviewKeyUp="Window_PreviewKeyUp"
        SizeChanged="Window_SizeChanged"
        WindowState="Maximized">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:Player}">
            <Ellipse Fill="Green" Stroke="Black" Height="{Binding Size.Height}" Width="{Binding Size.Width}"/>
        </DataTemplate>

        <DataTemplate DataType="{x:Type local:Enemy}">
            <Ellipse Fill="Red" Stroke="Black" Height="{Binding Size.Height}" Width="{Binding Size.Width}"/>
        </DataTemplate>
    </Window.Resources>

    <DockPanel>
        <DockPanel.Background>
            <LinearGradientBrush StartPoint="0,0" EndPoint="1,0" Opacity=".6">
                <GradientStop Color="Blue" Offset="0"/>
                <GradientStop Color="Red" Offset=".5"/>
                <GradientStop Color="Green" Offset="1"/>
            </LinearGradientBrush>
        </DockPanel.Background>
        <StackPanel Orientation="Horizontal" Background="#70000000" DockPanel.Dock="Top">
            <TextBlock Text="{Binding Lives, StringFormat='Lives: {0}'}" Margin="10" Foreground="AliceBlue"/>
            <TextBlock Text="{Binding Score, StringFormat='Score: {0}'}" Margin="10" Foreground="AliceBlue"/>
        </StackPanel>
        <ItemsControl ItemsSource="{Binding GameObjects}" x:Name="GameArea">
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Canvas.Left" Value="{Binding Location.X}"/>
                    <Setter Property="Canvas.Top" Value="{Binding Location.Y}"/>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas IsItemsHost="True"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DockPanel>
</Window>
  • 将我的项目中的代码量(以及它的清洁度)与在 winforms 中执行几乎任何操作所需的多个 hack 进行比较。
  • 还要比较性能和分辨率独立性。
于 2013-04-15T16:10:35.317 回答