Selasa, 11 Desember 2012

TUTORIAL PROGRAM PUZZLE MODUL 3



TUTORIAL GAME PUZZLE




KOMPONEN TOOLBOX :
NO
KOMPONEN
PROPERTIS
KETERANGAN
1
FORM
NAME
TEXT
FORM1

2
LABEL 1
NAME
TEXT
LABEL1
SILAHKAN KLIK DISINI UNTUK BERMAIN PUZZLE
3
BUTTON1
NAME
TEXT
BUTTON
MAIN



KOMPONEN TOOLBOX :
NO
KOMPONEN
PROPERTIS
KETERANGAN
1
FORM
NAME
TEXT
FORM1
PUZZLE
2
BUTTON1
NAME
TEXT
BUTTON1
1
3
BUTTON2
NAME
TEXT
BUTTON2
2
4
BUTTON3
NAME
TEXT
BUTTON3
3
5
BUTTON4
NAME
TEXT
BUTTON4
4
6
BUTTON5
NAME
TEXT
BUTTON5
5
7
BUTTON6
NAME
TEXT
BUTTON6
6
8
BUTTON7
NAME
TEXT
BUTTON7
7
9
BUTTON8
NAME
TEXT
BUTTON8
8
10
BUTTON9
NAME
TEXT
BUTTON9

11
BUTTON10
NAME
TEXT
BUTTON10
ACAK
12
BUTTON11
NAME
TEXT
BUTTON11
STOP
13
BUTTON12
NAME
TEXT
BUTTON12
EXIT
14
BUTTON13
NAME
TEXT
BUTTON13
RESTART
15
LABEL1
NAME
TEXT
LABEL1
TIME :
16
LABEL2
NAME
TEXT
LABEL2
MINUTES
17
LABEL3
NAME
TEXT
LABEL3
TIME LEFT :
18
LABEL4
NAME
TEXT
LABEL4
19
TEXTBOX1
NAME
TEXT
TEXTBOX1
20
TIMER1
NAME
TEXT
TIMER1
21
TIMER2
NAME
TEXT
TIMER2

ANALISIS LISTING :
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.Show()
        Me.Hide()
        BackColor = Color.AntiqueWhite
    End Sub

Tombol untuk pindah ke form berikutnya dengan menghilangkan form sebeumnya
Memberi warna pada form 2
End Class

Public Class Form2
    Dim smt As String
    Dim acak As Integer
    Dim gambar As Object
    Dim timeleft As TimeSpan
    Dim starttime As DateTime
    Dim ttlMins As Integer

Untuk mendeklarasikan

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Timer1.Enabled = True
        acak = Rnd() * 10
    End Sub
Mengaktifkan timer dan mengacak puzzle

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Timer1.Enabled = False
        Button11.Enabled = False
        Button12.Enabled = True
        Timer2.Interval = 1000
        Timer2.Enabled = True
        ttlMins = Integer.Parse(TextBox1.Text)
        starttime = Now.AddMinutes(ttlMins)
        timeleft = starttime.Subtract(Now)
        Label4.Text = Format(timeleft.Minutes, "#0") & " Mins" & "  " & Format(timeleft.Seconds, "00") & " Secs"
    End Sub

Memberhentikan puzzle yang di acak serta mengaktifkan timer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button2.Text = "" Then
            gambar = Button1.BackgroundImage
            Button1.BackgroundImage = Button2.BackgroundImage
            Button2.BackgroundImage = gambar
            smt = Button1.Text
            Button1.Text = Button2.Text
            Button2.Text = smt
        ElseIf Button4.Text = "" Then
            gambar = Button1.BackgroundImage
            Button1.BackgroundImage = Button4.BackgroundImage
            Button4.BackgroundImage = gambar
            smt = Button1.Text
            Button1.Text = Button4.Text
            Button4.Text = smt
        End If

Memberi perinth untuk memindahkan puzzle pada button1

        If Button1.Text = "1" And Button2.Text = "2" And Button3.Text = "3" And Button4.Text = "4" And Button5.Text = "5" And Button6.Text = "6" And Button7.Text = "7" And Button8.Text = "8" And Button9.Text = "" Then
            MsgBox("SELAMAT")
        End If
    End Sub

Memberi msgbox ketika kita bisa menyusun dengan benar dan urut yang bertuliskan “SELAMAT”

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Button1.Text = "" Then
            gambar = Button2.BackgroundImage
            Button2.BackgroundImage = Button2.BackgroundImage
            Button1.BackgroundImage = gambar
            smt = Button2.Text
            Button2.Text = Button1.Text
            Button1.Text = smt
        ElseIf Button3.Text = "" Then
            gambar = Button2.BackgroundImage
            Button2.BackgroundImage = Button3.BackgroundImage
            Button3.BackgroundImage = gambar
            smt = Button2.Text
            Button2.Text = Button3.Text
            Button3.Text = smt
        ElseIf Button5.Text = "" Then
            gambar = Button5.BackgroundImage
            Button5.BackgroundImage = Button2.BackgroundImage
            Button2.BackgroundImage = gambar
            smt = Button5.Text
            Button5.Text = Button2.Text
            Button2.Text = smt

