基于 VB6的猜拳游戏

基于 VB6的猜拳游戏

1 欢迎页的制作

  • welcomeFrom

    在这里插入图片描述
    在这里插入图片描述
    添加一个定时器
    在这里插入图片描述

代码如下:

	Private Sub Form_Load()
    '定时器Timer1的时间间隔设置为1000毫秒
    Timer1.Interval = 1000
    Timer1.Enabled = True
	End Sub
	Private Sub Timer1_Timer()
	    '关闭当前窗体
	    Unload Me
	    ReadyFrom.Show
	End Sub

2 准备页的制作

  • 放Image数组 组件在这里插入图片描述
    在这里插入图片描述

  • 下面再放一个Image,以便拖动选择头像
    在这里插入图片描述

  • 如下
    在这里插入图片描述

  • 设置图片拖动图标属性DragIcon

  • 拖动头像代码如下

Public intIndex As Integer ' 当前图片索引
Public intFlag As Integer ' 标记变量,用于判断是否选择了图片
' 图片控件的鼠标按下事件
Private Sub Image1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    Image1(Index).Drag vbBeginDrag ' 开始拖动图片
    intIndex = Index ' 记录当前图片的索引
End Sub
' 图片控件的放下事件
Private Sub Image2_DragDrop(Source As Control, X As Single, Y As Single)
    Image2.Picture = Image1(intIndex).Picture ' 将拖动的图片设置为目标图片
    intFlag = 1 ' 设置标记变量为1,表示已选择了图片
End Sub
  • 加入一个按钮和文本框
    在这里插入图片描述
  • 代码如下
Private Sub nameEdit_KeyDown(KeyCode As Integer, Shift As Integer)
    ' 按下Enter键时触发事件
    If KeyCode = 13 Then
        Call playGame
    End If
End Sub


Private Sub playBtn_Click()
    Call playGame
End Sub

Private Sub playGame()
    ' 游戏开始逻辑
    If nameEdit.Text = "" Then
        MsgBox "お名前をご入力ください!" ' 提示输入用户名
    ElseIf intFlag = 0 Then
        MsgBox "アバターを選択してください" ' 提示选择一张图片
    Else
        strUserName = nameEdit.Text ' 获取用户输入的用户名
        ' 显示游戏主界面并关闭当前窗体
        gameFrom.Show
        Unload Me
    End If
End Sub

3 游戏页的制作

  • 页面图
    在这里插入图片描述
1 显示选择的头像和用户名

在这里插入图片描述
代码如下:

Dim strMyName As String ' 玩家名称
Private Sub Form_Load()
    Call pageInit
    ' 初始化界面控件状态
    '历史聊天框
    historyEdit.Locked = True
    '连接IP按钮
    connectBtn.Enabled = False
    '发送信息按钮
    sendBtn.Enabled = False
    '发送信息文本区
    textEdit.Enabled = False
    '选择石头剪刀按钮
    myChoiceBtn.Enabled = False

End Sub

' 页面初始化函数
Private Sub pageInit()
    If ReadyFrom.intIndex = 0 Then
        strImgPath = "\img\img0.jpg"
    ElseIf ReadyFrom.intIndex = 1 Then
        strImgPath = "\img\img1.jpg"
    ElseIf ReadyFrom.intIndex = 2 Then
        strImgPath = "\img\img2.jpeg"
    ElseIf ReadyFrom.intIndex = 3 Then
        strImgPath = "\img\img3.jpg"
    ElseIf ReadyFrom.intIndex = 4 Then
        strImgPath = "\img\img4.jpg"
    End If
    
    ' 加载图片到界面
    myImg.Picture = LoadPicture(App.Path & strImgPath)
    ' 设置玩家名称
    myNameLable.Caption = ReadyFrom.strUserName
    strMyName = ReadyFrom.strUserName
End Sub
2 石头剪刀布显示在我的选择框中

在这里插入图片描述代码如下:

Dim strChoosedImgPath As String ' 选择的图片路径
Dim boolImgFlag As Boolean ' 是否可以选择图片
Dim boolMePunches As Boolean ' 我的出拳情况

