SoFunction
Updated on 2025-04-10

vbs homemade digital puzzle game implementation code

When watching VB development, I suddenly had a whim when I saw a two-dimensional array using a two-dimensional array as the game level;

1. Select the operation: At the beginning of the game, you will prompt you to select three options: registration, login, and log out.

2. Registration: After successful registration, the user account and password will be stored in the user data txt document. If the empty value is entered three times in a row, it will directly exit the game;

3. Login: When logging in, the input data will be compared with the user data document content. If the user exists, the login will be successful. If the same fails three times, the game will be logged out;

4. Start the game: After logging in successfully, you will enter the game directly. The game is tentatively set to three levels, each level has three subsections. The game passes the rules to subscribe to two-dimensional arrays with levels as two-dimensional arrays (subscripts), and subsections are subscribed as one-dimensional arrays (subscripts + 4)... There are specific implementations in the file, and it is not too long-winded;

5. When the user logs in, each game will be written to the game log document game _log.txt to record the user, operation, progress of the clearance, time of clearance, etc.

ps: After downloading the file, double-click to run it. The game's user document game_user.txt and log document game_log.txt were created according to my desktop path. If you don't like it, you can directly modify it to d:\game\game_user.txt, etc. The game doesn't care much fun. I hope it can help children's shoes that want to learn vbs.

Complete code

Dim game(3,2),i,j,result,num 'Define a two-dimensional array 2-dimensional length 3, 1-dimensional length 2
 Dim fso ,ws,f ,logFileWrite,logFileRead, fileStr,flag,flagFailNum, flagIndex ' Define log files
  set ws = CreateObject("")
  Set fso = CreateObject("") 
  If ("C:\Users\18190\Desktop\vbs\test\game_log.txt") Then 
    Else 
    Set f = ("C:\Users\18190\Desktop\vbs\test\game_log.txt",true)
      'If ("C:\Users\18190\Desktop\vbs\test\game_log.txt") Then
       ' Set logFileWrite = ("C:\Users\18190\Desktop\vbs\test\game_log.txt",8,true)
       '     "Number Guess Guess 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 an 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 "If you make three consecutive operation errors, the system will exit directly..."
  Exit do
 end if 
 cnum = InputBox( "Welcome to the number guessing game, please select the operation:"&chr(10)&" 1. Register 2. Log in 3. Log out","Guess the Numbers and Guess the 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: ","Guess the Numbers and Guess the Mini Game - Register")
  upwd = InputBox("Please enter the registration password: ","Guess the Numbers and Guess the Mini Game - Register")
  If uname <> "" Then 
    If upwd <> "" Then 
     regStr = uname&"#"&upwd
    Else 
     regStr =""
     MsgBox "The password you entered incorrectly,Please re-enter"
    End If
  Else
    regStr =""
     MsgBox "The password you entered incorrectly,Please re-enter"
  End If
 
 If regStr <>"" Then 
  If ("C:\Users\18190\Desktop\vbs\test\game_user.txt") Then 
      Set logFileWrite = ("C:\Users\18190\Desktop\vbs\test\game_user.txt",8,true)
      t= Year(now)&"-"&month(now)&"-"&day(now)&" "& Hour(now)&":"&minute(now)&":"&second(now)
      ' 1
       regStr &"   "&t
      
      MsgBox "Registered successfully!"
      exit do
    else 
    Set f = ("C:\Users\18190\Desktop\vbs\test\game_user.txt",true)
      If ("C:\Users\18190\Desktop\vbs\test\game_user.txt") Then
       Set logFileWrite = ("C:\Users\18190\Desktop\vbs\test\game_user.txt",8,true)
           "Number Guess Guess Game-User Data"
           regStr
           
          MsgBox "Registered successfully!"
          exit do
     end if 
  End If 
 end if   
 loop

 ' 1000 

case 2
 ' Account login
Dim str 
  Set logFileRead = ("C:\Users\18190\Desktop\vbs\test\game_user.txt",1)
  fileStr = 
  
  str = InputBox("Please enter your account name and password, and use # keys separated: "," Number Guess Guess 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 and the system exits"
      5000 ' Just quit like this -- Lackwscript Object
     flagFailNum =0
    end if 
      MsgBox "There is any errors in your account and password, please check it and try again..."
     str = InputBox("Please enter your account name and password, and use # keys separated: "," Number Guess Guess 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 and the system exits"
      5000 ' Just quit like this -- Lackwscript Object
     flagFailNum =0
    end if 
      MsgBox "There is any errors in your account and password, please check it and try again..."
     str = InputBox("Please enter your account name and password, and use # keys separated: "," Number Guess Guess Game-Login")     if str <> "" then 
     flagIndex = InStr(fileStr,str)
     end if 
     loop
     flag = 1
  end if 


' Verification by login
if flag =1 then 
  Set logFileWrite = ("C:\Users\18190\Desktop\vbs\test\game_log.txt",8,true)
      t= Year(now)&"-"&month(now)&"-"&day(now)&" "& Hour(now)&":"&minute(now)&":"&second(now)
      ' 1
       "User Login: "&str &"   "&t

  MsgBox "Congratulations on logging in successfully. Guess the number game! Next, let's get to the main topic of the game: "
  num =0
  for i=0 to 2
    For j=0 To 1
     result = InputBox("Now is the game number"&(i+1)&"Guan Di"&(j+1)&"Section"&chr(10)&" Please enter the game results(i*3+j+4): "&chr(10)&"Exit Press-5","Puzzle game - guess number: ")
     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, you have successfully passed the first time"&(i+1)&"Guan Di"&(j+1)&"Secret, keep cheering..."   
     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 and failed to pass the level!"&chr(10)&"The game progress is final"&(i+1)&"Guan Di"&(j+1)&"Secret, work hard next time..."
      exit Do
    End if
      MsgBox "Sorry, you guessed it wrong! Please check and try again..."
       result = InputBox("Now is the game number"&(i+1)&"Guan Di"&(j+1)&"Section, Please enter the game results(i*3+j+4): ","Answer: ")
     If result ="" Then
     result = "-1"
     end if 
     If cint(result) = i*3+j+4 then
      MsgBox "Congratulations, you have successfully passed the first time"&(i+1)&"Guan Di"&(j+1)&"Secret, keep cheering..."  
      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 Warriors, congratulations on successfully completing this game! Momoda..."
     MsgBox msg
     t= Year(now)&"-"&month(now)&"-"&day(now)&" "& Hour(now)&":"&minute(now)&":"&second(now)
      "User: "&str&"Game Progress: "&msg &"   "&t
     sucFlag =1
    exit for
    End If
     msg = "Congratulations on successfully passing the grade"&(i+1)&"Level, next step"&(i+2)&"Love...good luck!"
    MsgBox msg
     t= Year(now)&"-"&month(now)&"-"&day(now)&" "& Hour(now)&":"&minute(now)&":"&second(now)
      "User: "&str&"Game Progress: "&msg &"   "&t

  Next 

end if

Case 3
 MsgBox "The system is about to exit..."
 exit do
case Else 
  MsgBox "Your choice is wrong, please re-select..."
 failNum = failNum +1
End select

loop
  

This is all about this article. I hope everyone will support me in the future.