图像框移动顺畅?

时间:2014-03-17 17:33:05

标签: vb.net visual-studio

我想知道是否有办法让图像框顺畅地流过表格 我在表单上打开了双缓冲区,并没有那么多改进。

Public Class Form1
    Dim Offset As Point
    Dim moveRight As Boolean = True
    Dim moveDown As Boolean = True
    Const Speed As Decimal = 1

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If moveRight = True Then
            pxbox.Left += Speed
            If (pxbox.Left + pxbox.Width) > Me.ClientSize.Width Then moveRight = False
        Else
            pxbox.Left -= Speed
            If pxbox.Left < 0 Then moveRight = True

        End If

        If moveDown = True Then
            pxbox.Top += Speed
            If (pxbox.Top + pxbox.Height) > Me.ClientSize.Height Then moveDown = False
        Else
            pxbox.Top -= Speed
            If pxbox.Top <= 0 Then moveDown = True

        End If

        'Collision Method 1
        Dim Col As Boolean = Collision(pxbox, pbpaddle1)
        If Col = True Then
            moveRight = Not moveRight
            moveDown = Not moveDown
        End If

        Dim Col2 As Boolean = Collision(pxbox, pbpaddle2)

        If Col2 = True Then
            moveRight = Not moveRight
            moveDown = Not moveDown
        End If

        'Method 2
        'If pxbox.Bounds.IntersectsWith(pbpaddle1.Bounds) Then
        '    moveRight = Not moveRight
        '    moveDown = Not moveDown
        'End If
        'If pxbox.Bounds.IntersectsWith(pbpaddle2.Bounds) Then
        '    moveRight = Not moveRight
        '    moveDown = Not moveDown
        'End If
    End Sub

    Private Function Collision(ByVal P1 As PictureBox, ByVal P2 As PictureBox) As Boolean
        If P1.Left + P1.Width < P2.Left Then Return False
        If P2.Left - 15 + P2.Width < P1.Left Then Return False
        If P1.Top + P1.Height < P2.Top Then Return False
        If P2.Top + P2.Height < P1.Top Then Return False

        Return True

    End Function

1 个答案:

答案 0 :(得分:0)

Winform的Timer控件实际上并不是为了获得平滑动画所需的精确度而设计的。你也会有很多开销试图在你的动画中使用控件来实现“精灵”。

你真的应该考虑使用GDI绘图代码将所有绘图用于内存位图,然后将该位图发布到单个大图像控件,表单背景或其他一些显示表面。