2009年4月28日 星期二

打磚塊 - 201 2009/04/28版

image

Public Class Form1
    Dim pbox(80) As PictureBox
    Dim Ball As Image = My.Resources.ball
    Dim x, y As Integer
    Dim dx, dy As Integer
    Dim ballsize = 28
    Dim scoAcc = 0
    Dim lossAcc = 0
    Dim dl = 10

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        KeyPreview = True
        NUpDown1.Value = 70
        NUpDown2.Value = 28
        Label1.Text = "得分數:" & scoAcc
        Label2.Text = "失球數:" & lossAcc
        Randomize()
        Me.Width = 10 + 60 * 8 + 10

        Timer1.Interval = 50
        Timer1.Enabled = True

        x = 0
        dx = 1
        y = 200
        dy = 1

        Dim i
        For i = 1 To 80
            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 16) * 30
                .Top = 50 + ((i - 1) \ 16) * 20
                .Width = 29
                .Height = 19
            End With
            Me.Controls.Add(pbox(i))
        Next
        PBox1.Width = 70
    End Sub

    Private Sub NUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NUpDown1.ValueChanged
        PBox1.Width = NUpDown1.Value
    End Sub

    Private Sub NUpDown2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NUpDown2.ValueChanged
        ballsize = NUpDown2.Value
    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 + 50
            If PBox1.Left + PBox1.Width > Me.Width Then PBox1.Left = Me.Width - PBox1.Width
        End If

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

        '
        Dim i
        For i = 1 To 80
            If y + ballsize > pbox(i).Top And y < pbox(i).Top + pbox(i).Height And (x > pbox(i).Left - ballsize) And (x < pbox(i).Left + pbox(i).Width) Then
                If pbox(i).Visible = True Then
                    pbox(i).Visible = False
                    If dy = 1 Then
                        dy = -1
                    Else
                        dy = 1
                    End If
                    scoAcc = scoAcc + 1
                    Label1.Text = "得分數:" & scoAcc
                    dl = Int(Rnd() * 5) + 10
                    Exit For
                End If
            End If
        Next

        '
        If y + ballsize > PBox1.Top And y < PBox1.Top + PBox1.Height And (x > PBox1.Left - ballsize) And (x < PBox1.Left + PBox1.Width) Then
            dy = -1
        End If

        If y > Me.Height Then
            lossAcc = lossAcc + 1
            Label2.Text = "失球數:" & lossAcc
            NewBall()
        End If

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

        x = x + dl * dx
        y = y + dl * dy

        Me.Refresh()
    End Sub

    Sub NewBall()
        y = 150
        Dim generator As New Random
        Dim randomValue As Integer = generator.Next(1, Me.Width)
        x = randomValue
        dy = 1
    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

沒有留言:

張貼留言