原文和更多项目链接:https://autohotkey.com/boards/viewtopic.php?f=6&t=7348

 

项目打包下载地址:

蓝奏云:https://wwp.lanzouj.com/iJfub1xom8nc   提取码:ahk6

1233网盘:https://www.123pan.com/s/ufi3Td-w0O53.html   提取码:ahk6

 

代码片段:

; Script Information ===========================================================
; Name:        Project 2D
; Description: Example of a tile-based game engine using AutoHotkey
; AHK Version: AHK_L 1.1.25.01 (Unicode 32-bit) - March 5, 2017
; OS Version:  Windows 2000+
; Language:    English - United States (en-US)
; Author:      Weston Campbell <westoncampbell@gmail.com>
; Website:     https://autohotkey.com/boards/viewtopic.php?f=6&t=7348
; ==============================================================================

; Revision History =============================================================
; Revision 2 (March 13, 2017)
; * Complete rewrite
; ------------------------------------------------------------------------------
; Revision 1 (June 08, 2016)
; * Initial Release
; ==============================================================================

; Auto-Execute =================================================================
#SingleInstance, Force ; Allow only one running instance of script
#Persistent ; Keep the script permanently running until terminated
#NoEnv ; Avoid checking empty variables for environment variables
#Warn ; Enable warnings to assist with detecting common errors
#NoTrayIcon ; Disable the tray icon of the script
SendMode, Input ; The method for sending keystrokes and mouse clicks
SetWorkingDir, %A_ScriptDir% ; Set the working directory of the script
SetBatchLines, -1 ; The speed at which the lines of the script are executed
SetWinDelay, -1 ; The delay to occur after modifying a window
SetControlDelay, -1 ; The delay to occur after modifying a control
OnExit("OnUnload") ; Run a subroutine or function when exiting the script

return ; End automatic execution
; ==============================================================================

; Labels =======================================================================
;GuiEscape:
GuiClose:
ExitSub:
    ExitApp ; Terminate the script unconditionally
return
; ==============================================================================

; Functions ====================================================================
OnLoad() {
    Global ; Assume-global mode
    Static Init := OnLoad() ; Call function

    OnMessage(0x0100, "WM_KEYDOWN")
}

OnUnload(ExitReason, ExitCode) {
    Global ; Assume-global mode
}

WM_KEYDOWN(wParam, lParam, Msg, Hwnd) {
    Global ; Assume-global mode
    Static VK_UP := 26, VK_LEFT := 25, VK_DOWN := 28, VK_RIGHT := 27,
    VK_KEY_W := 57, VK_KEY_A := 41, VK_KEY_S := 53, VK_KEY_D := 44,
    VK_F1 := 70, VK_RETURN := "D",  VK_ESCAPE := "1B"

    If (lParam & 0x40000000) {
        return ; Disable auto-repeat
    }

    VK := Format("{:x}", wParam)

    If (VK = VK_UP) || (VK = VK_KEY_W) {
        Hero.Move(0, -1, true)
    }

    If (VK = VK_LEFT) || (VK = VK_KEY_A) {
        Hero.Move(-1, 0, true)
    }

    If (VK = VK_DOWN) || (VK = VK_KEY_S) {
        Hero.Move(0, 1, true)
    }

    If (VK = VK_RIGHT) || (VK = VK_KEY_D) {
        Hero.Move(1, 0, true)
    }

    If (VK = VK_RETURN) {
        GridAction("VK_RETURN")
    }

    If (VK = VK_F1) {
        GuiControlGet, DebugVisible, Visible, Debug
        GuiControl, % (!DebugVisible ? "Show" : "Hide"), Debug
    }

    If (VK = VK_ESCAPE) {
        GoSub, ExitSub
    }
}

GuiCreate() {
    Global ; Assume-global mode
    Static Init := GuiCreate() ; Call function

    Menu, Tray, Icon, resources\images\icon.ico
    Gui, +LastFound -Resize +HWNDhProject2D
    Gui, Color, 000000
    Gui, Margin, 10, 10
  Gui, Add, Edit, x0 y0 w0 h0 0x800 ; Focus
  Gui, Add, Picture, x0 y0 w640 h640 vStage
    Gui, Add, Picture, x0 y0 w32 h32 vHero BackgroundTrans
    Gui, Add, Text, x10 y10 w620 r3 cFFFFFF vDebug BackgroundTrans
    Gui, Show, w640 h640, Project 2D

  Stage := new Stage("Stage")
  Stage.Color("000000")
  Stage.Background("bg_title.png")
    Stage.Map("map_0000.map")

  Hero := new Player("Hero")
    Hero.Move(-1, -1, false)
}

