vbs⾃制数字益智游戏实现代码
在看vb开发,看到⼆维数组时突发奇想⽤⼆维数组作为游戏关卡;
1. 选择操作: 游戏开始时会提⽰选择注册, 登录, 退出三种选项,
2.注册: 注册成功会将⽤户账户与密码存⼊⽤户数据txt⽂档中,连续三次输⼊空值会直接退出游戏;
3. 登录: 登录时会将输⼊数据与⽤户数据⽂档内容进⾏⽐对, 存在该⽤户则登录成功, 同样失败三次后退出游戏;
4. 开始游戏: 登录成功后,会直接进⼊游戏,游戏暂定为三个关卡, 每个关卡三个⼩节,游戏通过规则为以关卡作为⼆维数组的⼆维数组下标(下标),⼩节作为⼀位数组下标(下标+4)...⽂件⾥有具体实现,不啰嗦额;
5. ⽤户登录,通关每关游戏都会写⼊游戏⽇志⽂档game _ ,记录⽤户,操作,通关进度, 通关时间等
ps: ⽂件下载后,双击即可运⾏, 游戏的⽤户⽂档和⽇志⽂档是按照我桌⾯路径创建的,如果不喜欢可以直接修改为d:\game\等等,游戏不在乎多好玩, 希望能给想学习vbs的童鞋⼀些帮助
完整代码
Dim game(3,2),i,j,result,num '定义⼆维数组⼆维长度3, ⼀维长度2
Dim fso ,ws,f ,logFileWrite,logFileRead, fileStr,flag,flagFailNum, flagIndex ' 定义⽇志⽂件
set ws = CreateObject("wscript.shell")
Set fso = CreateObject("scripting.filesystemobject")
If fso.fileexists("C:\Users\18190\Desktop\vbs\测试\") Then
Else
Set f = atetextfile("C:\Users\18190\Desktop\vbs\测试\",true)
'If fso.fileexists("C:\Users\18190\Desktop\vbs\测试\") Then
' Set logFileWrite = fso.OpenTextFile("C:\Users\18190\Desktop\vbs\测试\",8,true)
'    logFileWrite.writeLine "数字猜猜猜⼩游戏-游戏⽇志"
' end if
End If
For i=0 To 2 ' 关卡赋值
For j=4 To 5
game(i,j-4)= i*3+j
Next
Next
'For i=0 To 2
' For j=0 To 1
'MsgBox "game("&i&","&j&"): "& game(i,j)
'Next
'Next
' 选择操作
Dim cnum, failNum, sucFlag,t
failNum =0
sucFlag =0
Do While 1=1
If sucFlag=1 Then
Exit Do
End if
If failNum =3 Then
MsgBox "您有连续三次操作失误,系统将直接退出..."
Exit do
end if
cnum = InputBox( "欢迎来到数字猜猜猜⼩游戏请选择操作:"&chr(10)&" 1.注册  2.登录  3.退出","数字猜猜猜⼩游戏")
If cnum ="" Then
cnum = "-1"
End if
Select Case cnum
Case 1
' 账户注册
Dim juname, upwd,regStr
regStr = ""
do while regStr = ""
uname = InputBox("请输⼊注册账号:  ","数字猜猜猜⼩游戏-注册")
upwd = InputBox("请输⼊注册密码:  ","数字猜猜猜⼩游戏-注册")
If uname <> "" Then
If upwd <> "" Then
regStr = uname&"#"&upwd
Else
regStr =""
MsgBox "您输⼊的注册账号密码有误,请重新输⼊"
End If
Else
regStr =""
MsgBox "您输⼊的注册账号密码有误,请重新输⼊"
End If
If regStr <>"" Then
If fso.fileexists("C:\Users\18190\Desktop\vbs\测试\") Then
Set logFileWrite = fso.OpenTextFile("C:\Users\18190\Desktop\vbs\测试\",8,true)      t= Year(now)&"-"&month(now)&"-"&day(now)&" "& Hour(now)&":"&minute(now)&":"&second(now)      'logFileWrite.WriteBlankLines 1
logFileWrite.writeLine regStr &"  "&t
logFileWrite.close
MsgBox "注册成功!"
exit do
else
Set f = atetextfile("C:\Users\18190\Desktop\vbs\测试\",true)
If fso.fileexists("C:\Users\18190\Desktop\vbs\测试\") Then
Set logFileWrite = fso.OpenTextFile("C:\Users\18190\Desktop\vbs\测试\",8,true)          logFileWrite.writeLine "数字猜猜猜⼩游戏-⽤户数据"
logFileWrite.writeLine regStr
logFileWrite.close
MsgBox "注册成功!"
exit do
end if
End If
end if
loop
'wscript.sleep 1000
case 2
' 账户登录
Dim str
Set logFileRead = fso.OpenTextFile("C:\Users\18190\Desktop\vbs\测试\",1)
fileStr = logFileRead.ReadAll
logFileRead.close
str = InputBox("请输⼊您的账户名与密码,中间以#键隔开: "," 数字猜猜猜⼩游戏-登录")
if str <> "" then
flagIndex = InStr(fileStr,str)
If flagIndex>0 Then
flag =1
Else
flag = 0
Do While flagIndex =0
flagFailNum = flagFailNum +1
If flagFailNum = 3 Then
MsgBox "抱歉, 您已连续失败3次,系统退出"
wscript.sleep 5000 ' 就这样退出吧 -- 缺少wscript 对象
flagFailNum =0
end if
MsgBox "您的账户与密码输⼊有误,请查证后再试试...."
str = InputBox("请输⼊您的账户名与密码,中间以#键隔开: "," 数字猜猜猜⼩游戏-登录")
if str <> "" then
flagIndex = InStr(fileStr,str)
end if
loop
flag = 1
End If
Else
flag = 0
Do While flagIndex =0
vbs表白代码教程flagFailNum = flagFailNum +1
If flagFailNum = 3 Then
MsgBox "抱歉, 您已连续失败3次,系统退出"
wscript.sleep 5000 ' 就这样退出吧 -- 缺少wscript 对象
flagFailNum =0
end if
MsgBox "您的账户与密码输⼊有误,请查证后再试试...."
str = InputBox("请输⼊您的账户名与密码,中间以#键隔开: "," 数字猜猜猜⼩游戏-登录")
if str <> "" then
flagIndex = InStr(fileStr,str)
end if
loop
flag = 1
end if
' 通过登录验证
if flag =1 then
Set logFileWrite = fso.OpenTextFile("C:\Users\18190\Desktop\vbs\测试\",8,true)
t= Year(now)&"-"&month(now)&"-"&day(now)&" "& Hour(now)&":"&minute(now)&":"&second(now)
'logFileWrite.WriteBlankLines 1
logFileWrite.writeLine "⽤户登录: "&str &"  "&t
MsgBox "恭喜您成功登录数字猜猜猜⼩游戏! 接下来进⼊游戏正题:  "
num =0
for i=0 to 2
For j=0 To 1
result = InputBox("现在是游戏第"&(i+1)&"关第"&(j+1)&"⼩节"&chr(10)&" 请输⼊游戏结果(i*3+j+4): "&chr(10)&"退出按-5","益智⼩游戏-数字猜猜猜: ")    If result ="-5" Then
exit for
end if
If result ="" Then
result = "-1"
end if
If cint(result) = i*3+j+4 then
MsgBox "恭喜您,成功通过第"&(i+1)&"关第"&(j+1)&"⼩节,继续加油哦..."
End if
Do While  cint(result) <> i*3+j+4
num =num+1
If num =3 Then
MsgBox "抱歉, 您已连续失败3次,通关失败!"&chr(10)&" 游戏进度截⽌为第"&(i+1)&"关第"&(j+1)&"⼩节,下次努⼒哦..."
exit Do
End if
MsgBox "抱歉,您猜错了!请查证后再试...."
result = InputBox("现在是游戏第"&(i+1)&"关第"&(j+1)&"⼩节, 请输⼊游戏结果(i*3+j+4): ","答案: ")
If result ="" Then
result = "-1"
end if
If cint(result) = i*3+j+4 then
MsgBox "恭喜您,成功通过第"&(i+1)&"关第"&(j+1)&"⼩节,继续加油哦..."
Exit do
End if
loop
If num =3 Then
exit for
end if
Next
If result ="-5" Then
exit for
end if
If num =3 Then
exit for
End if
If i=2 Then
msg = "聪明的勇⼠,恭喜您成功通关本游戏!么么哒..."
MsgBox msg
t= Year(now)&"-"&month(now)&"-"&day(now)&" "& Hour(now)&":"&minute(now)&":"&second(now)
logFileWrite.writeLine "⽤户: "&str&"游戏进度: "&msg &"  "&t
sucFlag =1
exit for
End If
msg = "恭喜您成功通关第"&(i+1)&"关卡, 接下来进⼊第"&(i+2)&"关...祝您好运!"
MsgBox msg
t= Year(now)&"-"&month(now)&"-"&day(now)&" "& Hour(now)&":"&minute(now)&":"&second(now)
logFileWrite.writeLine "⽤户: "&str&"游戏进度: "&msg &"  "&t
Next
end if
Case 3
MsgBox "系统即将退出..."
exit do
case Else
MsgBox "您的选择有误,请重新选择..."
failNum = failNum +1
End select
loop
这篇⽂章就介绍到这了,希望⼤家以后多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。