'-1石头
'0剪刀
'1布
Private Sub Command1_Click()
Dim a, b As Integer
Randomize
a = -1
b = Int(Rnd * (-3) + 2)
Label1.Caption = "石头"
Select Case a - b
Case -2: Label2.Caption = "布": MsgBox "电脑胜"
Case 0: Label2.Caption = "石头": MsgBox "平"
Case -1: Label2.Caption = "剪刀": MsgBox "玩家胜"
End Select
End Sub
Private Sub Command2_Click()
Dim a, b As Integer
Randomize
a = 0
b = Int(Rnd * (-3) + 2)
Label1.Caption = "剪刀"
Select Case a - b
Case 1: Label2.Caption = "布": MsgBox "玩家胜"
Case 0: Label2.Caption = "剪刀": MsgBox "平"
Case -1: Label2.Caption = "石头": MsgBox "电脑胜"
End Select
End Sub
Private Sub Command3_Click()
Dim a, b As Integer
Randomize
a = 1
b = Int(Rnd * (-3) + 2)
Label1.Caption = "布"
Select Case a - b
Case 2: Label2.Caption = "石头": MsgBox "玩家胜"
Case 0: Label2.Caption = "布": MsgBox "平"
Case 1: Label2.Caption = "剪刀": MsgBox "电脑胜"
End Select
End Sub
Private Sub Form_Load()
Command1.Caption = "石头"
Command2.Caption = "剪刀"
Command3.Caption = "布"
End Sub
这是一个很简单的游戏。大致流程如下:在点击下picture的时候,生成一个1-3随机数(也就是电脑出的),可以把石头、剪刀、布分别看作0、1、2然后用if来进行判断,最后记下胜负。代码如下:
请在程序目录下放上三个jpg图片名为“剪刀”、“石头”、“布”picture123的图片请自己在属性列表中设置,pictue4 有电脑加载
Option Explicit'全局变量声明
Dim YouChoose'记录你的选择
Dim ComputerChoose'记录电脑选择
'0 石头 1剪刀 2布
Private Sub Picture1_Click()'选了石头
YouChoose = 0'记录下石头
Randomize'随机数生成函数
ComputerChoose = Int(Rnd() * 3)'电脑选择,并记录下载
display'显示电脑的选择(这是个用户函数)
ToGo'判断胜负(这是个用户函数)
End Sub
Private Sub Picture2_Click()'基本同上,选择剪刀
Randomize
YouChoose = 1
ComputerChoose = Int(Rnd() * 3)
display
ToGo
End Sub
Private Sub Picture3_Click()'基本同上,选择石头
Randomize
YouChoose = 2
ComputerChoose = Int(Rnd() * 3)
display
ToGo
End Sub
Sub ToGo()'判断胜负函数
If YouChoose = ComputerChoose Then'一样就平
MsgBox "平"
ElseIf (YouChoose = 0 And ComputerChoose = 1) Or (YouChoose = 1 And ComputerChoose = 2) Or (YouChoose = 2 And ComputerChoose = 0) Then'出现三种获胜可能之一就判定获胜
MsgBox "你赢"
Else'其他则输
MsgBox "电脑赢"
End If
End Sub
Sub display()'显示电脑的选择
On Error Resume Next'碰到错误则继续
If ComputerChoose = 0 Then'如果电脑选石头
Picture4.Picture = LoadPicture(App.Path + "石头.jpg")'加载石头图片
MsgBox "石头"'对话框d出石头
ElseIf ComputerChoose = 1 Then'同上
Picture4.Picture = LoadPicture(App.Path + "剪刀.jpg")
MsgBox "剪刀"
ElseIf ComputerChoose = 2 Then
Picture4.Picture = LoadPicture(App.Path + "布.jpg")
MsgBox "布"
End If
End Sub
Sub Start()Dim i As Integer
Dim bot As Integer
Dim tmpStr As String
Dim tmpstr2 As String
Randomize
go1:
i = Int(InputBox("请选择石头(输入1),剪刀(输入2),布(输入3),结束(输入0)", "参数"))
If i = 0 Then End
If i = 1 Then
tmpstr2 = "石头"
ElseIf i = 2 Then
tmpstr2 = "剪刀"
ElseIf i = 3 Then
tmpstr2 = "布"
Else
MsgBox "错误": GoTo go1
End If
bot = Int(Rnd * 3 + 1)
Select Case bot
Case 1
If i = 3 Then tmpStr = "你赢了!"
If i = 1 Then tmpStr = "平局!"
If i = 2 Then tmpStr = "你输了!"
MsgBox "你出的是" &tmpstr2 &",计算机出的是石头," &tmpStr
Case 2
If i = 1 Then tmpStr = "你赢了!"
If i = 2 Then tmpStr = "平局!"
If i = 3 Then tmpStr = "你输了!"
MsgBox "你出的是" &tmpstr2 &",计算机出的是剪刀," &tmpStr
Case 3
If i = 2 Then tmpStr = "你赢了!"
If i = 3 Then tmpStr = "平局!"
If i = 1 Then tmpStr = "你输了!"
MsgBox "你出的是" &tmpstr2 &",计算机出的是布," &tmpStr
End Select
GoTo go1
End Sub
执行Start过程就可以了!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)