2009年4月22日 星期三

打磚塊--部份完成例

image

Public Class Form1
    Dim pbox(40) As PictureBox
    Dim ball As Image = Image.FromFile("..\..\ball.gif")
    Dim ballsize = 36
    Dim x, y As Integer
    Dim dx, dy As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Width = 474

        Dim i
        For i = 1 To 40
            pbox(1) = New PictureBox
            Dim r = Int(Rnd() * 256)
            Dim g = Int(Rnd() * 256)
            Dim b = Int(Rnd() * 256)
            With pbox(1)
                ' .BackColor = Color.Blue
                .BackColor = Color.FromArgb(255, r, g, b)
                .Width = 55
                .Height = 23
                .Left = 5 + (i Mod 8) * (.Width + 2)
                .Top = 5 + ((i - 1) \ 8) * 25
            End With
            Me.Controls.Add(pbox(1))
        Next
        dx = 1
        dy = 1
        Timer1.Enabled = True
        Timer1.Interval = 10
    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

    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 > Me.Height - ballsize Then dy = -1
        If x < 1 Then dx = 1
        If y < 1 Then dy = 1
        x = x + dx * 10
        y = y + dy * 10
        Me.Refresh()
    End Sub
End Class

沒有留言:

張貼留言