Excelのこととか色々

Excel のこととか楽天とか いろいろ書いてみます・・・

【Excel/VBA】Janken game 02

 

    Janken icon

  

 

  _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

※Please correct my English if you find any mistakes. Thank you.

 

    form Janke game

 

Double click each part and write the following codes.

Macros

Command button: Close

Command button: Close

Private Sub cmdClose_Click()
    End
End Sub

--------------------------------------------------

Command button: Fight

Command button: Fight

Private Sub cmdFight_Click()

    'Define variables for assigning numbers to "You"
    Dim PYouRock As Long
    Dim PYouPeper As Long
    Dim PYouScissors As Long

    'Define variables for assign numbers to "Comp"
    Dim PCompRock As Long
    Dim PCompPeper As Long
    Dim PCompScissors As Long

    'Define variables for converting the selection to numbers
    Dim PYou As Long
    Dim PComp As Long


    'Converting the selection to numbers
    Select Case imgYou.Picture
        Case imgRock.Picture
            PYou = 1
        Case imgScissors.Picture
            PYou = 2
        Case imgPaper.Picture
            PYou = 3
    End Select

    'Formula for generating numbers for Comp's selection
    PComp = Int(3 * Rnd() + 1)


    Select Case PComp 'Assing 1 to Rock, 2 to Scissors, 3 to Paper1 and show each images on the main place
        Case 1
            imgComp.Picture = imgRock.Picture
        Case 2
            imgComp.Picture = imgScissors.Picture
        Case 3
            imgComp.Picture = imgPaper.Picture
    End Select


    'Judge winner and update win-loss standings
    Select Case PYou
        Case Is = 1 '"You" is Rock
            If PComp = 1 Then 'Comp is Rock
                lblDraw.Caption = lblDraw.Caption + 1
            ElseIf PComp = 2 Then 'Comp is Scissors
                lblWin.Caption = lblWin.Caption + 1
            Else 'Comp is Paper
                lblLose.Caption = lblLose.Caption + 1
            End If

    Case Is = 2 '"You" is Scissors
        If PComp = 1 Then 'Comp is Rock
            lblLose.Caption = lblLose.Caption + 1
        ElseIf PComp = 2 Then 'Comp is Scissors
            lblDraw.Caption = lblDraw.Caption + 1
        Else 'Comp is Paper
            lblWin.Caption = lblWin.Caption + 1
        End If

    Case Is = 3 '"You" is Paper
        If PComp = 1 Then 'Comp is Rock
            lblWin.Caption = lblWin.Caption + 1
        ElseIf PComp = 2 Then 'Comp is Scissors
            lblLose.Caption = lblLose.Caption + 1
        Else 'Comp is Paper
            lblDraw.Caption = lblDraw.Caption + 1
        End If
    End Select

End Sub

--------------------------------------------------

Command button: Reset

Command button: Reset

Private Sub cmdReset_Click()

    lblWin.Caption = 0
    lblDraw.Caption = 0
    lblLose.Caption = 0

End Sub

--------------------------------------------------

Option button: Paper

Option button: Paper

Private Sub opPaper_Click()

    If opPaper = True Then
        imgYou.Picture = imgPaper.Picture

    End If
End Sub

--------------------------------------------------

Option button: Rock

Option button: Rock

Private Sub opRock_Click()

    If opRock = True Then
        imgYou.Picture = imgRock.Picture

    End If
End Sub

--------------------------------------------------

Option button: Scissors

Option button: Scissors

Private Sub opScissors_Click()

    If opScissors = True Then
        imgYou.Picture = imgScissors.Picture

    End If
End Sub

--------------------------------------------------

Private Sub UserForm_Initialize()

    lblWin.Caption = 0
    lblDraw.Caption = 0
    lblLose.Caption = 0

End Sub

 

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/