'单机imgGame组件事件
Private Sub imgGame_Click(Index As Integer)
    If boolImgFlag Or boolMePunches = False Then
        ' 显示对手选择的默认图片
        FriChoiceImg.Picture = LoadPicture(App.Path & "\img\img5.jpg")

        If Index = 0 Then
            strChoosed = 1 ' 石头
            strChoosedImgPath = "\img\shi.jpg"
        ElseIf Index = 1 Then
            strChoosed = 2 ' 剪刀
            strChoosedImgPath = "\img\jian.jpg"
        ElseIf Index = 2 Then
            strChoosed = 3 ' 布
            strChoosedImgPath = "\img\bu.jpg"
        End If
        
        ' 启用确认按钮
        myChoiceBtn.Enabled = True
        MyChoiceImg.Picture = LoadPicture(App.Path & strChoosedImgPath)
    Else
        MsgBox "当前不能选择手势或已经出拳,请等待对手出拳完成后再操作"
    End If
End Sub
3 ip是否正确地址的检验,及利用Winsock进行udp连接作成简单聊天
  • 引入Winsock,并设置protocol
    在这里插入图片描述
    在这里插入图片描述

  • 检验文本框ip是否正确
    在这里插入图片描述

Dim strIpAddress As String ' IP地址
'校验IP地址
Private Sub friendIpEdit_Change()
    connectBtn.Enabled = False  ' 禁用连接按钮

    ' 获取用户输入的IP地址
    strIpAddress = friendIpEdit.Text

    ' 检查IP地址是否合法
    If IsValidstrIpAddress(strIpAddress) Then
        connectBtn.Enabled = True  ' 若IP地址合法,则启用连接按钮
    End If
End Sub
' 校验IP地址是否合法的函数
Public Function IsValidstrIpAddress(ByVal strIpAddress As String) As Boolean
    ' 声明变量
    Dim parts() As String  ' 用于存储IP地址各部分的数组
    Dim i As Integer  ' 循环计数器
    Dim temp As Integer  ' 临时存储转换后的IP地址部分值

    ' 使用"."分割IP地址字符串并存入数组parts
    parts = Split(strIpAddress, ".")

    ' 判断IP地址部分数量是否为4
    If UBound(parts) <> 3 Then
        IsValidstrIpAddress = False  ' 返回False
        Exit Function  ' 退出函数
    End If

    ' 遍历IP地址的各个部分
    For i = LBound(parts) To UBound(parts)
        ' 判断是否为数字
        If Not IsNumeric(parts(i)) Then
            IsValidstrIpAddress = False  ' 返回False
            Exit Function  ' 退出函数
        End If

        ' 将IP地址部分转换为整数并判断是否在0~255范围内
        temp = CInt(parts(i))
        If temp < 0 Or temp > 255 Then
            IsValidstrIpAddress = False  ' 返回False
            Exit Function  ' 退出函数
        End If
    Next i

    ' 若通过上述检查,则IP地址合法,返回True
    IsValidstrIpAddress = True
End Function
  • udp的连接及聊天和状态显示
    在这里插入图片描述

代码如下

Dim strImgPath As String ' 图片路径
Dim strDataName As String ' 数据名称
Dim boolConnectFlag As Boolean ' 是否连接状态
Dim boolImgFlag As Boolean ' 是否可以选择图片
' 发起连接按钮点击事件
Private Sub Form_Load()
    Dim ipname As String
    Dim Bind() As String

    ' 获取本地IP地址
    ipname = Winsock.LocalIP
    ' 将IP地址按点分割成数组
    Bind = Split(ipname, ".")
    ' 绑定本地IP和端口号,端口号为1000 + IP地址的最后一位
    Winsock.Bind (1000 + Bind(3))
    
    boolImgFlag = True
    boolConnectFlag = True
End Sub
Private Sub connectBtn_Click()
    On Error Resume Next
    ' 设置远程主机IP和端口号
    With Winsock
        .RemoteHost = strIpAddress
        Dim RemotePort() As String
        RemotePort = Split(strIpAddress, ".")
        .RemotePort = (1000 + RemotePort(3))
    End With
    ' 发送初始化消息包括玩家名称和图片路径
    Winsock.SendData "#InitSend#" & strMyName & "#" & strImgPath
    If Err.Number <> 0 Then
         MsgBox (Err.Description)
         Err.Clear
    End If
End Sub

