1
+ ; Script Information ===========================================================
2
+ ; Name: Project 2D
3
+ ; Description: Example of a tile-based game engine using AutoHotkey
4
+ ; AHK Version: AHK_L 1.1.25.01 (Unicode 32-bit) - March 5, 2017
5
+ ; OS Version: Windows 2000+
6
+ ; Language: English - United States (en-US)
7
+ ; Author: Weston Campbell <[email protected] >
8
+ ; Website: https://autohotkey.com/boards/viewtopic.php?f=6&t=7348
9
+ ; ==============================================================================
10
+
11
+ ; Revision History =============================================================
12
+ ; Revision 2 (March 13, 2017)
13
+ ; * Complete rewrite
14
+ ; ------------------------------------------------------------------------------
15
+ ; Revision 1 (June 08, 2016)
16
+ ; * Initial Release
17
+ ; ==============================================================================
18
+
19
+ ; Auto-Execute =================================================================
20
+ #SingleInstance , Force ; Allow only one running instance of script
21
+ #Persistent ; Keep the script permanently running until terminated
22
+ #NoEnv ; Avoid checking empty variables for environment variables
23
+ #Warn ; Enable warnings to assist with detecting common errors
24
+ #NoTrayIcon ; Disable the tray icon of the script
25
+ SendMode , Input ; The method for sending keystrokes and mouse clicks
26
+ SetWorkingDir , %A_ScriptDir % ; Set the working directory of the script
27
+ SetBatchLines , - 1 ; The speed at which the lines of the script are executed
28
+ SetWinDelay , - 1 ; The delay to occur after modifying a window
29
+ SetControlDelay , - 1 ; The delay to occur after modifying a control
30
+ OnExit (" OnUnload" ) ; Run a subroutine or function when exiting the script
31
+
32
+ return ; End automatic execution
33
+ ; ==============================================================================
34
+
35
+ ; Labels =======================================================================
36
+ ; GuiEscape:
37
+ GuiClose :
38
+ ExitSub :
39
+ ExitApp ; Terminate the script unconditionally
40
+ return
41
+ ; ==============================================================================
42
+
43
+ ; Functions ====================================================================
44
+ OnLoad () {
45
+ Global ; Assume-global mode
46
+ Static Init := OnLoad() ; Call function
47
+
48
+ OnMessage (0x0100 , " WM_KEYDOWN" )
49
+ }
50
+
51
+ OnUnload (ExitReason, ExitCode ) {
52
+ Global ; Assume-global mode
53
+ }
54
+
55
+ WM_KEYDOWN (wParam, lParam, Msg, Hwnd ) {
56
+ Global ; Assume-global mode
57
+ Static VK_UP := 26 , VK_LEFT := 25 , VK_DOWN := 28 , VK_RIGHT := 27 ,
58
+ VK_KEY_W := 57 , VK_KEY_A := 41 , VK_KEY_S := 53 , VK_KEY_D := 44 ,
59
+ VK_F1 := 70 , VK_RETURN := " D" , VK_ESCAPE := " 1B"
60
+
61
+ If (lParam & 0x40000000 ) {
62
+ return ; Disable auto-repeat
63
+ }
64
+
65
+ VK := Format (" {:x}" , wParam)
66
+
67
+ If (VK = VK_UP) || (VK = VK_KEY_W) {
68
+ Hero.Move(0 , - 1 , true )
69
+ }
70
+
71
+ If (VK = VK_LEFT) || (VK = VK_KEY_A) {
72
+ Hero.Move(- 1 , 0 , true )
73
+ }
74
+
75
+ If (VK = VK_DOWN) || (VK = VK_KEY_S) {
76
+ Hero.Move(0 , 1 , true )
77
+ }
78
+
79
+ If (VK = VK_RIGHT) || (VK = VK_KEY_D) {
80
+ Hero.Move(1 , 0 , true )
81
+ }
82
+
83
+ If (VK = VK_RETURN) {
84
+ GridAction(" VK_RETURN" )
85
+ }
86
+
87
+ If (VK = VK_F1) {
88
+ GuiControlGet , DebugVisible, Visible , Debug
89
+ GuiControl , % (! DebugVisible ? " Show" : " Hide" ), Debug
90
+ }
91
+
92
+ If (VK = VK_ESCAPE) {
93
+ GoSub , ExitSub
94
+ }
95
+ }
96
+
97
+ GuiCreate () {
98
+ Global ; Assume-global mode
99
+ Static Init := GuiCreate() ; Call function
100
+
101
+ Menu , Tray , Icon , resources\images\icon.ico
102
+ Gui , + LastFound - Resize + HWNDhProject2D
103
+ Gui , Color , 000000
104
+ Gui , Margin , 10 , 10
105
+ Gui , Add , Edit , x0 y0 w0 h0 0x800 ; Focus
106
+ Gui , Add , Picture , x0 y0 w640 h640 vStage
107
+ Gui , Add , Picture , x0 y0 w32 h32 vHero BackgroundTrans
108
+ Gui , Add , Text , x10 y10 w620 r3 cFFFFFF vDebug BackgroundTrans
109
+ Gui , Show , w640 h640, Project 2D
110
+
111
+ Stage := new Stage(" Stage" )
112
+ Stage.Color(" 000000" )
113
+ Stage.Background(" bg_title.png" )
114
+ Stage.Map(" map_0000.map" )
115
+
116
+ Hero := new Player(" Hero" )
117
+ Hero.Move(- 1 , - 1 , false )
118
+ }
119
+
120
+ GridAction (Action := "" ) {
121
+ Global ; Assume-global mode
122
+
123
+ If (GridType = 1 ) {
124
+ Hero.Move(PX, PY, false )
125
+ return
126
+ }
127
+
128
+ If (Map = " map_0000.map" ) {
129
+ If (Action = " VK_RETURN" ) {
130
+ Stage.Background(" bg_0001.png" )
131
+ Stage.Map(" map_0001.map" )
132
+ Hero.Move(9 , 11 , false )
133
+ return
134
+ }
135
+ }
136
+
137
+ If (Map = " map_0001.map" ) {
138
+ If (GridType = 2 ) { ; Go to next map
139
+ Stage.Background(" bg_0002.png" )
140
+ Stage.Map(" map_0002.map" )
141
+ Hero.Move(PX, 19 , false )
142
+ return
143
+ }
144
+ }
145
+
146
+ If (Map = " map_0002.map" ) {
147
+ If (GridType = 2 ) { ; Go to previous map
148
+ Stage.Background(" bg_0001.png" )
149
+ Stage.Map(" map_0001.map" )
150
+ Hero.Move(PX, 0 , false )
151
+ return
152
+ }
153
+
154
+ If (GridType = 3 ) { ; Go to next map
155
+ Stage.Background(" bg_0003.png" )
156
+ Stage.Map(" map_0003.map" )
157
+ Hero.Move(PX, 19 , false )
158
+ return
159
+ }
160
+ }
161
+
162
+ If (Map = " map_0003.map" ) {
163
+ If (GridType = 2 ) { ; Go to previous map
164
+ Stage.Background(" bg_0002.png" )
165
+ Stage.Map(" map_0002.map" )
166
+ Hero.Move(PX, 0 , false )
167
+ return
168
+ }
169
+
170
+ If (GridType = 3 ) { ; Fell in hole
171
+ Hero.Move(10 , 19 , false )
172
+ return
173
+ }
174
+
175
+ If (GridType = 4 ) { ; Go to next map
176
+ Stage.Background(" bg_0004.png" )
177
+ Stage.Map(" map_0004.map" )
178
+ Hero.Move(16 , 15 , false )
179
+ return
180
+ }
181
+ }
182
+
183
+ If (Map = " map_0004.map" ) {
184
+ If (GridType = 2 ) { ; Go to previous map
185
+ Stage.Background(" bg_0003.png" )
186
+ Stage.Map(" map_0003.map" )
187
+ Hero.Move(8 , 10 , false )
188
+ return
189
+ }
190
+
191
+ If (GridType = 3 ) { ; AutoHotkey "A" icon
192
+ Stage.Background(" bg_0004_2.png" )
193
+ Stage.Map(" map_0004_2.map" )
194
+ return
195
+ }
196
+ }
197
+
198
+ If (Map = " map_0004_2.map" ) {
199
+ If (GridType = 2 ) { ; Go to next map
200
+ Stage.Background(" bg_0005.png" )
201
+ Stage.Map(" map_0005.map" )
202
+ Hero.Move(19 , PY, false )
203
+ return
204
+ }
205
+ }
206
+
207
+ If (Map = " map_0005.map" ) {
208
+ If (GridType = 2 ) { ; Go to previous map
209
+ Stage.Background(" bg_0004_2.png" )
210
+ Stage.Map(" map_0004_2.map" )
211
+ Hero.Move(0 , PY, false )
212
+ return
213
+ }
214
+
215
+ If (GridType = 3 ) { ; Key item
216
+ Stage.Background(" bg_0005_2.png" )
217
+ Stage.Map(" map_0005_2.map" )
218
+ return
219
+ }
220
+ }
221
+
222
+ If (Map = " map_0005_2.map" ) {
223
+ If (GridType = 2 ) { ; AutoHotkey "H" icon
224
+ Stage.Background(" bg_0005_3.png" )
225
+ Stage.Map(" map_0005_3.map" )
226
+ return
227
+ }
228
+ }
229
+
230
+ If (Map = " map_0005_3.map" ) {
231
+ If (GridType = 2 ) { ; Go to next map
232
+ Stage.Background(" bg_0006.png" )
233
+ Stage.Map(" map_0006.map" )
234
+ Hero.Move(PX, 0 , false )
235
+ return
236
+ }
237
+ }
238
+
239
+ If (Map = " map_0006.map" ) {
240
+ If (GridType = 2 ) { ; Go to previous map
241
+ Stage.Background(" bg_0005_3.png" )
242
+ Stage.Map(" map_0005_3.map" )
243
+ Hero.Move(PX, 19 , false )
244
+ return
245
+ }
246
+
247
+ If (GridType = 3 ) { ; Key item
248
+ Stage.Background(" bg_0006_2.png" )
249
+ Stage.Map(" map_0006_2.map" )
250
+ return
251
+ }
252
+ }
253
+
254
+ If (Map = " map_0006_2.map" ) {
255
+ If (GridType = 2 ) { ; AutoHotkey "K" icon
256
+ Stage.Background(" bg_0006_3.png" )
257
+ Stage.Map(" map_0006_3.map" )
258
+ return
259
+ }
260
+ }
261
+
262
+ If (Map = " map_0006_3.map" ) {
263
+ If (GridType = 2 ) { ; Go to next map
264
+ Stage.Background(" bg_0007.png" )
265
+ Stage.Map(" map_0007.map" )
266
+ Hero.Move(9 , 13 , false )
267
+ return
268
+ }
269
+ }
270
+
271
+ If (Map = " map_0007.map" ) {
272
+ If (GridType = 2 ) { ; AutoHotkey icon
273
+ Stage.Background(" bg_0008.png" )
274
+ Stage.Map(" map_0008.map" )
275
+ Hero.Move(- 1 , - 1 , false )
276
+ return
277
+ }
278
+ }
279
+ }
280
+
281
+ Class Stage {
282
+ __New (Hwnd ) {
283
+ this.Stage := Hwnd
284
+ }
285
+
286
+ Background (File ) {
287
+ Global ; Assume-global mode
288
+
289
+ GuiControl ,, % this.Stage, % " resources\images\" (Background := File)
290
+ }
291
+
292
+ Color (Hex ) {
293
+ Gui , Color , % Hex
294
+ }
295
+
296
+ Map (File ) {
297
+ Global ; Assume-global mode
298
+
299
+ GridMap := []
300
+
301
+ FileRead , MapData, % " resources\maps\" (Map := File)
302
+
303
+ For Each, Line In StrSplit (MapData, " `n" , " `r" ) {
304
+ GridMap.Push(StrSplit (Line))
305
+ }
306
+ }
307
+ }
308
+
309
+ Class Player {
310
+ __New (Hwnd ) {
311
+ this.Player := Hwnd
312
+ }
313
+
314
+ Visible (Show := true ) {
315
+ GuiControl , % (Show ? " Show" : " Hide" ), % this.Player
316
+ }
317
+
318
+ Image (File ) {
319
+ GuiControl ,, % this.Player, % " resources\images\" File
320
+ }
321
+
322
+ Move (X, Y, Relative := 0 ) {
323
+ Global ; Assume-global mode
324
+
325
+ GuiControlGet , Player, Pos , % this.Player
326
+ PX := (PlayerX // 32 ), PY := (PlayerY // 32 )
327
+ X := (Relative ? PX + X : X), Y := (Relative ? PY + Y : Y)
328
+ GridType := GridMap[Y + 2 , X + 2 ]
329
+ GuiControl ,, Debug, %X%, %Y%`n%Background %`n%Map% ; Debug info
330
+
331
+ If (Y < PY) {
332
+ this.Image(" player_up.png" )
333
+ }
334
+
335
+ If (X < PX) {
336
+ this.Image(" player_left.png" )
337
+ }
338
+
339
+ If (Y > PY) {
340
+ this.Image(" player_down.png" )
341
+ }
342
+
343
+ If (X > PX) {
344
+ this.Image(" player_right.png" )
345
+ }
346
+
347
+ GuiControl , Move , % this.Player, % " x" X * 32 " y" Y * 32
348
+ GridAction(" Move" )
349
+ }
350
+ }
351
+ ; ==============================================================================
0 commit comments