用vbs格式编写打地鼠小游戏
时间: 2023-12-07 08:04:45 浏览: 159
以下是用 VBS 编写的打地鼠小游戏代码:
```
' 打地鼠小游戏
' 作者:AI助手
Option Explicit
' 游戏区域大小
Const GAME_WIDTH = 10
Const GAME_HEIGHT = 10
' 地鼠数量
Const MOLE_COUNT = 10
' 游戏区域
Dim Game(GAME_WIDTH, GAME_HEIGHT)
' 地鼠位置
Dim Moles(MOLE_COUNT, 2)
' 分数
Dim Score
' 初始化游戏
Sub InitGame()
' 清空游戏区域
Dim i, j
For i = 0 To GAME_WIDTH
For j = 0 To GAME_HEIGHT
Game(i, j) = 0
Next
Next
' 随机生成地鼠位置
Dim k
For k = 0 To MOLE_COUNT - 1
Dim x, y
Do
x = Int(Rnd() * GAME_WIDTH)
y = Int(Rnd() * GAME_HEIGHT)
Loop While Game(x, y) = 1
Game(x, y) = 1
Moles(k, 0) = x
Moles(k, 1) = y
Next
' 初始化分数
Score = 0
End Sub
' 显示游戏界面
Sub ShowGame()
Dim i, j
For j = 0 To GAME_HEIGHT
For i = 0 To GAME_WIDTH
If Game(i, j) = 1 Then
' 地鼠
WScript.StdOut.Write("[O]")
Else
' 地洞
WScript.StdOut.Write("[ ]")
End If
Next
WScript.StdOut.WriteLine()
Next
WScript.StdOut.WriteLine("Score: " & Score)
End Sub
' 玩家点击游戏区域
Sub OnClick(x, y)
If Game(x, y) = 1 Then
' 击中地鼠
Game(x, y) = 0
Score = Score + 10
Else
' 没有击中地鼠
Score = Score - 5
End If
End Sub
' 游戏主循环
Sub MainLoop()
Do While True
' 显示游戏界面
ShowGame()
' 等待玩家点击游戏区域
WScript.StdOut.Write("Please click (x y): ")
Dim input
input = InputBox("请输入坐标,格式如:1 2", "打地鼠小游戏")
If input = "" Then
Exit Do
End If
Dim arr
arr = Split(input, " ")
Dim x, y
x = CInt(arr(0))
y = CInt(arr(1))
OnClick(x, y)
' 判断游戏是否结束
Dim k, found
found = False
For k = 0 To MOLE_COUNT - 1
If Game(Moles(k, 0), Moles(k, 1)) = 1 Then
found = True
Exit For
End If
Next
If Not found Then
WScript.StdOut.WriteLine("Game over! Your score is " & Score)
Exit Do
End If
' 清空屏幕
WScript.Sleep 1000
WScript.StdOut.WriteLine()
WScript.StdOut.WriteLine()
WScript.StdOut.WriteLine()
Loop
End Sub
' 启动游戏
Randomize
InitGame()
MainLoop
```
保存为 `.vbs` 格式,双击运行即可。注意:此代码需要在 Windows 环境下运行。
阅读全文