Private Sub sendBtn_Click()
    ' 检查文本编辑框是否有内容
    If textEdit.Text <> "" Then
        historyEdit.Text = historyEdit.Text & "---mine---" & vbCrLf  ' 在历史记录中添加发送消息标识
        historyEdit.Text = historyEdit.Text & textEdit.Text & vbCrLf  ' 在历史记录中添加发送的文本内容
      
        ' 发送消息包括玩家名称和文本内容
        Winsock.SendData "@MsgSend@" & strMyName & "@" & textEdit.Text  ' 发送消息数据
        
        textEdit.Text = ""  ' 清空文本编辑框内容

    Else
        MsgBox "请输入要发送的消息内容"  ' 如果文本编辑框内容为空,弹出提示框
    End If
End Sub

'接受数据处理
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String  ' 定义字符串变量用于存储接收的数据
    Dim strDatas() As String  ' 定义字符串数组用于存储分割后的数据
    Dim strDataTxt As String  ' 定义字符串变量用于存储文本数据
    Dim strDataImgPath As String  ' 定义字符串变量用于存储图片路径

    ' 接收数据
    Winsock.GetData strData

    If InStr(strData, "#InitSend#") Then
        ' 如果接收到初始化数据
        strDatas = Split(strData, "#")  ' 使用#符号分割数据
        strDataName = strDatas(2)  ' 获取对方名称
        strDataImgPath = strDatas(3)  ' 获取对方头像路径
        friendNameLable.Caption = strDataName  ' 设置对方名称标签显示内容
        Print (strDataImgPath)
        'friendImg.Picture = LoadPicture(App.Path & strDataImgPath)  ' 加载对方头像图片
        
        ' 如果是刚连接上,则回复初始化消息
        If boolConnectFlag Then
            Winsock.SendData "#InitSend#" & strMyName & "#" & strImgPath  ' 回复初始化消息
            boolConnectFlag = False  ' 将连接标志设为False
        End If
        
        sendBtn.Enabled = True  ' 启用发送按钮
        textEdit.Enabled = True  ' 启用文本编辑框
        
        
    ElseIf InStr(strData, "#MsgSend#") Then
        ' 如果接收到消息数据
        strDatas = Split(strData, "#")  ' 使用#符号分割数据
        strDataName = strDatas(2)  ' 获取对方名称
        strDataTxt = strDatas(3)  ' 获取消息内容
        
        historyEdit.Text = historyEdit.Text & "---" & strDataName & "---" & vbCrLf  ' 在历史记录中添加对方名称
        historyEdit.Text = historyEdit.Text & strDataTxt & vbCrLf  ' 在历史记录中添加消息内容
        historyEdit.SelStart = Len(historyEdit.Text)  ' 将光标定位到文本末尾
    End If
End Sub
  • 猜拳游戏的完成
    代码如下:
Dim strChoosed As String ' 选择的手势
Dim strMyName As String ' 玩家名称
Dim IntGameNumber As Integer ' 游戏局数
Dim strDataChoPath As String ' 数据路径
Dim strDataFriChoe As String ' 对手选择
Dim strGame As String ' 我和对手的选择

Dim boolFriPunches As Boolean ' 对手的出拳情况

Private Sub Form_Load()
   
    boolMePunches = False
    boolFriPunches = False
End Sub

Private Sub myChoiceBtn_Click()
    ' 禁用确认按钮
    myChoiceBtn.Enabled = False
    boolMePunches = True
    ' 发送游戏出拳消息包括玩家名称和选择的手势
    Winsock.SendData "@GameSend@" & strMyName & "@" & strChoosed & "@" & strChoosedImgPath
    historyEdit.Text = historyEdit.Text & "等待对手出拳..." & vbCrLf
    ' 如果双方都已出拳,则判断胜负
    If boolMePunches And boolFriPunches Then
        WinOrLose
    End If
    boolImgFlag = False
