在看vb开发,看到二维数组时突发奇想用二维数组作为游戏关卡;
1. 选择操作: 游戏开始时会提示选择注册, 登录, 退出三种选项,
2.注册: 注册成功会将用户账户与密码存入用户数据txt文档中,连续三次输入 空值会直接退出游戏;
3. 登录: 登录时会将输入数据与用户数据文档内容进行比对, 存在该用户则登 录成功, 同样失败三次后退出游戏;
4. 开始游戏: 登录成功后,会直接进入游戏,游戏暂定为三个关卡, 每个关卡三 个小节,游戏通过规则为以关卡作为二维数组的二维数组下标(下标),小 节作为 一位数组下标(下标+4)...文件里有具体实现,不啰嗦额;
5. 用户登录,通关每关游戏都会写入游戏日志文档game _log.txt ,记录用户,操 作,通关进度, 通关时间等
ps: 文件下载后,双击即可运行, 游戏的用户文档game_user.txt和日志文档game_log.txt是按照我桌面路径创建的,如果不喜欢可以直接修改为d:\game\game_user.txt等等,游戏不在乎多好玩, 希望能给想学习vbs的童鞋一些帮助
完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
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\测试\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 if Case 3 MsgBox "系统即将退出..." exit do case Else MsgBox "您的选择有误,请重新选择..." failNum = failNum +1 End select loop |
这篇文章就介绍到这了,希望大家以后多多支持服务器之家。