2009年4月21日 星期二

打磚塊--自動移動擋板版

image

'自動移動擋板版
Public Class Form1
    Dim pbox(40) As PictureBox
    Dim Ball As Image = My.Resources.ball
    Dim x, y As Integer
    Dim dx, dy As Integer
    Dim ballsize = 36

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
        Me.Width = 10 + 60 * 8 + 10

        Timer1.Interval = 10
        Timer1.Enabled = True

        x = 0
        dx = 1
        y = 10
        dy = 1

        Dim i
        For i = 1 To 40
            pbox(i) = New PictureBox
            With pbox(i)
                Dim r = Int(Rnd() * 256)
                Dim g = Int(Rnd() * 256)
                Dim b = Int(Rnd() * 256)
                .BackColor = Color.FromArgb(255, r, g, b)
                .Left = 5 + (i Mod 8) * 60
                .Top = 50 + ((i - 1) \ 8) * 20
                .Width = 59
                .Height = 19
            End With
            Me.Controls.Add(pbox(i))
        Next
    End Sub
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Right Then PBox1.Left = PBox1.Left + 20
        If e.KeyCode = Keys.Left Then PBox1.Left = PBox1.Left - 20
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If x > Me.Width - ballsize Then dx = -1
        ' If y > 585 - ballsize Then dy = -1

        '
        '
        '自動移動擋板版
        PBox1.Left = x - PBox1.Width / 2
        '
        If y > 585 - ballsize Then
            If (x + ballsize > PBox1.Left) And (x < PBox1.Left + PBox1.Width) Then
                dy = -1
            End If
        End If

        If x < 1 Then dx = 1
        If y < 1 Then dy = 1

        x = x + 10 * dx
        y = y + 10 * dy

        Me.Refresh()
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim g As Graphics = e.Graphics
        g.DrawImage(Ball, x, y, ballsize, ballsize)
    End Sub
End Class

沒有留言:

張貼留言