End Sub

Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String  ' 定义字符串变量用于存储接收的数据
    Dim strDatas() As String  ' 定义字符串数组用于存储分割后的数据
    Dim strDataTxt As String  ' 定义字符串变量用于存储文本数据
    Dim strDatastrImgPath As String  ' 定义字符串变量用于存储图片路径
    ' 接收数据
    Winsock.GetData strData
    If InStr(strData, "@InitSend@") Then
        ' 如果接收到初始化数据
        strDatas = Split(strData, "@")  ' 使用@符号分割数据
        strDataName = strDatas(2)  ' 获取对方名称
        strDatastrImgPath = strDatas(3)  ' 获取对方头像路径
        friendNameLable.Caption = strDataName  ' 设置对方名称标签显示内容
        friendImg.Picture = LoadPicture(App.Path & strDatastrImgPath)  ' 加载对方头像图片
        ' 如果是刚连接上,则回复初始化消息
        If boolConnectFlag Then
            Winsock.SendData "@InitSend@" & strMyName & "@" & strImgPath  ' 回复初始化消息
            boolConnectFlag = False  ' 将连接标志设为False
        End If
        
        sendBtn.Enabled = True  ' 启用发送按钮
        textEdit.Enabled = True  ' 启用文本编辑框
        
    ElseIf InStr(strData, "@GameSend@") Then
        ' 如果接收到游戏数据
        strDatas = Split(strData, "@")  ' 使用@符号分割数据
        strDataName = strDatas(2)  ' 获取对方名称
        strDataFriChoe = strDatas(3)  ' 获取对方选择
        strDataChoPath = strDatas(4)  ' 获取对方选择图片路径
        boolFriPunches = True  ' 对方已出拳标志设为True
        
        ' 如果双方都已出拳,则判断胜负
        If boolMePunches And boolFriPunches Then
            WinOrLose  ' 调用判断胜负函数
        End If   
    ElseIf InStr(strData, "@MsgSend@") Then
        ' 如果接收到消息数据
        strDatas = Split(strData, "@")  ' 使用@符号分割数据
        strDataName = strDatas(2)  ' 获取对方名称
        strDataTxt = strDatas(3)  ' 获取消息内容
        
        historyEdit.Text = historyEdit.Text & "---" & strDataName & "---" & vbCrLf  ' 在历史记录中添加对方名称
        historyEdit.Text = historyEdit.Text & strDataTxt & vbCrLf  ' 在历史记录中添加消息内容
        historyEdit.SelStart = Len(historyEdit.Text)  ' 将光标定位到文本末尾
        
    ElseIf InStr(strData, "@endSend@") Then
        ' 如果接收到结束连接数据
        MsgBox "对方已断开连接"  ' 弹出提示对话框
        friendImg.Picture = LoadPicture(App.Path & "\img\img5.jpg")  ' 加载默认对方头像
        friendNameLable.Caption = "???"  ' 设置对方名称为问号
        FriChoiceImg.Picture = LoadPicture(App.Path & "\img\img5.jpg")  ' 加载默认选择图片
        
        strDataChoPath = ""  ' 清空选择图片路径
        strDataFriChoe = ""  ' 清空对方选择
        strGame = ""  ' 清空游戏状态
        strDataName = ""  ' 清空对方名称
    End If
End Sub

Private Sub WinOrLose()
    ' 显示对手选择的图片
    FriChoiceImg.Picture = LoadPicture(App.Path & strDataChoPath)

    ' 判断胜负
    strGame = strChoosed & strDataFriChoe
    Select Case strGame
        Case "11"
            MsgBox "平局"
            DisResults ("0")
        Case "12"
            MsgBox "你赢了"
            DisResults (strMyName)
        Case "13"
            MsgBox "对方赢了"
            DisResults (strDataName)
        Case "21"
            MsgBox "对方赢了"
            DisResults (strDataName)
        Case "22"
            MsgBox "平局"
            DisResults ("0")
        Case "23"
            MsgBox "你赢了"
            DisResults (strMyName)
        Case "31"
            MsgBox "你赢了"
            DisResults (strMyName)
        Case "32"
            MsgBox "对方赢了"
            DisResults (strDataName)
        Case "33"
            MsgBox "平局"
            DisResults ("0")
    End Select
    
    boolImgFlag = True
    IntGameNumber = 0
    strChoosed = ""
    strDataFriChoe = ""
    strDataChoPath = ""
    strDataFriChoe = ""
    strGame = ""
    boolFriPunches = False
    boolMePunches = False
    boolImgFlag = True
End Sub

Private Sub DisResults(str As String)
    If str = "0" Then
        historyEdit.Text = historyEdit.Text & vbCrLf & "--**平局**--" & vbCrLf & vbCrLf
    Else
        historyEdit.Text = historyEdit.Text & vbCrLf & "结果--" & str & "--胜利" & vbCrLf & vbCrLf
    End If
    historyEdit.Text = historyEdit.Text & "------------------------" & vbCrLf & vbCrLf
    historyEdit.SelStart = Len(historyEdit.Text)
