2009年4月23日 星期四

打磚塊--打到磚塊回頭版

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_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 + 50
        If e.KeyCode = Keys.Left Then PBox1.Left = PBox1.Left - 50
    End Sub

    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(i) = New PictureBox
            Dim r = Int(Rnd() * 256)
            Dim g = Int(Rnd() * 256)
            Dim b = Int(Rnd() * 256)
            With pbox(i)
                ' .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(i))
        Next
        x = 1
        y = 166
        dx = 1
        dy = 1
        Timer1.Enabled = True
        Timer1.Interval = 100
    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
        '有無打到磚塊
        Dim i As Integer
        For i = 1 To 40
            If x + ballsize > pbox(i).Left And x < pbox(i).Left + pbox(i).Width And y < pbox(i).Top + ballsize Then
                '第一次打到磚塊回頭
                If pbox(i).Visible = True Then dy = 1
                pbox(i).Visible = False
            End If
        Next

        If x > Me.Width - ballsize Then dx = -1
        '有無擋到球
        If y > 479 - ballsize And x + ballsize > PBox1.Left And x < PBox1.Left + PBox1.Width And y < PBox1.Top + ballsize Then
            dy = -1
        End If

        If x < 1 Then dx = 1
        If y < 1 Then dy = 1
        x = x + dx * 10
        y = y + dy * 12
        Me.Refresh()
    End Sub
End Class

沒有留言:

張貼留言