Puzzle merupakan
permainan yang ada sudah lama sejak zaman dahulu kala. Permainan ini dapat
merangsang otak untuk berpikir kritis dalam menyelesaikan masalah. Kali ini
saya ingin share tugas pemrogaman visual ke 4 yang ke dua tentan membuat
puzzle. Sudah saya cari disana sini tapi tidak ketemu juga akhirnya saya buat
sesuai logika saya sendiri. Berikut cara membuat game puzzle menggunakan visual
studio 2010 Langkah pertama
buka visual basic dan buat form dengan tampilan sebagai berikut :
Agar dapat
berjalan seperti diatas maka kita akan tambahkann perintah disetiap button 1
sampai 9, berikut kode perintahnya.
Demikian progam
yang saya buat untuk melengkapi tugas pemrogaman sebelumnya semoga bermanfaat.
Untuk source kode selengkapnya dapat dilihat dibawah ini.
susunan form tampilan game puzzle |
Jika sudah maka
tinggal memasukan source code ke setiap tombol. Namun sebelumnya saya akan
jelaskan bagaimana cara progam ini berjalan.
- Tombbol acak berfungsi untuk mengacak angaka 1 – 0 , sekaligus akan menjadi tombol stop saat progam dijalankan
- Tombol tambah kecepatan berfungsi untuk menambah kecepatan proses pengacakan tombol puzzle. Jika kecepatan sudah sampai maksimum maka muncul pesan “ kecepatan maksimum tercapai”
- Perpindahan angka hanya bisa dilakukan jika ada tombol kosong disebelah tombol yang isi. Jika tombol isi di pilih sedang tombol kosong ada disebelahnya maka secara otomatis tombol akan bertukar tempat. Jika sebelahnya isi maka tombol tidak akan bertukar tempat
- Jika semua tombol sudah terletak pada tempatnya masing masing maka akan muncul “ selamat anda berhasil”
OK paham lah
yah... kalo kurang paham main ketempat saya aja nanti saya jelaskan. Pertama
kita akan membuat tombol acaknya dahulu, kenapa tidak masukan angkanya aja dulu?
Ya terserah saya aja yah... yang penting kan nanti hasilnya.....
Pada tombol acak
kita masukan kode berikut
PrivateSubbacak_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handlesbacak.Click
jalan = Notjalan
Timer1.Enabled = jalan
If Timer1.Enabled = TrueThen
bacak.Text = "STOP"
Else
bacak.Text = "ACAK"
EndIf
EndSub
Lho kok error...
ya iya lah itu karena kita belum membuat timer. Timer disini berfungsi untuk
mengaca angka secara continyu. Untuk itu kita harus buat object timer.
Selanjutnya pada object timer kita masukan kode berikut :
PrivateSub Timer1_Tick(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
Timer1.Tick
DimacakAsInteger
Do
acak = Rnd() * 10
b1.Text = acak
'
If b1.Text = 10 Or b1.Text = 0 Or b1.Text = 9 Then
'b1.Text = ""
'End If
LoopWhileacak = 10 Oracak =
9
Do
acak = Rnd() * 10
b2.Text = acak
'If b2.Text = 10 Or b2.Text = 0 Or
b2.Text = 9 Then
'b2.Text = ""
' End If
LoopWhile b2.Text = b1.Text Oracak
= 10 Oracak = 9
Do
acak = Rnd() * 10
b3.Text = acak
'
If b3.Text = 10 Or b3.Text = 0 Or b3.Text = 9 Then
'b3.Text = ""
'End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = 10 Oracak
= 9
Do
acak = Rnd() * 10
b4.Text = acak
' If b4.Text = 10 Or b4.Text = 0 Or
b4.Text = 9 Then
'b4.Text = ""
' End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = b3.Text Oracak = 10 Oracak =
9
Do
acak = Rnd() * 10
b5.Text = acak
'If b5.Text = 10 Or b5.Text = 0 Or
b5.Text = 9 Then
'b5.Text = ""
'End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = b3.Text Oracak = b4.Text Oracak
= 10 Oracak = 9
Do
acak = Rnd() * 10
b6.Text = acak
'If b6.Text = 10 Or b6.Text = 0 Or
b6.Text = 9 Then
'b6.Text = ""
'End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = b3.Text Oracak = b4.Text Oracak
= b5.Text Oracak = 10 Oracak
= 9
Do
acak = Rnd() * 10
b7.Text = acak
'If b7.Text = 10 Or b7.Text = 0 Or
b7.Text = 9 Then
'b7.Text = ""
'End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = b3.Text Oracak = b4.Text Oracak
= b5.Text Oracak = b6.Text Oracak = 10 Oracak =
9
Do
acak = Rnd() * 10
b8.Text = acak
' If b8.Text = 10 Or b8.Text = 0 Or
b8.Text = 9 Then
'b8.Text = ""
'End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = b3.Text Oracak = b4.Text Oracak
= b5.Text Oracak = b6.Text Oracak = b7.Text Oracak
= 10 Oracak = 9
Do
acak = Rnd() * 10
b9.Text = acak
' If b9.Text = 10 Or b9.Text = 0 Or
b9.Text = 9 Then
'b9.Text = ""
'End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = b3.Text Oracak = b4.Text Oracak
= b5.Text Oracak = b6.Text Oracak = b7.Text Oracak
= b8.Text Oracak = 10 Oracak
= 9
Dim control AsWindows.Forms.Control
ForEach control InMe.Controls
Ifcontrol.Text = "0"Then
control.Text = ""
EndIf
Next
EndSub
Sekarang kita
coba jalankan maka hasilnya button 1 sampai 9 telah terisi angka, sebenarnya
ada cara yang lebih mudah yaitu dengan menggunakan array tapi pakai cara yang
susah aja dulu biar jalan logikanya. Berikut tampilan progamnya ketika diacak
Ketika tombol
stop di hentikan maka proses acak akan berhenti dan tombol stop akan berubah
menjadi tombol acak lagi. Setelah kita dapat mengacak angka maka sekarang kita
tinggal membuat perintah agar tombol mau berpindah tempat. Sebagai contoh
ketika tombol acak dihentikan maka tampilannya akan sebagai berikut. Saat angka
7 kita klik maka angka 7 akan berpindah ke tengah dan tombol kosong akan
berpindah ke bawah.
PrivateSub b1_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b1.Click
If b2.Text = ""Then
b2.Text = b1.Text
b1.Text = ""
EndIf
If b4.Text = ""Then
b4.Text = b1.Text
b1.Text = ""
EndIf
EndSub
PrivateSub b2_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b2.Click
If b1.Text = ""Then
b1.Text = b2.Text
b2.Text = ""
EndIf
If b5.Text = ""Then
b5.Text = b2.Text
b2.Text = ""
EndIf
If b3.Text = ""Then
b3.Text = b2.Text
b2.Text = ""
EndIf
EndSub
PrivateSub b3_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b3.Click
If b2.Text = ""Then
b2.Text = b3.Text
b3.Text = ""
EndIf
If b6.Text = ""Then
b6.Text = b3.Text
b3.Text = ""
EndIf
EndSub
PrivateSub b4_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b4.Click
If b1.Text = ""Then
b1.Text = b4.Text
b4.Text = ""
EndIf
If b5.Text = ""Then
b5.Text = b4.Text
b4.Text = ""
EndIf
If b7.Text = ""Then
b7.Text = b4.Text
b4.Text = ""
EndIf
EndSub
PrivateSub b5_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b5.Click
If b2.Text = ""Then
b2.Text = b5.Text
b5.Text = ""
EndIf
If b4.Text = ""Then
b4.Text = b5.Text
b5.Text = ""
EndIf
If b6.Text = ""Then
b6.Text = b5.Text
b5.Text = ""
EndIf
If b8.Text = ""Then
b8.Text = b5.Text
b5.Text = ""
EndIf
EndSub
PrivateSub b6_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b6.Click
If b3.Text = ""Then
b3.Text = b6.Text
b6.Text = ""
EndIf
If b5.Text = ""Then
b5.Text = b6.Text
b6.Text = ""
EndIf
If b9.Text = ""Then
b9.Text = b6.Text
b6.Text = ""
EndIf
EndSub
PrivateSub b7_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b7.Click
If b4.Text = ""Then
b4.Text = b7.Text
b7.Text = ""
EndIf
If b8.Text = ""Then
b8.Text = b7.Text
b7.Text = ""
EndIf
EndSub
PrivateSub b8_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b8.Click
If b5.Text = ""Then
b5.Text = b8.Text
b8.Text = ""
EndIf
If b9.Text = ""Then
b9.Text = b8.Text
b8.Text = ""
EndIf
If b7.Text = ""Then
b7.Text = b8.Text
b8.Text = ""
EndIf
EndSub
PrivateSub b9_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b9.Click
If b8.Text = ""Then
b8.Text = b9.Text
b9.Text = ""
EndIf
If b6.Text = ""Then
b6.Text = b9.Text
b9.Text = ""
EndIf
If (b1.Text = "1"And b2.Text = "2"And b3.Text = "3"And b4.Text = "4"And b5.Text = "5"And b6.Text = "6"And b7.Text = "7"And b8.Text = "8"And b9.Text = "")
Then
MsgBox("selamatandaberhasil")
EndIf
EndSub
Pada button 9
kita beri perintah untuk menampilkan kotak dialog “ selamat anda berhasil” jika
kita dapat menyelesaikan puzzle yang kita buat. Karena sudah pasti ketika kita
menyelesaikan puzzle maka tombol ke 9 akan di tekan terakhir kali...
Demikian cara
membuat game puzzle sederhana dengan visual studio. Eh... ada yang kurang...
itu tombol tambah kecepatannya. Sebenarnya tidak terlalu penting si hanya
sbagai pelengkap saja. Tombol tambah kecepatan hanya berfungsi untuk mengubah
nilai interval pada timer sampai nilai tertentu.
Jika kecepatan
sudah maksimum maka akan muncul pesan “ kecepatan maksimum tercapai “ untuk itu
kita perlu menambahkan perintah pada botton tambah kecepatan sbagai berikut
PrivateSub Button1_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
Button1.Click
Timer1.Interval = Timer1.Interval / 2
If Timer1.Interval < 32 Then
MsgBox("kecepatanmaksimumtercapai")
Timer1.Interval = 15
PublicClassForm1
DimjalanAsBoolean = False
PrivateSub b1_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b1.Click
If b2.Text = ""Then
b2.Text = b1.Text
b1.Text = ""
EndIf
If b4.Text = ""Then
b4.Text = b1.Text
b1.Text = ""
EndIf
EndSub
PrivateSub b2_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b2.Click
If b1.Text = ""Then
b1.Text = b2.Text
b2.Text = ""
EndIf
If b5.Text = ""Then
b5.Text = b2.Text
b2.Text = ""
EndIf
If b3.Text = ""Then
b3.Text = b2.Text
b2.Text = ""
EndIf
EndSub
PrivateSub b3_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b3.Click
If b2.Text = ""Then
b2.Text = b3.Text
b3.Text = ""
EndIf
If b6.Text = ""Then
b6.Text = b3.Text
b3.Text = ""
EndIf
EndSub
PrivateSub b4_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b4.Click
If b1.Text = ""Then
b1.Text = b4.Text
b4.Text = ""
EndIf
If b5.Text = ""Then
b5.Text = b4.Text
b4.Text = ""
EndIf
If b7.Text = ""Then
b7.Text = b4.Text
b4.Text = ""
EndIf
EndSub
PrivateSub b5_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b5.Click
If b2.Text = ""Then
b2.Text = b5.Text
b5.Text = ""
EndIf
If b4.Text = ""Then
b4.Text = b5.Text
b5.Text = ""
EndIf
If b6.Text = ""Then
b6.Text = b5.Text
b5.Text = ""
EndIf
If b8.Text = ""Then
b8.Text = b5.Text
b5.Text = ""
EndIf
EndSub
PrivateSub b6_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b6.Click
If b3.Text = ""Then
b3.Text = b6.Text
b6.Text = ""
EndIf
If b5.Text = ""Then
b5.Text = b6.Text
b6.Text = ""
EndIf
If b9.Text = ""Then
b9.Text = b6.Text
b6.Text = ""
EndIf
EndSub
PrivateSub b7_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b7.Click
If b4.Text = ""Then
b4.Text = b7.Text
b7.Text = ""
EndIf
If b8.Text = ""Then
b8.Text = b7.Text
b7.Text = ""
EndIf
EndSub
PrivateSub b8_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b8.Click
If b5.Text = ""Then
b5.Text = b8.Text
b8.Text = ""
EndIf
If b9.Text = ""Then
b9.Text = b8.Text
b8.Text = ""
EndIf
If b7.Text = ""Then
b7.Text = b8.Text
b8.Text = ""
EndIf
EndSub
PrivateSub b9_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
b9.Click
If b8.Text = ""Then
b8.Text = b9.Text
b9.Text = ""
EndIf
If b6.Text = ""Then
b6.Text = b9.Text
b9.Text = ""
EndIf
If (b1.Text = "1"And b2.Text = "2"And b3.Text = "3"And b4.Text = "4"And b5.Text = "5"And b6.Text = "6"And b7.Text = "7"And b8.Text = "8"And b9.Text = "")
Then
MsgBox("selamatandaberhasil")
EndIf
EndSub
PrivateSubbacak_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handlesbacak.Click
jalan = Notjalan
Timer1.Enabled = jalan
If Timer1.Enabled = TrueThen
bacak.Text = "STOP"
Else
bacak.Text = "ACAK"
EndIf
EndSub
PrivateSub Timer1_Tick(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
Timer1.Tick
DimacakAsInteger
Do
acak = Rnd() * 10
b1.Text = acak
'
If b1.Text = 10 Or b1.Text = 0 Or b1.Text = 9 Then
'b1.Text = ""
'End If
LoopWhileacak = 10 Oracak =
9
Do
acak = Rnd() * 10
b2.Text = acak
'If b2.Text = 10 Or b2.Text = 0 Or
b2.Text = 9 Then
'b2.Text = ""
' End If
LoopWhile b2.Text = b1.Text Oracak
= 10 Oracak = 9
Do
acak = Rnd() * 10
b3.Text = acak
'
If b3.Text = 10 Or b3.Text = 0 Or b3.Text = 9 Then
'b3.Text = ""
'End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = 10 Oracak
= 9
Do
acak = Rnd() * 10
b4.Text = acak
' If b4.Text = 10 Or b4.Text = 0 Or
b4.Text = 9 Then
'b4.Text = ""
' End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = b3.Text Oracak = 10 Oracak =
9
Do
acak = Rnd() * 10
b5.Text = acak
'If b5.Text = 10 Or b5.Text = 0 Or
b5.Text = 9 Then
'b5.Text = ""
'End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = b3.Text Oracak = b4.Text Oracak
= 10 Oracak = 9
Do
acak = Rnd() * 10
b6.Text = acak
'If b6.Text = 10 Or b6.Text = 0 Or
b6.Text = 9 Then
'b6.Text = ""
'End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = b3.Text Oracak = b4.Text Oracak
= b5.Text Oracak = 10 Oracak
= 9
Do
acak = Rnd() * 10
b7.Text = acak
'If b7.Text = 10 Or b7.Text = 0 Or
b7.Text = 9 Then
'b7.Text = ""
'End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = b3.Text Oracak = b4.Text Oracak
= b5.Text Oracak = b6.Text Oracak = 10 Oracak =
9
Do
acak = Rnd() * 10
b8.Text = acak
' If b8.Text = 10 Or b8.Text = 0 Or
b8.Text = 9 Then
'b8.Text = ""
'End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = b3.Text Oracak = b4.Text Oracak
= b5.Text Oracak = b6.Text Oracak = b7.Text Oracak
= 10 Oracak = 9
Do
acak = Rnd() * 10
b9.Text = acak
' If b9.Text = 10 Or b9.Text = 0 Or
b9.Text = 9 Then
'b9.Text = ""
'End If
LoopWhileacak = b1.Text Oracak
= b2.Text Oracak = b3.Text Oracak = b4.Text Oracak
= b5.Text Oracak = b6.Text Oracak = b7.Text Oracak
= b8.Text Oracak = 10 Oracak
= 9
Dim control AsWindows.Forms.Control
ForEach control InMe.Controls
Ifcontrol.Text = "0"Then
control.Text = ""
EndIf
Next
EndSub
PrivateSub Button1_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs) Handles
Button1.Click
Timer1.Interval = Timer1.Interval / 2
If Timer1.Interval < 32 Then
MsgBox("kecepatanmaksimumtercapai")
Timer1.Interval = 15
EndIf
EndSub
PrivateSub Label1_Click(ByVal
sender AsSystem.Object,
ByVal e AsSystem.EventArgs)
EndSub
EndClass
sekiann ..
ConversionConversion EmoticonEmoticon