End Sub
  • 该页总代码
Dim strImgPath As String ' 图片路径
Dim strMyName As String ' 玩家名称
Dim strChoosedImgPath As String ' 选择的图片路径
Dim strIpAddress As String ' IP地址
Dim strDataName As String ' 数据名称
Dim boolConnectFlag As Boolean ' 是否连接状态

Dim strChoosed As String ' 选择的手势
Dim IntGameNumber As Integer ' 游戏局数
Dim strDataChoPath As String ' 数据路径
Dim strDataFriChoe As String ' 对手选择
Dim strGame As String ' 我和对手的选择

Dim boolFriPunches As Boolean ' 对手的出拳情况

Dim boolImgFlag As Boolean ' 是否可以选择图片
Dim boolMePunches As Boolean ' 我的出拳情况
Private Sub Form_Load()
    Call pageInit
    ' 初始化界面控件状态
    '历史聊天框
    historyEdit.Locked = True
    '连接IP按钮
    connectBtn.Enabled = False
    '发送信息按钮
    sendBtn.Enabled = False
    '发送信息文本区
    textEdit.Enabled = False
    '选择石头剪刀按钮
    myChoiceBtn.Enabled = False
    
    
    Dim ipname As String
    Dim Bind() As String

    ' 获取本地IP地址
    ipname = Winsock.LocalIP
    ' 将IP地址按点分割成数组
    Bind = Split(ipname, ".")
    ' 绑定本地IP和端口号,端口号为1000 + IP地址的最后一位
    Winsock.Bind (1000 + Bind(3))
    
    boolMePunches = False
    boolFriPunches = False
    boolImgFlag = True
    boolConnectFlag = True
End Sub






Private Sub myChoiceBtn_Click()
    ' 禁用确认按钮
    myChoiceBtn.Enabled = False

    boolMePunches = True
    ' 发送游戏出拳消息包括玩家名称和选择的手势
    Winsock.SendData "#GameSend#" & strMyName & "#" & strChoosed & "#" & strChoosedImgPath
    historyEdit.Text = historyEdit.Text & "等待对手出拳..." & vbCrLf

    ' 如果双方都已出拳,则判断胜负
    If boolMePunches And boolFriPunches Then
        WinOrLose
    End If

    boolImgFlag = False
End Sub








' 发起连接按钮点击事件
Private Sub connectBtn_Click()
    On Error Resume Next
    ' 设置远程主机IP和端口号
    With Winsock
        .RemoteHost = strIpAddress
        Dim RemotePort() As String
        RemotePort = Split(strIpAddress, ".")
        .RemotePort = (1000 + RemotePort(3))
    End With
    ' 发送初始化消息包括玩家名称和图片路径
    Winsock.SendData "#InitSend#" & strMyName & "#" & strImgPath
    If Err.Number <> 0 Then
         MsgBox (Err.Description)
         Err.Clear
    End If
End Sub



Private Sub sendBtn_Click()
    ' 检查文本编辑框是否有内容
    If textEdit.Text <> "" Then
        historyEdit.Text = historyEdit.Text & "---mine---" & vbCrLf  ' 在历史记录中添加发送消息标识
        historyEdit.Text = historyEdit.Text & textEdit.Text & vbCrLf  ' 在历史记录中添加发送的文本内容
      
        ' 发送消息包括玩家名称和文本内容
        Winsock.SendData "#MsgSend#" & strMyName & "#" & textEdit.Text  ' 发送消息数据
        
        textEdit.Text = ""  ' 清空文本编辑框内容

    Else
        MsgBox "请输入要发送的消息内容"  ' 如果文本编辑框内容为空,弹出提示框
    End If
End Sub

