首页 > 编程 > VBScript > 正文

vbs 自制数字益智游戏实现代码

2020-07-26 11:20:16
字体:
来源:转载
供稿:网友

在看vb开发,看到二维数组时突发奇想用二维数组作为游戏关卡;

1. 选择操作: 游戏开始时会提示选择注册, 登录, 退出三种选项,

2.注册: 注册成功会将用户账户与密码存入用户数据txt文档中,连续三次输入 空值会直接退出游戏;

3. 登录: 登录时会将输入数据与用户数据文档内容进行比对, 存在该用户则登 录成功, 同样失败三次后退出游戏;

4. 开始游戏: 登录成功后,会直接进入游戏,游戏暂定为三个关卡, 每个关卡三 个小节,游戏通过规则为以关卡作为二维数组的二维数组下标(下标),小 节作为 一位数组下标(下标+4)...文件里有具体实现,不嗦额;

5. 用户登录,通关每关游戏都会写入游戏日志文档game _log.txt ,记录用户,操 作,通关进度, 通关时间等

ps: 文件下载后,双击即可运行, 游戏的用户文档game_user.txt和日志文档game_log.txt是按照我桌面路径创建的,如果不喜欢可以直接修改为d:/game/game_user.txt等等,游戏不在乎多好玩, 希望能给想学习vbs的童鞋一些帮助

完整代码

Dim game(3,2),i,j,result,num '定义二维数组 二维长度3, 一维长度2Dim 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/测试/game_log.txt") Then     Else     Set f = fso.createtextfile("C:/Users/18190/Desktop/vbs/测试/game_log.txt",true)      'If fso.fileexists("C:/Users/18190/Desktop/vbs/测试/game_log.txt") Then       ' Set logFileWrite = fso.OpenTextFile("C:/Users/18190/Desktop/vbs/测试/game_log.txt",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/测试/game_user.txt") Then       Set logFileWrite = fso.OpenTextFile("C:/Users/18190/Desktop/vbs/测试/game_user.txt",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 = fso.createtextfile("C:/Users/18190/Desktop/vbs/测试/game_user.txt",true)      If fso.fileexists("C:/Users/18190/Desktop/vbs/测试/game_user.txt") Then       Set logFileWrite = fso.OpenTextFile("C:/Users/18190/Desktop/vbs/测试/game_user.txt",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/测试/game_user.txt",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      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/测试/game_log.txt",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 ifCase 3 MsgBox "系统即将退出..." exit docase Else   MsgBox "您的选择有误,请重新选择..." failNum = failNum +1End selectloop  

这篇文章就介绍到这了,希望大家以后多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表