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 = 50
Timer1.Enabled = True
x = 0
dx = 1
y = 200
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 + 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 40
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
dy = 1
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 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
沒有留言:
張貼留言