GridAction(Action := "") {
    Global ; Assume-global mode

    If (GridType = 1) {
        Hero.Move(PX, PY, false)
        return
    }

    If (Map = "map_0000.map") {
        If (Action = "VK_RETURN") {
            Stage.Background("bg_0001.png")
            Stage.Map("map_0001.map")
            Hero.Move(9, 11, false)
            return
        }
    }

    If (Map = "map_0001.map") {
        If (GridType = 2) { ; Go to next map
            Stage.Background("bg_0002.png")
            Stage.Map("map_0002.map")
            Hero.Move(PX, 19, false)
            return
        }
    }

    If (Map = "map_0002.map") {
        If (GridType = 2) { ; Go to previous map
            Stage.Background("bg_0001.png")
            Stage.Map("map_0001.map")
            Hero.Move(PX, 0, false)
            return
        }

        If (GridType = 3) { ; Go to next map
            Stage.Background("bg_0003.png")
            Stage.Map("map_0003.map")
            Hero.Move(PX, 19, false)
            return
        }
    }

    If (Map = "map_0003.map") {
        If (GridType = 2) { ; Go to previous map
            Stage.Background("bg_0002.png")
            Stage.Map("map_0002.map")
            Hero.Move(PX, 0, false)
            return
        }

        If (GridType = 3) { ; Fell in hole
            Hero.Move(10, 19, false)
            return
        }

        If (GridType = 4) { ; Go to next map
            Stage.Background("bg_0004.png")
            Stage.Map("map_0004.map")
            Hero.Move(16, 15, false)
            return
        }
    }

    If (Map = "map_0004.map") {
        If (GridType = 2) { ; Go to previous map
            Stage.Background("bg_0003.png")
            Stage.Map("map_0003.map")
            Hero.Move(8, 10, false)
            return
        }

        If (GridType = 3) { ; AutoHotkey "A" icon
            Stage.Background("bg_0004_2.png")
            Stage.Map("map_0004_2.map")
            return
        }
    }

    If (Map = "map_0004_2.map") {
        If (GridType = 2) { ; Go to next map
            Stage.Background("bg_0005.png")
            Stage.Map("map_0005.map")
            Hero.Move(19, PY, false)
            return
        }
    }

    If (Map = "map_0005.map") {
        If (GridType = 2) { ; Go to previous map
            Stage.Background("bg_0004_2.png")
            Stage.Map("map_0004_2.map")
            Hero.Move(0, PY, false)
            return
        }

        If (GridType = 3) { ; Key item
            Stage.Background("bg_0005_2.png")
            Stage.Map("map_0005_2.map")
            return
        }
    }

    If (Map = "map_0005_2.map") {
        If (GridType = 2) { ; AutoHotkey "H" icon
            Stage.Background("bg_0005_3.png")
            Stage.Map("map_0005_3.map")
            return
        }
    }

    If (Map = "map_0005_3.map") {
        If (GridType = 2) { ; Go to next map
            Stage.Background("bg_0006.png")
            Stage.Map("map_0006.map")
            Hero.Move(PX, 0, false)
            return
        }
    }

    If (Map = "map_0006.map") {
        If (GridType = 2) { ; Go to previous map
            Stage.Background("bg_0005_3.png")
            Stage.Map("map_0005_3.map")
            Hero.Move(PX, 19, false)
            return
        }

        If (GridType = 3) { ; Key item
            Stage.Background("bg_0006_2.png")
            Stage.Map("map_0006_2.map")
            return
        }
    }

    If (Map = "map_0006_2.map") {
        If (GridType = 2) { ; AutoHotkey "K" icon
            Stage.Background("bg_0006_3.png")
            Stage.Map("map_0006_3.map")
            return
        }
    }

    If (Map = "map_0006_3.map") {
        If (GridType = 2) { ; Go to next map
            Stage.Background("bg_0007.png")
            Stage.Map("map_0007.map")
            Hero.Move(9, 13, false)
            return
        }
    }

    If (Map = "map_0007.map") {
        If (GridType = 2) { ; AutoHotkey icon
            Stage.Background("bg_0008.png")
            Stage.Map("map_0008.map")
            Hero.Move(-1, -1, false)
            return
        }
    }
}

Class Stage {
    __New(Hwnd) {
        this.Stage := Hwnd
    }

  Background(File) {
        Global ; Assume-global mode

    GuiControl,, % this.Stage, % "resources\images\" (Background := File)
  }

  Color(Hex) {
    Gui, Color, % Hex
  }

    Map(File) {
        Global ; Assume-global mode

        GridMap := []

        FileRead, MapData, % "resources\maps\" (Map := File)

        For Each, Line In StrSplit(MapData, "`n", "`r") {
            GridMap.Push(StrSplit(Line))
        }
    }
}

Class Player {
    __New(Hwnd) {
        this.Player := Hwnd
    }

  Visible(Show := true) {
    GuiControl, % (Show ? "Show" : "Hide"), % this.Player
  }

  Image(File) {
    GuiControl,, % this.Player, % "resources\images\" File
  }

    Move(X, Y, Relative := 0) {
        Global ; Assume-global mode

    GuiControlGet, Player, Pos, % this.Player
        PX := (PlayerX // 32), PY := (PlayerY // 32)
        X := (Relative ? PX + X : X), Y := (Relative ? PY + Y : Y)
        GridType := GridMap[Y + 2, X + 2]
        GuiControl,, Debug, %X%, %Y%`n%Background%`n%Map% ; Debug info

        If (Y < PY) {
            this.Image("player_up.png")
        }

        If (X < PX) {
            this.Image("player_left.png")
        }

        If (Y > PY) {
            this.Image("player_down.png")
        }

        If (X > PX) {
            this.Image("player_right.png")
        }

        GuiControl, Move, % this.Player, % " x" X * 32 " y" Y * 32
        GridAction("Move")
    }
}
; ==============================================================================

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。