大家好,今天要和大家来做一个小游戏的示例。
我们先来看一下运行效果
 

没错,今天我们要做一个猜拳小游戏。
那下去我们来看一下怎么来制作这个小游戏
01、创建窗体
参照下面的表格与图表,在窗体上添加对的控件,可以适当的调整一下
| 控件 | 控件名称 | 属性 | 
| 标签 |   | 标题改成“你出:” | 
| 标签 |   | 标题改成“电脑出:” | 
| 标签 | lblUser |   | 
| 标签 | lblComputer |   | 
| 标签 | lblResult | 标题改成“结果” | 
| 按钮 | btnST | 标题改成“石头” | 
| 按钮 | btnJD | 标题改成“剪刀” | 
| 按钮 | btnB | 标题改成“布” | 

02、添加代码
这次我换个另类的方法,我们来写几个类,通过调用类来实现我们需要的功能。
第一个类,用户类,用于用户出拳
Option Compare Database
Option Explicit
Private strFistName As String
Public Property Get FistName() As String
    FistName = strFistName
End Property
Public Function ShowFist(fist As String) As String
    strFistName = fist
    Select Case fist
    Case "石头"
        ShowFist = 1
    Case "剪刀"
        ShowFist = 2
    Case "布"
        ShowFist = 3
    End Select
End Function第二个类,电脑类,用于电脑出拳
Option Compare Database
Option Explicit
Private strFistName As String
Public Property Get FistName() As String
    FistName = strFistName
End Property
Public Function ShowFist() As Integer
    Dim r As Integer
    Randomize
    r = Int((3 * Rnd) + 1)
    Select Case r
    Case 1
        strFistName = "石头"
    Case 2
        strFistName = "剪刀"
    Case 3
        strFistName = "布"
    End Select
    ShowFist = r
End Function第三个类,裁判类,用于对出拳的结果进行判断
Option Compare Database
Option Explicit
Public Function IsUserWin(user As Integer, computer As Integer) As String
    If user - computer = 0 Then
        IsUserWin = "平局~~~"
    ElseIf user - computer = -1 Or user - computer = 2 Then
         IsUserWin = "你居然赢了…"
    Else
        IsUserWin = "你真菜,你输了!!!"
    End If
End Function窗体中的代码
Private Sub btnB_Click()
btn Me.btnB.Caption
End Sub
Private Sub btnJD_Click()
btn Me.btnJD.Caption
End Sub
Private Sub btnST_Click()
btn Me.btnST.Caption
End Sub
Private Function btn(strCaption As String)
    Dim ul As New UserPlay
    Dim userFist As Integer
    Dim pc As New ComputerUser
    Dim computerFist As Integer
    userFist = ul.ShowFist(strCaption)
    Me.lblUser.Caption = ul.FistName
    computerFist = pc.ShowFist()
    Me.lblComputer.Caption = pc.FistName
    Dim cp As New CaiPan
    Me.lblResult.Caption = cp.IsUserWin(userFist, computerFist)
End Function
Private Sub Form_Load()
    Me.lblUser.Caption = ""
    Me.lblComputer.Caption = ""
    Me.lblResult.Caption = ""
End Sub03、运行测试
最后一步就是来运行测试一下
知识点
这里主要的知识点是关于类操作,在面向对象的开发中,自定义类是很常规的操作,关于类的一些特点,像封装、继承、多态大家可以去了解一下,这里我们就不太深入的讲解了。