Memberi perinth untuk memindahkan puzzle pada button2

        End If
        If Button1.Text = "1" And Button2.Text = "2" And Button3.Text = "3" And Button4.Text = "4" And Button5.Text = "5" And Button6.Text = "6" And Button7.Text = "7" And Button8.Text = "8" And Button9.Text = "" Then
            MsgBox("SELAMAT")
        End If
    End Sub

Memberi msgbox ketika kita bisa menyusun dengan benar dan urut bertuliska “SELAMAT”

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Button2.Text = "" Then
            gambar = Button3.BackgroundImage
            Button3.BackgroundImage = Button2.BackgroundImage
            Button2.BackgroundImage = gambar
            smt = Button3.Text
            Button3.Text = Button2.Text
            Button2.Text = smt
        ElseIf Button6.Text = "" Then
            gambar = Button3.BackgroundImage
            Button3.BackgroundImage = Button6.BackgroundImage
            Button6.BackgroundImage = gambar
            smt = Button3.Text
            Button3.Text = Button6.Text
            Button6.Text = smt
        End If

Memberi perinth untuk memindahkan puzzle pada button3

        If Button1.Text = "1" And Button2.Text = "2" And Button3.Text = "3" And Button4.Text = "4" And Button5.Text = "5" And Button6.Text = "6" And Button7.Text = "7" And Button8.Text = "8" And Button9.Text = "" Then
            MsgBox("SELAMAT")
        End If
    End Sub

Memberi msgbox ketika kita bisa menyusun dengan benar dan urut beruliska “SELAMAT”

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        gambar = Button9.BackgroundImage
        Button9.BackgroundImage = Button4.BackgroundImage
        Button4.BackgroundImage = gambar
        smt = Button9.Text
        Button9.Text = Button4.Text
        Button4.Text = smt

        gambar = Button9.BackgroundImage
        Button9.BackgroundImage = Button3.BackgroundImage
        Button3.BackgroundImage = gambar
        smt = Button9.Text
        Button9.Text = Button3.Text
        Button3.Text = smt

        gambar = Button2.BackgroundImage
        Button2.BackgroundImage = Button7.BackgroundImage
        Button7.BackgroundImage = gambar
        smt = Button2.Text
        Button2.Text = Button7.Text
        Button7.Text = smt

        gambar = Button2.BackgroundImage
        Button2.BackgroundImage = Button6.BackgroundImage
        Button6.BackgroundImage = gambar
        smt = Button2.Text
        Button2.Text = Button6.Text
        Button6.Text = smt

        gambar = Button3.BackgroundImage
        Button3.BackgroundImage = Button8.BackgroundImage
        Button8.BackgroundImage = gambar
        smt = Button3.Text
        Button3.Text = Button8.Text
        Button8.Text = smt

        gambar = Button3.BackgroundImage
        Button3.BackgroundImage = Button1.BackgroundImage
        Button1.BackgroundImage = gambar
        smt = Button3.Text
        Button3.Text = Button1.Text
        Button1.Text = smt

        gambar = Button5.BackgroundImage
        Button5.BackgroundImage = Button8.BackgroundImage
        Button8.BackgroundImage = gambar
        smt = Button5.Text
        Button5.Text = Button8.Text
        Button8.Text = smt

        gambar = Button5.BackgroundImage
        Button5.BackgroundImage = Button7.BackgroundImage
        Button7.BackgroundImage = gambar
        smt = Button5.Text
        Button5.Text = Button7.Text
        Button7.Text = smt

        gambar = Button6.BackgroundImage
        Button6.BackgroundImage = Button1.BackgroundImage
        Button1.BackgroundImage = gambar
        smt = Button6.Text
        Button6.Text = Button1.Text
        Button1.Text = smt
    End Sub

Untuk mengacak gambar dengan acak dengan bantuan timer

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If Button1.Text = "" Then
            gambar = Button4.BackgroundImage
            Button4.BackgroundImage = Button1.BackgroundImage
            Button1.BackgroundImage = gambar
            smt = Button4.Text
            Button4.Text = Button1.Text
            Button1.Text = smt
        ElseIf Button5.Text = "" Then
            gambar = Button4.BackgroundImage
            Button4.BackgroundImage = Button5.BackgroundImage
            Button5.BackgroundImage = gambar
            smt = Button4.Text
            Button4.Text = Button5.Text
            Button5.Text = smt
        ElseIf Button7.Text = "" Then
            gambar = Button4.BackgroundImage
            Button4.BackgroundImage = Button7.BackgroundImage
            Button7.BackgroundImage = gambar
            smt = Button4.Text
            Button4.Text = Button7.Text
            Button7.Text = smt
        End If

