震惊!Excel也能玩游戏?

前言

前UC震惊部小喽啰闲着没事干用excel写个文字小游戏

1.震前准备

  • 开启excel开发模式
  • 将普通xlsx另存为xlsm文件

2.设计窗体

image-20210319095302305

各项控件名称:

image-20210319095328056

3.简单代码逻辑

全局hp血量

Public lz_hp As Integer
Public th_hp As Integer

开始按钮点击事件

Private Sub btn_start_Click()
    '初始化血量,技能名称
    lz_hp = 150
    th_hp = 150
    Dim lz_skills As Variant
    lz_skills = Array("给你一拳", "拱你", "干嘛?", "欸嘿")
    Dim th_skills As Variant
    th_skills = Array("大战不带我?", "一套西湖别墅", "狗东西", "你要好好发光发热")
    
    Dim i As Integer
    '开启对战
    For i = 0 To 100
        If lz_hp < 0 Or th_hp < 0 Then
            Exit For
        End If
        
        If i Mod 2 = 0 Then
            vs_event "桃花", lz_hp, th_skills
        Else
            vs_event "落猪", th_hp, lz_skills
        End If
    Next
    
    If lz_hp < 0 Then
        txt_log.Value = txt_log.Value + "呜呜呜!!!落猪倒了QAQ"
    Else
        txt_log.Value = txt_log.Value + "呜呜呜!!!桃桃倒了QAQ"
    End If
End Sub

对战逻辑处理

'传入a角色,传b hp, 传a技能
Sub vs_event(role As String, ByRef hp As Integer, skills As Variant)
    Dim time1 As Single
    time1 = timer
    Do
        DoEvents
    Loop While timer - time1 < 1
    Dim n1 As Integer
    n1 = Int(20 * Rnd)
    nr = Int(4 * Rnd)
    hp = hp - n1
    
    '扣血
    If role = "桃花" Then
        lab_lzhp.Caption = "HP:" & hp
    Else
        lab_thhp.Caption = "HP:" & hp
    End If
    
    txt_log.Value = txt_log.Value + role + "使用了[" & skills(nr) & "]造成" & n1 & "伤害" & Chr(10)
    txt_log.SetFocus
End Sub

4.运行展示

image-20210319104416546

Q.E.D.


这个人很懒,什么都没有写