加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

写了一个在EXCEL中运行的推箱子游戏

(2008-06-05 16:37:58)
标签:

杂谈

分类: EXCEL真奇妙
老婆的手机上有一个推箱子游戏,今天玩的时候突然感觉屏幕的图案有点象EXCEL的单元格啊。所以就以手机上的游戏为基础,写了一个在EXCEL里玩的推箱子游戏。
 
 写了一个在EXCEL中运行的推箱子游戏

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.EditDirectlyInCell = True
End Sub

Private Sub Workbook_Open()
    Sheets("sheet1").ScrollArea = "D3:I8"
    Application.EditDirectlyInCell = False
    Call base
End Sub

 

Public weizhi As String

Sub pailie()
    Application.EnableEvents = False
    Dim tu$
    Dim OperRg As Range
    Sheets("sheet1").Select
    Set OperRg = Sheets("sheet1").Range("D3:I8")
    OperRg.Clear
    OperRg.HorizontalAlignment = xlCenter
    OperRg.VerticalAlignment = xlCenter
    OperRg.Font.Name = "Wingdings"
    OperRg.Font.Size = 28
    tu = Sheets("mission").Cells(Sheets("sheet1").Range("M6"), 1)
    With OperRg
        For i = 1 To 36
            If Mid(tu, i, 1) = "0" Then .Cells(i) = ""
            If Mid(tu, i, 1) = "1" Then
                .Cells(i) = "w"
                .Cells(i).Interior.ColorIndex = 5
                .Cells(i).Font.ColorIndex = 5
            End If
            If Mid(tu, i, 1) = "2" Then .Cells(i) = "O"
            If Mid(tu, i, 1) = "3" Then .Cells(i) = "x"
            If Mid(tu, i, 1) = "4" Then
                .Cells(i) = "J"
                weizhi = .Cells(i).Address
                .Cells(i).Select
            End If
        Next i
        If Len(tu) = 38 Then .Cells(CInt(Right(tu, 2))) = "O" & .Cells(CInt(Right(tu, 2)))
        If Len(tu) = 40 Then
            .Cells(CInt(Mid(tu, 37, 2))) = "O" & .Cells(CInt(Mid(tu, 37, 2)))
            .Cells(CInt(Mid(tu, 39, 2))) = "O" & .Cells(CInt(Mid(tu, 39, 2)))
        End If
        For i = 1 To 36
            .Cells(i).Font.Size = IIf(Len(.Cells(i)) = 2, 18, 28)
            .Cells(i).Font.ColorIndex = IIf(.Cells(i) = "Ox", 3, IIf(.Cells(i) = "w", 5, 0))
        Next i
    End With
    Application.EnableEvents = True
End Sub

Sub base()
    Dim user
    Dim stage%, i%
    Dim lastrow%
    Sheets("sheet1").Select
    user = Application.InputBox("请输入用户名:" & vbCrLf & "如果点取消,将用默认的用户名<aaa>来登录", "用户登录", "aaa")
    If user = False Then user = "aaa"
    With Sheets("user")
        lastrow = .Range("A65536").End(xlUp).Row
        For i = 1 To lastrow
            If .Cells(i, 1) = user Then
                stage = .Cells(i, 2)
                Exit For
            End If
        Next i
        If stage = 0 Then
            .Cells(lastrow + 1, 1) = user
            .Cells(lastrow + 1, 2) = 1
            stage = 1
        End If
    End With
    Sheets("sheet1").Range("M5").Value = user
    Sheets("sheet1").Range("M6").Value = stage
    Call pailie
End Sub


'版权所有: www.excelhome.net 井底之蛙

Private Sub CommandButton1_Click()
    Call pailie
End Sub

Private Sub CommandButton2_Click()
    ThisWorkbook.Close savechanges:=True
End Sub