Memberi perinth untuk memindahkan puzzle pada button4

        If Button1.Text = "1" And Button2.Text = "2" And Button3.Text = "3" And Button4.Text = "4" And Button5.Text = "5" And Button6.Text = "6" And Button7.Text = "7" And Button8.Text = "8" And Button9.Text = "" Then
            MsgBox("SELAMAT")
        End If
    End Sub

Memberi msgbox ketika kita bisa menyusun dengan benar dan urut bertuliskan “SELAMAT”

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If Button2.Text = "" Then
            gambar = Button5.BackgroundImage
            Button5.BackgroundImage = Button2.BackgroundImage
            Button2.BackgroundImage = gambar
            smt = Button5.Text
            Button5.Text = Button2.Text
            Button2.Text = smt
        ElseIf Button4.Text = "" Then
            gambar = Button5.BackgroundImage
            Button5.BackgroundImage = Button4.BackgroundImage
            Button4.BackgroundImage = gambar
            smt = Button5.Text
            Button5.Text = Button4.Text
            Button4.Text = smt
        ElseIf Button6.Text = "" Then
            gambar = Button5.BackgroundImage
            Button5.BackgroundImage = Button6.BackgroundImage
            Button6.BackgroundImage = gambar
            smt = Button5.Text
            Button5.Text = Button6.Text
            Button6.Text = smt
        ElseIf Button8.Text = "" Then
            gambar = Button5.BackgroundImage
            Button5.BackgroundImage = Button8.BackgroundImage
            Button8.BackgroundImage = gambar
            smt = Button5.Text
            Button5.Text = Button8.Text
            Button8.Text = smt
        End If

Memberi perinth untuk memindahkan puzzle pada button5

        If Button1.Text = "1" And Button2.Text = "2" And Button3.Text = "3" And Button4.Text = "4" And Button5.Text = "5" And Button6.Text = "6" And Button7.Text = "7" And Button8.Text = "8" And Button9.Text = "" Then
            MsgBox("SELAMAT")
        End If
    End Sub

Memberi msgbox ketika kita bisa menyusun dengan benar dan urut bertuliskan “SELAMAT”

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If Button3.Text = "" Then
            gambar = Button6.BackgroundImage
            Button6.BackgroundImage = Button3.BackgroundImage
            Button3.BackgroundImage = gambar
            smt = Button6.Text
            Button6.Text = Button3.Text
            Button3.Text = smt
        ElseIf Button5.Text = "" Then
            gambar = Button6.BackgroundImage
            Button6.BackgroundImage = Button5.BackgroundImage
            Button5.BackgroundImage = gambar
            smt = Button6.Text
            Button6.Text = Button5.Text
            Button5.Text = smt
        ElseIf Button9.Text = "" Then
            gambar = Button6.BackgroundImage
            Button6.BackgroundImage = Button9.BackgroundImage
            Button9.BackgroundImage = gambar
            smt = Button6.Text
            Button6.Text = Button9.Text
            Button9.Text = smt
        End If

Memberi perinth untuk memindahkan puzzle pada button6

        If Button1.Text = "1" And Button2.Text = "2" And Button3.Text = "3" And Button4.Text = "4" And Button5.Text = "5" And Button6.Text = "6" And Button7.Text = "7" And Button8.Text = "8" And Button9.Text = "" Then
            MsgBox("SELAMAT")
        End If
    End Sub

Memberi msgbox ketika kita bisa menyusun dengan benar dan urut

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        If Button4.Text = "" Then
            gambar = Button7.BackgroundImage
            Button7.BackgroundImage = Button4.BackgroundImage
            Button4.BackgroundImage = gambar
            smt = Button7.Text
            Button7.Text = Button4.Text
            Button4.Text = smt
        ElseIf Button8.Text = "" Then
            gambar = Button7.BackgroundImage
            Button7.BackgroundImage = Button8.BackgroundImage
            Button8.BackgroundImage = gambar
            smt = Button7.Text
            Button7.Text = Button8.Text
            Button8.Text = smt
        End If

Memberi perinth untuk memindahkan puzzle pada button7

        If Button1.Text = "1" And Button2.Text = "2" And Button3.Text = "3" And Button4.Text = "4" And Button5.Text = "5" And Button6.Text = "6" And Button7.Text = "7" And Button8.Text = "8" And Button9.Text = "" Then
            MsgBox("SELAMAT")
        End If
    End Sub

