Selamat siang,sahabat blogger hari ini saya akan mengposting sebuah
codingan bagaimana membuat program bola mantul-mantul dengan menggunkana
VB.NET.
Berikut source codenya :
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Font
Imports System.Drawing.FontFamily
Public Class Form1
Dim x, y, xe, ye As Integer
Dim arahx, arahy As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
x = -100
y = -100
xe = 50
ye = 50
arahx = 1
arahy = 1
‘Timer1.Start()
End Sub
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
Dim g As Graphics = e.Graphics
If ComboBox1.SelectedIndex = 0 Then
g.DrawRectangle(Pens.Aqua, x, y, 100, 50)
Else
g.DrawEllipse(Pens.Aqua, x, y, 100, 50)
End If
Dim a() As Point = {New Point(x, y), New Point(x + 50, y + 100), New Point(x + 100, y), New Point(x, y)}
g.DrawLines(Pens.Red, a)
Dim gp As GraphicsPath = New GraphicsPath(FillMode.Alternate)
gp.AddLines(a)
g.FillPath(Brushes.Blue, gp)
g.DrawEllipse(Pens.Aqua, xe, ye, 50, 50)
End Sub
Private Sub Panel1_MouseClick(sender As Object, e As MouseEventArgs) Handles Panel1.MouseClick
x = e.X
y = e.Y
Panel1.Refresh()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
xe += arahx
ye += arahy
If xe + 50 > Panel1.Width Then
arahx *= -1
End If
If ye + 50 > Panel1.Height Then
arahy *= -1
End If
If ye < 0 Then
arahy *= -1
End If
If xe < 0 Then
arahx *= -1
End If
Panel1.Refresh()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer1.Stop()
End Sub
End Class
Screenshot :
Di sini saya menggunakan panel dan 2 button dan sebuah timer
Ini adalah tampilan awal programnya.Bola akan berjalan bila button start di klik

Ini adalah contoh pergerakan bola,bola akan berhenti bergerak bila button stop di klik

NB : Timer memiliki pernan yang sangat penting karena timer di gunakan untuk menjalankan bolanya.
Video :
Sekian postingan dari saya semoga postingan ini berguna bagi para sahabat blogger yang ingin mempelajarinya.
Terima Kasih.
Berikut source codenya :
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Font
Imports System.Drawing.FontFamily
Public Class Form1
Dim x, y, xe, ye As Integer
Dim arahx, arahy As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
x = -100
y = -100
xe = 50
ye = 50
arahx = 1
arahy = 1
‘Timer1.Start()
End Sub
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
Dim g As Graphics = e.Graphics
If ComboBox1.SelectedIndex = 0 Then
g.DrawRectangle(Pens.Aqua, x, y, 100, 50)
Else
g.DrawEllipse(Pens.Aqua, x, y, 100, 50)
End If
Dim a() As Point = {New Point(x, y), New Point(x + 50, y + 100), New Point(x + 100, y), New Point(x, y)}
g.DrawLines(Pens.Red, a)
Dim gp As GraphicsPath = New GraphicsPath(FillMode.Alternate)
gp.AddLines(a)
g.FillPath(Brushes.Blue, gp)
g.DrawEllipse(Pens.Aqua, xe, ye, 50, 50)
End Sub
Private Sub Panel1_MouseClick(sender As Object, e As MouseEventArgs) Handles Panel1.MouseClick
x = e.X
y = e.Y
Panel1.Refresh()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
xe += arahx
ye += arahy
If xe + 50 > Panel1.Width Then
arahx *= -1
End If
If ye + 50 > Panel1.Height Then
arahy *= -1
End If
If ye < 0 Then
arahy *= -1
End If
If xe < 0 Then
arahx *= -1
End If
Panel1.Refresh()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer1.Stop()
End Sub
End Class
Screenshot :
Di sini saya menggunakan panel dan 2 button dan sebuah timer
Ini adalah tampilan awal programnya.Bola akan berjalan bila button start di klik

Ini adalah contoh pergerakan bola,bola akan berhenti bergerak bila button stop di klik

NB : Timer memiliki pernan yang sangat penting karena timer di gunakan untuk menjalankan bolanya.
Video :
Sekian postingan dari saya semoga postingan ini berguna bagi para sahabat blogger yang ingin mempelajarinya.
Terima Kasih.