EXCEL用53行代码制作一个打地鼠的小游戏,很有趣

2018-05-27 11:27  小嘟嘟熊

大家好,我是头条作者:小嘟嘟熊,今天向大伙分享我用EXCEL做的一个小作品(打地鼠),只用了53行代码,很简单的,首先我们打开EXCEL软件,行数从A2到A11,列数从A到J设置1个10*10的区域,把颜色填充为白色,宽度调整一下,画一个边框,这个区域就是刷新地鼠的位置,在A1单元格中写"GO"开始游戏格子(详情看图3),设置好区域后,在菜单栏上单击"工具","宏","Visual Basic 编辑器",在左边资源管理器中双击"Sheet1",在代码编辑框中编写代码:

Option Explicit

Private Const PosX = 1

Private Const PosY = 2

Dim Pre_theMouse As Integer

Dim Pre_PosY As Integer

Dim Cur_theMouse As Integer

Dim cur_PosY As Integer

Dim cut As Integer

Dim KillCut As Integer

Dim KillFalg As Boolean

Public Sub GenerateMouse(ByVal Target As Range)

Randomize

If Pre_theMouse > 0 Then

Target.Cells(Pre_PosY, Pre_theMouse).Interior.Color = vbWhite

Target.Cells(Pre_PosY, Pre_theMouse) = ""

End If

Cur_theMouse = Int(Rnd() * 10 + PosX)

cur_PosY = Int(Rnd() * 10 + PosY)

Target.Cells(cur_PosY, Cur_theMouse).Interior.Color = vbRed

Target.Cells(cur_PosY, Cur_theMouse) = "鼠"

Pre_theMouse = Cur_theMouse

Pre_PosY = cur_PosY

End Sub

EXCEL用53行代码制作一个打地鼠的小游戏,很有趣

图1

要不断的有地鼠刷新,就要一个定时器,我们写一个简单的定时器,来控制刷新地鼠

Private Sub sleep(ByVal value As Single) '定时器'

Dim tim As Long

tim = Timer + value

分享到微信朋友圈 ×
打开微信,点击底部的“发现”,
使用“扫一扫”即可将网页分享至朋友圈。
×

本网页已闲置过久,点击关闭或空白处,即可回到网页