Memberi msgbox ketika kita bisa menyusun dengan benar dan urut bertuliskan “SELAMAT”


    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        If Button7.Text = "" Then
            gambar = Button8.BackgroundImage
            Button8.BackgroundImage = Button7.BackgroundImage
            Button7.BackgroundImage = gambar
            smt = Button8.Text
            Button8.Text = Button7.Text
            Button7.Text = smt
        ElseIf Button5.Text = "" Then
            gambar = Button8.BackgroundImage
            Button8.BackgroundImage = Button5.BackgroundImage
            Button5.BackgroundImage = gambar
            smt = Button8.Text
            Button8.Text = Button5.Text
            Button5.Text = smt
        ElseIf Button9.Text = "" Then
            gambar = Button8.BackgroundImage
            Button8.BackgroundImage = Button9.BackgroundImage
            Button9.BackgroundImage = gambar
            smt = Button8.Text
            Button8.Text = Button9.Text
            Button9.Text = smt
        End If

Memberi perinth untuk memindahkan puzzle pada button8

        If Button1.Text = "1" And Button2.Text = "2" And Button3.Text = "3" And Button4.Text = "4" And Button5.Text = "5" And Button6.Text = "6" And Button7.Text = "7" And Button8.Text = "8" And Button9.Text = "" Then
            MsgBox("SELAMAT")
        End If
    End Sub

Memberi msgbox ketika kita bisa menyusun dengan benar dan urut bertuliskan “SELAMAT”


    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        If Button6.Text = "" Then
            gambar = Button9.BackgroundImage
            Button9.BackgroundImage = Button6.BackgroundImage
            Button6.BackgroundImage = gambar
            smt = Button9.Text
            Button9.Text = Button6.Text
            Button6.Text = smt
        ElseIf Button8.Text = "" Then
            gambar = Button9.BackgroundImage
            Button9.BackgroundImage = Button8.BackgroundImage
            Button8.BackgroundImage = gambar
            smt = Button9.Text
            Button9.Text = Button8.Text
            Button8.Text = smt
        End If

Memberi perinth untuk memindahkan puzzle pada button9

        If Button1.Text = "1" And Button2.Text = "2" And Button3.Text = "3" And Button4.Text = "4" And Button5.Text = "5" And Button6.Text = "6" And Button7.Text = "7" And Button8.Text = "8" And Button9.Text = "" Then
            MsgBox("SELAMAT")
        End If
    End Sub

Memberi msgbox ketika kita bisa menyusun dengan benar dan urut bertuliskan “SELAMAT”


    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Close()
    End Sub

Mengeluarkan dari permaian game tersebut(exit)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CenterToScreen()
    End Sub

Menampilkan form1 dengan posisi berada ditengah screen
   
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Button10.Enabled = True
        Button11.Enabled = True
        Button1.Text = "1"
        Button2.Text = "2"
        Button3.Text = "3"
        Button4.Text = "4"
        Button5.Text = "5"
        Button6.Text = "6"
        Button7.Text = "7"
        Button8.Text = "8"
        Button9.Text = ""
        Timer2.Enabled = False
    End Sub

Memberi perintah untuk memulai ulang dan puzzle tertata rapi seperti semula

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        timeLeft = startTime.Subtract(Now)
        If Not timeLeft.Milliseconds < 0 Then
            Label4.Text = Format(timeleft.Minutes, "#0") & " Mins" & "  " & Format(timeleft.Seconds, "00") & " Secs"

Untuk memberi waktu pada label 4 sampai habis hitugan mundur

        Else
            Timer2.Enabled = False
            If MsgBox("Wanna Try Again?", vbQuestion + vbYesNo, "Game Over!") = vbYes Then
                Button10.Enabled = True
                Button11.Enabled = True
                Button1.Text = "1"
                Button2.Text = "2"
                Button3.Text = "3"
                Button4.Text = "4"
                Button5.Text = "5"
                Button6.Text = "6"
                Button7.Text = "7"
                Button8.Text = "8"
                Button9.Text = ""
                Timer2.Enabled = False
                Label4.Text = ""
                TextBox1.Text = ""
            Else
                Close()
            End If
        End If

Ketika timer habis akan keluar msgbox , ketika kita memilih yes maka puzzle akan tersusu rapi lagi seperti awal mula main game ini

    End Sub

    Private Sub TextBox1_keypress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim a As Boolean = Char.IsDigit(e.KeyChar)
        If Not a Then e.Handled = True
    End Sub

End Class

Memberi perintah supaya di textbox1 hanya bisa diisi dengan angka.