Membuat Game Suwit dengan Visual Basic 6.0 (VB6)

Pada artikel kali ini Admin akan berbagi sebuah source code permainan Game Suwit, apa itu permainan Game Suwit.

Game Suwit adalah permainan klasik Batu-Gunting-Kertas di mana pemain melawan komputer.

Untuk lebih jelas bagaimana bentuk dari permainan Game Suwit,  perhatikan gambar dibawah ini :

Suwit Game

Berikut adalah langkah-langkah cara membuat Game Suwit dengan vb6 :

1. Buka Form vb6 Anda pilih Standar EXE

2. Tanamkan 2 Control PictureBox, 3 CommandButton dan beberapa Label

3. Desainlah Form seperti gambar dibawah ini :

Desain Form

4. Setelah selesai mendesain Form seperti gambar diatas simpan Form1, langkah selanjutnya adalah mendownload ketiga gambar dibawah ini dibawah ini :





5. Setelah ketiga gambar diatas di download, ubah gambar tersebut dengan file extension .BMP dengan aplikasi olah gambar seperti Paint atau Photoshop dll, misal batu.BMP, kertas.BMP dan gunting.BMP

6. Simpan ketiga Gambar diatas di satu folder yang sama dengan file Form1

7. Kemudian ketik kode dibawah ini :

Option Explicit

Dim playerChoice As Integer
Dim computerChoice As Integer
Dim playerScore As Integer
Dim computerScore As Integer

Private Function GetComputerChoice() As Integer
    Randomize
    GetComputerChoice = Int(Rnd * 3) + 1 ' 1 = Batu, 2 = Gunting, 3 = Kertas
End Function

Private Function DetermineWinner(player As Integer, computer As Integer) As String
    If player = computer Then
        DetermineWinner = "Seri!"
    ElseIf (player = 1 And computer = 2) Or _
           (player = 2 And computer = 3) Or _
           (player = 3 And computer = 1) Then
        DetermineWinner = "Kamu Menang!"
        playerScore = playerScore + 1
    Else
        DetermineWinner = "Komputer Menang!"
        computerScore = computerScore + 1
    End If
End Function

Private Sub ShowChoice(pic As PictureBox, choice As Integer)
    Select Case choice
        Case 1
            pic.Picture = LoadPicture("D:\VB6.0\FILEvb6\Suwit Game\batu.bmp") ' Ganti dengan path gambar Batu
            
        Case 2
            pic.Picture = LoadPicture("D:\VB6.0\FILEvb6\Suwit Game\gunting.bmp") ' Ganti dengan path gambar Gunting
             
        Case 3
            pic.Picture = LoadPicture("D:\VB6.0\FILEvb6\Suwit Game\kertas.bmp") ' Ganti dengan path gambar Kertas
    End Select
End Sub

Private Sub Command1_Click()
    PlayGame 1
End Sub

Private Sub Command2_Click()
    PlayGame 2
End Sub

Private Sub Command3_Click()
    PlayGame 3
End Sub

Private Sub PlayGame(choice As Integer)
    playerChoice = choice
    computerChoice = GetComputerChoice()
    
    ' Tampilkan pilihan
    ShowChoice Picture1, playerChoice
    ShowChoice Picture2, computerChoice

    ' Tentukan pemenang
    Label3.Caption = DetermineWinner(playerChoice, computerChoice)

    ' Update skor
    Label4.Caption = "User " & playerScore & " - " & computerScore & " Komputer"
End Sub

Private Sub Form_Load()
Label3.Caption = ""
Label4.Caption = ""
End Sub

8. Setelah mengetikan kode diatas coba jalankan Project Anda.

Catatan :
kode yang berwarna merah sesuaikan dengan alamat penyimpaman file gambar Anda

Cara Bermain :
Pilih tombol Anda inginkan (Gunting, Batu atau Kertas), ketika pilihan Anda di klik maka akan di tampilkan di kolom user dan kolom komputer akan terisi otomatis
Misal Anda memilih Gunting kemudian di kolom kumputer muncul Kertas, maka Anda menang karena gunting bisa memotong kertas.

Demikian artikel singkat pada kali ini semoga bermanfaat.

No comments: