VBS self-made digital puzzle game implementation code

Time:2019-11-8

In VB development, when we see two-dimensional array, we suddenly want to use two-dimensional array as the game level;

1. Select operation: at the beginning of the game, you will be prompted to select three options: registration, login and exit,

2. Registration: after successful registration, the user account and password will be saved in the user data TXT file, and the blank value will be entered three times in a row to directly exit the game;

3. Login: when logging in, the input data will be compared with the content of the user’s data document. If the user exists, the login succeeds. If the user fails three times, the game will exit;

4. Start the game: after successful login, you will enter the game directly. The game is tentatively divided into three levels, each level has three sections. The rules of the game are two-dimensional array subscript (subscript) with level as two-dimensional array, and section as one bit array subscript (subscript + 4)… The file has specific implementation, without verbose amount;

5. When the user logs in, the game log file game.txt will be written for each clearance game, recording the user, operation, clearance progress, clearance time, etc

PS: after downloading the file, double-click to run it. The game user document game.txt and log document game.txt are created according to my desktop path. If you don’t like it, you can directly modify it to D: \ game \ game_user.txt and so on. The game doesn’t care how much fun it is. I hope to give some help to children’s shoes who want to learn VBS

Complete code

Dim game (3,2), I, J, result, Num 'define two-dimensional array length 3, one-dimensional array length 2
Dim FSO, WS, F, logfilewrite, logfileread, filestr, flag, flagfailnum, flagindex 'define log file
  set ws = CreateObject("wscript.shell")
  Set fso = CreateObject("scripting.filesystemobject") 
  If fso.fileexists ("C: \ users \ 18190 \ desktop \ VBS \ test \ game \ log. TXT") then 
    Else 
    Set F = fso.createtextfile ("C: \ users \ 18190 \ desktop \ VBS \ test \ game \ log. TXT", true)
      'If fso.fileexists ("C: \ users \ 18190 \ desktop \ VBS \ test \ game \ log. TXT") then
       'set logfilewrite = fso.opentextfile ("C: \ users \ 18190 \ desktop \ VBS \ test \ game_log.txt", 8, true)
       'logfilewrite. Writeline "number guessing games - game log"
     ' end if 
   
  End If 

  For I = 0 to 2 'level assignment
   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

'Select Action
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 "you have three consecutive operation errors, the system will directly exit..."
  Exit do
 end if 
 Cnum = InputBox ("welcome to the digital guessing game, please select operation:" & Chr (10) & "1. Register 2. Log in 3. Exit", "digital guessing game")
  If cnum ="" Then
     cnum = "-1"
  End if
 Select Case cnum
 Case 1 
'account registration
Dim juname, upwd,regStr 
  regStr = ""
 do while regStr = ""
  Uname = InputBox ("please enter the registered account:", "number guessing game - register")
  Upwd = InputBox ("please enter the registration password:", "number guessing games - registration")
  If uname <> "" Then 
    If upwd <> "" Then 
     regStr = uname&"#"&upwd
    Else 
     regStr =""
     Msgbox "the password of your registered account is wrong, please re-enter"
    End If
  Else
    regStr =""
     Msgbox "the password of your registered account is wrong, please re-enter"
  End If
 
 If regStr <>"" Then 
  If fso.fileexists ("C: \ users \ 18190 \ desktop \ VBS \ test \ game \ user. TXT") then 
      Set logfilewrite = fso.opentextfile ("C: \ users \ 18190 \ desktop \ VBS \ test \ 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 "registration succeeded!"
      exit do
    else 
    Set F = fso.createtextfile ("C: \ users \ 18190 \ desktop \ VBS \ test \ game \ user. TXT", true)
      If fso.fileexists ("C: \ users \ 18190 \ desktop \ VBS \ test \ game \ user. TXT") then
       Set logfilewrite = fso.opentextfile ("C: \ users \ 18190 \ desktop \ VBS \ test \ game_user. TXT", 8, true)
          Logfilewrite.writeline "number guessing - user data"
          logFileWrite.writeLine regStr
           logFileWrite.close
          Msgbox "registration succeeded!"
          exit do
     end if 
  End If 
 end if   
 loop

 'wscript.sleep 1000 

case 2
 'account login
Dim str 
  Set logfileread = fso.opentextfile ("C: \ users \ 18190 \ desktop \ VBS \ test \ game \ user. TXT", 1)
  fileStr = logFileRead.ReadAll
  logFileRead.close
  STR = InputBox ("please enter your account name and password, separated by ා key:", "number guessing game - login")
  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 "sorry, you have failed 3 times in a row, the system exits"
     Wscript.sleep 5000 'just exit like this -- the Wscript object is missing
     flagFailNum =0
    end if 
      Msgbox "your account and password are wrong, please check and try again..."
     STR = InputBox ("please enter your account name and password, separated by ා key:", "number guessing game - login")
     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 "sorry, you have failed 3 times in a row, the system exits"
     Wscript.sleep 5000 'just exit like this -- the Wscript object is missing
     flagFailNum =0
    end if 
      Msgbox "your account and password are wrong, please check and try again..."
     STR = InputBox ("please enter your account name and password, separated by ා key:", "number guessing game - login")
     if str <> "" then 
     flagIndex = InStr(fileStr,str)
     end if 
     loop
     flag = 1
  end if 


'verify by login
if flag =1 then 
  Set logfilewrite = fso.opentextfile ("C: \ users \ 18190 \ desktop \ VBS \ test \ game_log.txt", 8, true)
      t= Year(now)&"-"&month(now)&"-"&day(now)&" "& Hour(now)&":"&minute(now)&":"&second(now)
      'logFileWrite.WriteBlankLines 1
      Logfilewrite.writeline "user login: & STR &" "& T

  Msgbox "congratulations on your success in the number guessing and guessing game! Next, enter the main topic of the game:"
  num =0
  for i=0 to 2
    For j=0 To 1
     Result = InputBox ("now is game number" & (I + 1) & "close number" & (j + 1) & "section" & Chr (10) & "please input game result (I * 3 + j + 4):" & Chr (10) & "exit press-5", "quiz game - number guessing:")
     If result ="-5" Then
     exit for
     end if 
     If result ="" Then
     result = "-1"
     end if 
     If cint(result) = i*3+j+4 then
      Msgbox "congratulations on your success in passing the" & (I + 1) & "close" & (j + 1) & "section. Keep going..."   
     End if
     Do While  cint(result) <> i*3+j+4 
      num =num+1
    If num =3 Then
      Msgbox "sorry, you have failed 3 times in a row, failed to pass the customs!" & Chr (10) & "the game progress is up to" & (I + 1) & "close" & (j + 1) & "section, next effort..."
      exit Do
    End if
      Msgbox "sorry, you guessed wrong! Please check and try again..."
       Result = InputBox ("now is the game & (I + 1) &" close "& (j + 1) &" section, please input the game result (I * 3 + j + 4): "," answer: ")
     If result ="" Then
     result = "-1"
     end if 
     If cint(result) = i*3+j+4 then
      Msgbox "congratulations on your success in passing the" & (I + 1) & "close" & (j + 1) & "section. Keep going..."  
      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 = "smart warrior, congratulations on your success in this game! MoMA Da..."
     MsgBox msg
     t= Year(now)&"-"&month(now)&"-"&day(now)&" "& Hour(now)&":"&minute(now)&":"&second(now)
     Logfilewrite.writeline "user: & STR &" game progress: & MSG & "" & T
     sucFlag =1
    exit for
    End If
     MSG = "congratulations on your success in passing the" & (I + 1) & "level, and then enter the" & (I + 2) & "level... Good luck!"
    MsgBox msg
     t= Year(now)&"-"&month(now)&"-"&day(now)&" "& Hour(now)&":"&minute(now)&":"&second(now)
     Logfilewrite.writeline "user: & STR &" game progress: & MSG & "" & T

  Next 

end if

Case 3
 Msgbox "system about to exit..."
 exit do
case Else 
  Msgbox "your choice is wrong, please choose again..."
 failNum = failNum +1
End select

loop

This article introduces this. I hope you can support developepaer in the future.

Recommended Today

Web API records the type of server address obtained by deploying IIS

There are many types of server address. The following records are available 1、HttpContext.Current.Server.MapPath(“~/File”) The returned value is D: \ 3project \ code \ mobileservice \ webapi \ file. Local service: this path is the root directory of the disk address where the project is located. Deployment server: the disk folder where the deployment file is […]