'接受数据处理
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String  ' 定义字符串变量用于存储接收的数据
    Dim strDatas() As String  ' 定义字符串数组用于存储分割后的数据
    Dim strDataTxt As String  ' 定义字符串变量用于存储文本数据
    Dim strDataImgPath As String  ' 定义字符串变量用于存储图片路径

    ' 接收数据
    Winsock.GetData strData

    If InStr(strData, "#InitSend#") Then
        ' 如果接收到初始化数据
        strDatas = Split(strData, "#")  ' 使用#符号分割数据
        strDataName = strDatas(2)  ' 获取对方名称
        strDataImgPath = strDatas(3)  ' 获取对方头像路径
        friendNameLable.Caption = strDataName  ' 设置对方名称标签显示内容
        friendImg.Picture = LoadPicture(App.Path & strDataImgPath)  ' 加载对方头像图片
        
        ' 如果是刚连接上,则回复初始化消息
        If boolConnectFlag Then
            Winsock.SendData "#InitSend#" & strMyName & "#" & strImgPath  ' 回复初始化消息
            boolConnectFlag = False  ' 将连接标志设为False
        End If
        
        sendBtn.Enabled = True  ' 启用发送按钮
        textEdit.Enabled = True  ' 启用文本编辑框
        
    ElseIf InStr(strData, "#GameSend#") Then
        ' 如果接收到游戏数据
        strDatas = Split(strData, "#")  ' 使用#符号分割数据
        strDataName = strDatas(2)  ' 获取对方名称
        strDataFriChoe = strDatas(3)  ' 获取对方选择
        strDataChoPath = strDatas(4)  ' 获取对方选择图片路径
        boolFriPunches = True  ' 对方已出拳标志设为True
        
        ' 如果双方都已出拳,则判断胜负
        If boolMePunches And boolFriPunches Then
            WinOrLose  ' 调用判断胜负函数
        End If
        
    ElseIf InStr(strData, "#MsgSend#") Then
        ' 如果接收到消息数据
        strDatas = Split(strData, "#")  ' 使用#符号分割数据
        strDataName = strDatas(2)  ' 获取对方名称
        strDataTxt = strDatas(3)  ' 获取消息内容
        
        historyEdit.Text = historyEdit.Text & "---" & strDataName & "---" & vbCrLf  ' 在历史记录中添加对方名称
        historyEdit.Text = historyEdit.Text & strDataTxt & vbCrLf  ' 在历史记录中添加消息内容
        historyEdit.SelStart = Len(historyEdit.Text)  ' 将光标定位到文本末尾
        
    ElseIf InStr(strData, "#endSend#") Then
        ' 如果接收到结束连接数据
        MsgBox "对方已断开连接"  ' 弹出提示对话框
        friendImg.Picture = LoadPicture(App.Path & "\img\img5.jpg")  ' 加载默认对方头像
        friendNameLable.Caption = "???"  ' 设置对方名称为问号
        FriChoiceImg.Picture = LoadPicture(App.Path & "\img\img5.jpg")  ' 加载默认选择图片
        
        strDataChoPath = ""  ' 清空选择图片路径
        strDataFriChoe = ""  ' 清空对方选择
        strGame = ""  ' 清空游戏状态
        strDataName = ""  ' 清空对方名称
    End If
End Sub
Private Sub WinOrLose()
    ' 显示对手选择的图片
    FriChoiceImg.Picture = LoadPicture(App.Path & strDataChoPath)

    ' 判断胜负
    strGame = strChoosed & strDataFriChoe
    Select Case strGame
        Case "11"
            MsgBox "平局"
            DisResults ("0")
        Case "12"
            MsgBox "你赢了"
            DisResults (strMyName)
        Case "13"
            MsgBox "对方赢了"
            DisResults (strDataName)
        Case "21"
            MsgBox "对方赢了"
            DisResults (strDataName)
        Case "22"
            MsgBox "平局"
            DisResults ("0")
        Case "23"
            MsgBox "你赢了"
            DisResults (strMyName)
        Case "31"
            MsgBox "你赢了"
            DisResults (strMyName)
        Case "32"
            MsgBox "对方赢了"
            DisResults (strDataName)
        Case "33"
            MsgBox "平局"
            DisResults ("0")
    End Select
    
    boolImgFlag = True
    IntGameNumber = 0
    strChoosed = ""
    strDataFriChoe = ""
    strDataChoPath = ""
    strDataFriChoe = ""
    strGame = ""
    boolFriPunches = False
    boolMePunches = False
    boolImgFlag = True
End Sub

Private Sub DisResults(str As String)
    If str = "0" Then
        historyEdit.Text = historyEdit.Text & vbCrLf & "--**平局**--" & vbCrLf & vbCrLf
    Else
        historyEdit.Text = historyEdit.Text & vbCrLf & "结果--" & str & "--胜利" & vbCrLf & vbCrLf
    End If
    historyEdit.Text = historyEdit.Text & "------------------------" & vbCrLf & vbCrLf
    
    historyEdit.SelStart = Len(historyEdit.Text)
