Public Class Form1
Private dx As Integer
Private dy As Integer
Private x As Integer
Private y As Integer
Private ix As Integer
Private iy As Integer
Private ballWidth As Integer = 50
Private ballHeigth As Integer = 50
Dim RandomClass As New Random()
Dim g As Graphics = Me.CreateGraphics
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Timer1.Interval = 100
Dim rnd As New Random
dx = rnd.Next(1, 4)
dy = rnd.Next(1, 4)
x = rnd.Next(0, Me.ClientSize.Width - ballWidth)
y = rnd.Next(0, Me.ClientSize.Height - ballWidth)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
Me.UpdateStyles()
End Sub
Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
ix = e.X
iy = e.Y
If ix >= x - dx And iy >= y - dy Then
Timer1.Stop()
g.Clear(Me.BackColor)
End If
End Sub
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim RandomNumber As Integer
Dim randoms As Integer
RandomNumber = RandomClass.Next(50, 55)
randoms = RandomClass.Next(50, 55)
e.Graphics.FillEllipse(Brushes.Black, x, y, randoms, RandomNumber)
e.Graphics.DrawEllipse(Pens.Black, x, y, randoms, RandomNumber)
End Sub
Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
x += dx
If x < 0 Then
dx = -dx
ElseIf x + ballWidth > Me.ClientSize.Width Then
dx = -dx
End If
y += dy
If y < 0 Then
dy = -dy
ElseIf y + ballHeigth > Me.ClientSize.Height Then
dy = -dy
End If
Me.Invalidate()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
End Class
Tag : .NET, Win (Windows App), Class Library, VB.NET, C#
Public Class Form1
Dim RandomClass As New Random()
Dim g As Graphics = Me.CreateGraphics
Private dx As Integer
Private dy As Integer
Private x As Integer
Private y As Integer
Private ix As Integer
Private iy As Integer
Private ballWidth As Integer = 50
Private ballHeigth As Integer = 50
Dim RandomWidth As Integer
Dim randomHeight As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Timer1.Interval = 100
Dim rnd As New Random
dx = rnd.Next(1, 4)
dy = rnd.Next(1, 4)
x = rnd.Next(0, Me.ClientSize.Width - ballWidth)
y = rnd.Next(0, Me.ClientSize.Height - ballHeigth)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
Me.UpdateStyles()
End Sub
Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
ix = e.X
iy = e.Y
Dim subX As Integer
Dim subY As Integer
If ix >= x And iy >= y Then
subX = x + RandomWidth
subY = y + randomHeight
If ix <= subX And iy <= subY Then
Timer1.Stop()
g.Clear(Me.BackColor)
End If
End If
End Sub
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
randomHeight = RandomClass.Next(50, 55)
RandomWidth = RandomClass.Next(50, 60)
e.Graphics.FillEllipse(Brushes.Black, x, y, RandomWidth, randomHeight)
e.Graphics.DrawEllipse(Pens.Black, x, y, RandomWidth, randomHeight)
End Sub
Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
x += dx
If x < 0 Then
dx = -dx
ElseIf x + ballWidth > Me.ClientSize.Width Then
dx = -dx
End If
y += dy
If y < 0 Then
dy = -dy
ElseIf y + ballHeigth > Me.ClientSize.Height Then
dy = -dy
End If
Me.Invalidate()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
End Class