Permainan Puzzle
Cara
membuat permainan Puzzle langkah -langkahnya sebagai berikut:
- Buat 1 form dan 9 Button, dengan Name Button: btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, dan btn9.
- Ubah Text Form1: Puzzle, dan ubah Text masing-masing button dari btn1: 1
seterusnya sampai btn8: 8. Untuk btn9 tidak diberi Text (kosong).
- Pada saat form dijalankan posisi angka acak, seperti gambar dibawah ini:

Listing programnya sebagai berikut:
Private Sub
Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Dim
control As Windows.Forms.Control
For Each control In Me.Controls
If
control.GetType.Name = "Button" Then
btn1.Text = "6"
btn2.Text = "1"
btn3.Text = "8"
btn4.Text = "7"
btn5.Text = "5"
btn6.Text = "2"
btn7.Text = "4"
btn8.Text = "3"
btn9.Text = " "
End
If
Next
End Sub
4. Saat ditekan tombol yang terletak di dekat tombol kosong maka tombol tersebut akan menempati tombol kosong, begitu seterusnya sampai dengan angkanya tersusun dari 1 sampai 8.
Berikut listing programnya:
Private Sub
btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btn1.Click
Dim
tuker As String
If
btn2.Text = " " Then
tuker = btn1.Text
btn1.Text = btn2.Text
btn2.Text = tuker
ElseIf
btn4.Text = " " Then
tuker = btn1.Text
btn1.Text = btn4.Text
btn4.Text = tuker
End If
benar()
End Sub
Private Sub btn2_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
btn2.Click
Dim
tuker As String
If
btn1.Text = " " Then
tuker = btn2.Text
btn2.Text = btn1.Text
btn1.Text = tuker
ElseIf
btn3.Text = " " Then
tuker = btn2.Text
btn2.Text = btn3.Text
btn3.Text = tuker
ElseIf
btn5.Text = " " Then
tuker = btn2.Text
btn2.Text = btn5.Text
btn5.Text = tuker
End If
benar()
End Sub
Private Sub btn3_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
btn3.Click
Dim
tuker As String
If
btn2.Text = " " Then
tuker = btn3.Text
btn3.Text = btn2.Text
btn2.Text = tuker
ElseIf
btn6.Text = " " Then
tuker = btn3.Text
btn3.Text = btn6.Text
btn6.Text = tuker
End If
benar()
End Sub
Private Sub btn4_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
btn4.Click
Dim
tuker As String
If
btn1.Text = " " Then
tuker = btn4.Text
btn4.Text = btn1.Text
btn1.Text = tuker
ElseIf
btn5.Text = " " Then
tuker = btn4.Text
btn4.Text = btn5.Text
btn5.Text = tuker
ElseIf
btn7.Text = " " Then
tuker = btn4.Text
btn4.Text = btn7.Text
btn7.Text = tuker
End If
benar()
End Sub
Private Sub btn5_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
btn5.Click
Dim
tuker As String
If
btn2.Text = " " Then
tuker = btn5.Text
btn5.Text = btn2.Text
btn2.Text = tuker
ElseIf
btn4.Text = " " Then
tuker = btn5.Text
btn5.Text = btn4.Text
btn4.Text = tuker
ElseIf
btn6.Text = " " Then
tuker = btn5.Text
btn5.Text = btn6.Text
btn6.Text = tuker
ElseIf
btn8.Text = " " Then
tuker = btn5.Text
btn5.Text = btn8.Text
btn8.Text = tuker
End If
benar()
End Sub
Private Sub
Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btn6.Click
Dim
tuker As String
If
btn9.Text = " " Then
tuker = btn6.Text
btn6.Text = btn9.Text
btn9.Text = tuker
ElseIf
btn3.Text = " " Then
tuker = btn6.Text
btn6.Text = btn3.Text
btn3.Text = tuker
ElseIf
btn5.Text = " " Then
tuker = btn6.Text
btn6.Text = btn5.Text
btn5.Text = tuker
End If
benar()
End Sub
Private Sub
btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btn7.Click
Dim
tuker As String
If
btn4.Text = " " Then
tuker = btn7.Text
btn7.Text = btn4.Text
btn4.Text = tuker
ElseIf
btn8.Text = " " Then
tuker = btn7.Text
btn7.Text = btn8.Text
btn8.Text = tuker
End If
benar()
End Sub
Private Sub btn8_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
btn8.Click
Dim
tuker As String
If
btn9.Text = " " Then
tuker = btn8.Text
btn8.Text = btn9.Text
btn9.Text = tuker
ElseIf
btn7.Text = " " Then
tuker = btn8.Text
btn8.Text = btn7.Text
btn7.Text = tuker
ElseIf
btn5.Text = " " Then
tuker = btn8.Text
btn8.Text = btn5.Text
btn5.Text = tuker
End If
benar()
End Sub
Private Sub btn9_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
btn9.Click
Dim
tuker As String
If
btn6.Text = " " Then
tuker = btn9.Text
btn9.Text = btn6.Text
btn6.Text = tuker
ElseIf
btn8.Text = " " Then
tuker = btn9.Text
btn9.Text = btn8.Text
btn8.Text = tuker
End If
benar()
End Sub
Berikut listing programnya:
Private Sub
benar()
If
btn1.Text = "1" And btn2.Text = "2"
And btn3.Text = "3"
And btn4.Text = "4"
And btn5.Text = "5"
And btn6.Text = "6"
And btn7.Text = "7"
And btn8.Text = "8"
Then
MessageBox.Show("SELAMAT ANDA BERHASIL")
End If
End Sub


Tidak ada komentar:
Posting Komentar