2009年4月29日 星期三

打磚塊 -- 料一甲20080429版

image

Public Class Form1
    Dim sco = 0
    Dim loss = 0
    Dim pbox(40) As PictureBox
    Dim ball As Image = Image.FromFile("..\..\ball.gif")
    Dim ballsize
    Dim x, y As Integer
    Dim dx, dy As Integer
    Dim timeAcc As Single = 0

    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
        KeyPreview = True
        ballsize = 30
        PBox1.Width = 50
        Label1.Text = "得分:" & sco
        Label2.Text = "失球:" & loss
        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 = 50 + ((i - 1) \ 8) * 25
            End With
            Me.Controls.Add(pbox(i))
        Next
        x = 1
        y = 200
        dx = 1
        dy = 1
        Timer1.Enabled = True
        Timer1.Interval = 50
    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
        '有無打到磚塊
        timeAcc = timeAcc + Timer1.Interval
        Label5.Text = "計時:" & Int(timeAcc / 1000) & "秒"
        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
                    If dy = -1 Then
                        dy = 1
                    Else
                        dy = -1
                    End If
                    pbox(i).Visible = False
                    sco = sco + 1
                    Label1.Text = "得分:" & sco

                    If sco = 40 Then
                        Timer1.Enabled = False
                        MsgBox("成績:" & Int(timeAcc) & "秒")
                    End If

                End If
            End If
        Next

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

        If y > 650 Then
            loss = loss + 1
            Label2.Text = "失球:" & loss
            ' Timer1.Enabled = False
            y = 200
        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

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

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

沒有留言:

張貼留言