End Sub

'单机imgGame组件事件
Private Sub imgGame_Click(Index As Integer)
    If boolImgFlag Or boolMePunches = False Then
        ' 显示对手选择的默认图片
        FriChoiceImg.Picture = LoadPicture(App.Path & "\img\img5.jpg")

        If Index = 0 Then
            strChoosed = 1 ' 石头
            strChoosedImgPath = "\img\shi.jpg"
        ElseIf Index = 1 Then
            strChoosed = 2 ' 剪刀
            strChoosedImgPath = "\img\jian.jpg"
        ElseIf Index = 2 Then
            strChoosed = 3 ' 布
            strChoosedImgPath = "\img\bu.jpg"
        End If
        
        ' 启用确认按钮
        myChoiceBtn.Enabled = True
        MyChoiceImg.Picture = LoadPicture(App.Path & strChoosedImgPath)
    Else
        MsgBox "当前不能选择手势或已经出拳,请等待对手出拳完成后再操作"
    End If
End Sub

'校验IP地址
Private Sub friendIpEdit_Change()
    connectBtn.Enabled = False  ' 禁用连接按钮

    ' 获取用户输入的IP地址
    strIpAddress = friendIpEdit.Text

    ' 检查IP地址是否合法
    If IsValidstrIpAddress(strIpAddress) Then
        connectBtn.Enabled = True  ' 若IP地址合法,则启用连接按钮
    End If
End Sub

' 校验IP地址是否合法的函数
Public Function IsValidstrIpAddress(ByVal strIpAddress As String) As Boolean
    ' 声明变量
    Dim parts() As String  ' 用于存储IP地址各部分的数组
    Dim i As Integer  ' 循环计数器
    Dim temp As Integer  ' 临时存储转换后的IP地址部分值

    ' 使用"."分割IP地址字符串并存入数组parts
    parts = Split(strIpAddress, ".")

    ' 判断IP地址部分数量是否为4
    If UBound(parts) <> 3 Then
        IsValidstrIpAddress = False  ' 返回False
        Exit Function  ' 退出函数
    End If

    ' 遍历IP地址的各个部分
    For i = LBound(parts) To UBound(parts)
        ' 判断是否为数字
        If Not IsNumeric(parts(i)) Then
            IsValidstrIpAddress = False  ' 返回False
            Exit Function  ' 退出函数
        End If

        ' 将IP地址部分转换为整数并判断是否在0~255范围内
        temp = CInt(parts(i))
        If temp < 0 Or temp > 255 Then
            IsValidstrIpAddress = False  ' 返回False
            Exit Function  ' 退出函数
        End If
    Next i

    ' 若通过上述检查,则IP地址合法,返回True
    IsValidstrIpAddress = True
End Function

' 页面初始化函数
Private Sub pageInit()
    If ReadyFrom.intIndex = 0 Then
        strImgPath = "\img\img0.jpg"
    ElseIf ReadyFrom.intIndex = 1 Then
        strImgPath = "\img\img1.jpg"
    ElseIf ReadyFrom.intIndex = 2 Then
        strImgPath = "\img\img2.jpeg"
    ElseIf ReadyFrom.intIndex = 3 Then
        strImgPath = "\img\img3.jpg"
    ElseIf ReadyFrom.intIndex = 4 Then
        strImgPath = "\img\img4.jpg"
    End If
    
    ' 加载图片到界面
    myImg.Picture = LoadPicture(App.Path & strImgPath)
    ' 设置玩家名称
    myNameLable.Caption = ReadyFrom.strUserName
    strMyName = ReadyFrom.strUserName
End Sub


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    ' 若仍处于连接状态则发送断开消息
    If strDataName <> "" Then
        Winsock.SendData "@endSend@"
    End If
    frmBye.Show
    Unload Me
    
End Sub




  • 页面4,结束页面

在这里插入图片描述
在这里插入图片描述

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    ' 若仍处于连接状态则发送断开消息
    If strDataName <> "" Then
        Winsock.SendData "@endSend@"
    End If
    Unload Me
	frmBye.Show
End Sub
  • frmBye
Private Sub Form_Load()
    Timer1.Interval = 1500
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Unload Me
    End
End Sub
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@活着笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值