Private Sub CommandButton3_Click()
    Call base
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim renrow%, rencol%, xiangrow%, xiangcol%, rowref%, colref%
    Dim i%, good%
    renrow = Range(weizhi).Row
    rencol = Range(weizhi).Column
    xiangrow = Target.Row
    xiangcol = Target.Column
    With Sheets("sheet1")
        If Abs(renrow - xiangrow) + Abs(rencol - xiangcol) > 1 Then
            .Range(weizhi).Select
            Exit Sub
        End If
        Select Case Target
        Case ""  '空
            .Range(weizhi) = IIf(.Range(weizhi) = "OJ", "O", "")
            weizhi = Target.Address
            Target = "J"
        Case "w"  '墙
            .Range(weizhi).Select
        Case "x", "Ox"    '箱子,目标和箱子
            If renrow = xiangrow Then    '横向移动
                If rencol > xiangcol Then rowref = 0: colref = -1    '向左推
                If rencol < xiangcol Then rowref = 0: colref = 1   '向右推
            End If
            If rencol = xiangcol Then    '纵向移动
                If renrow > xiangrow Then rowref = -1: colref = 0    '向上推
                If renrow < xiangrow Then rowref = 1: colref = 0   '向下推
            End If
            Select Case Target.Offset(rowref, colref)
            Case ""  '空
                Target.Offset(rowref, colref) = "x"
                Target = IIf(Target = "Ox", "OJ", "J")
                .Range(weizhi) = IIf(.Range(weizhi) = "OJ", "O", "")
                weizhi = Target.Address
            Case "w", "x", "Ox"    '墙,箱子,目标和箱子
                .Range(weizhi).Select
            Case "O"  '目标
                Target.Offset(rowref, colref) = "Ox"
                Target = IIf(Target = "Ox", "OJ", "J")
                .Range(weizhi) = IIf(.Range(weizhi) = "OJ", "O", "")
                weizhi = Target.Address
            End Select
        Case "O"  '目标
            Target = "OJ"
            .Range(weizhi) = IIf(.Range(weizhi) = "OJ", "O", "")
            weizhi = Target.Address
        End Select
    End With
    With Sheets("sheet1").Range("D3:I8")
        good = 0
        For i = 1 To 36
            .Cells(i).Font.Size = IIf(Len(.Cells(i)) = 2, 18, 28)
            If .Cells(i) = "Ox" Then good = good + 1
            .Cells(i).Font.ColorIndex = IIf(.Cells(i) = "Ox", 3, IIf(.Cells(i) = "w", 5, 0))
        Next i
    End With
    If good = 3 Then
        MsgBox "恭喜你,过关了", vbOKOnly + vbInformation, "信息"
        Application.EnableEvents = False
        If Sheets("sheet1").Range("M6").Value < 88 Then
            Sheets("sheet1").Range("M6").Value = Sheets("sheet1").Range("M6").Value + 1
        Else
            MsgBox "恭喜你已经过了所有88关,现在只能重新再温习一次了。", vbOKOnly + vbInformation, "恭喜你"
            Sheets("sheet1").Range("M6").Value = 1
        End If
        With Sheets("user")
            For i = 1 To .Range("A65536").End(xlUp).Row
                If .Cells(i, 1) = Sheets("sheet1").Range("M5").Value Then
                    .Cells(i, 2) = Sheets("sheet1").Range("M6").Value
                    ThisWorkbook.Save
                    Exit For
                End If
            Next i
        End With
        Application.EnableEvents = True
        Call pailie
    End If
End Sub

 

以下为关卡的代码
000120200300131100001131004000021000
000100020320131100001131004020001000
000100000300131100021131024000021000
000100020320131100001131024000001000
000100002300131100021131004200001000
00010000030013110002113102400000100014
101102003000433000010010011110200002
00100000103000102110300003002040110011
201111200011013000203310100000111104
14011100032000100000013010300011102023
222100030030031000001001001041111111
002220010010011010010010003330000014
10001101230102400103030110001111111115
101102000030400332210010011110000000
101102030330400000010010011110200002
000000033012010000100102101032400000
004000011121000321103120130100000000
111001103001030100430100101202120000
100011100000101232020100030301111004
10400001101001103000032001321000000122
111111100001121130022300001300004011
1111111002011410311003011013011000011728
11001110000102023100340011131011100028
1111111021301001001433001100011100011122
11002010301010230010013111040111111109
100240101210100030123301100111111111
11104100330100000110211113011110211109
121112101002103304100300100110100111
111041111000201330000001200311120011
022111030000001310040230111111111111
11002103010103300102140100001100011114
111111200421003101010320000310111000
202111213301000401031001100011111111
20011101000102010103330114011110011121
111111200020010100430032111103111100
00111100111102300013000010131112041115
104011102000203230131011100011111111
10001103101102041103000110130112001120
111111100111223002013030400100111100
10024110003211131010000000300100011116
140111100111120111033202003011110011
11111112001110101110330010032010041121
111111110001110121110021403301002301
00041103101103030000011012002011000114
100041100100120030103312111002111111
111111112011020011000330102034111111
14200103330100100100100211101011100010
111100111132110020110012030030000140
002111032201001131100100130004100111
110011110011110220003100002330111040
1100110103410030001130001100021111111015
11020003412000310100300110010111000121
11111111111110011100000100133400203235
140000201000033321021001001111111111
00000001011301403023020011110011110012
140001113000110200030131020021111001
000111003001133021140201110101110021
000111033000021200001101001131040021
111111111111020011021011043300032000
042100023020001300003001110011110011
100020030034232111001111001111001111
02001100141100301113100100010020300133
11111111102003402003010001003000011114
111111111111110011022032003304111000
111111111140111102030303000200110200
000100012303000300111120110004110002
111401110022103131000320001000000011
000411033111030200101210200010000000
11100111103104320001100000011000030234
111040111000103031103001200101220001
111111111204002300010011030302111000
111111002104010300032130102000100111
11111110000012110213003410013010010020
201111031111001111023040023010110000
000000013000030412131200100002111111
002011000011013320200110430110110000
01001000000012023100340000131001000028
110000112000003110203000001031000241
000001410000110230033101020021111001
10000110000112113003200000103004001120
102241100032111310100000030001000111
001111002140010300002130102030100111
001000031000201100023040023010110000
11002010301010230010013101040011100009
021112001002003304100301100111100111
04001100103003200012113000010100000014
20001100300111310110430110020111100015
00110002000021101004330013000110001122

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有