Code: Select all
;
; Author: Helgef.
; Date: 2016-07-24
; Scrolling game example.
; Move Mushy with Left and right arrows. Don't move the game window while Mushy is moving.
; Save platform.png and mushy4.png in script folder.
; Escape exits at any time.
;
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; Settings
SetWinDelay,-1
moveSpeed:=5 ; Movement speed of mushy/scroll, in pixels.
; Constants
iW:=3392 ; Image dimensions
iH:=448
sW:=800 ; Screen dimensions
sH:=238
; Variables
bgX:=0 ; Variable position of the background image
bgY:=0 ; Constant in this example
mdX:=10 ; Initial displacement of mushy, reltaive to gameScreen
mdY:=189
mX:=mdX ; Variable position of the mushroom
mY:=mdY
mRel:=0 ; The displacement of Mushy reltative his starting position, x-wise.
; Game screen gui.
Gui, gameScreen: new
Gui, gameScreen: +ToolWindow +HWNDgameScreen
Gui, gameScreen: Add, Picture, HWNDbg, platform.png
Gui, gameScreen: Show, w%sW% h%sH%, Mushy
WinWait, Mushy
WinGetPos,gX,gY,,,ahk_id %gameScreen%
; Mushy gui
Gui, mushy: new
Gui, mushy: +ToolWindow -Caption +HWNDmushyWin +AlwaysOnTop
Gui, mushy: Color, ABCDEF
Gui, mushy: Add, Picture, AltSubmit BackgroundTrans w32 h-1,mushy4.gif
Gui, mushy: Show, % "x" . gX+mX . " y" . gY+mY
WinSet, TransColor, ABCDEF, ahk_id %mushyWin% ; Make mushy background transparent
; Activate the gameScreen
WinActivate, ahk_id %gameScreen%
; Pin mushy to gameScreen, please dont move screen while scrolling/moving, will mess up the game. <-Fix this.
OnMessage(0x03, "WM_MOVE")
; Hotkeys
; Gameplay
Hotkey, IfWinActive, ahk_id %gameScreen%
Hotkey, Left, scrollLeft, on
Hotkey, Right, scrollRight, on
Hotkey, IfWinActive
; Quit
Hotkey, Esc, QuitRoutine, on
return ; End autoexec.
; Gameplay routines
scrollLeft:
WinGetPos, mX,mY,,,ahk_id %mushyWin%
WinGetPos,gX,gY,,,ahk_id %gameScreen%
Loop
{
if (mX!=gX && (mX<gX+sW/2 || abs(bgX)<sW/2)) ; If mushy is at the begining or end
{
WinMove,ahk_id %mushyWin%,, mX-=moveSpeed
mRel-=moveSpeed
}
else if bgX<4 ; Mushy is is centered at screen. The number 4 stops mushy from peeking outside of the background image.
{
GuiControl, Move, %bg%, % "x" bgX+=moveSpeed
}
Sleep,1
} until !GetKeyState(A_ThisHotkey, "P")
return
scrollRight:
WinGetPos, mX,mY,,,ahk_id %mushyWin%
WinGetPos,gX,gY,,,ahk_id %gameScreen%
Loop
{
if (mX=gX+sW-160) ; End of level.
{
msgbox, You won!
ExitApp
}
if (mX<gX+sW/2 || abs(bgX)>iW-sW) ; If mushy is at the begining or end
{
WinMove,ahk_id %mushyWin%,, mX+=moveSpeed
mRel+=moveSpeed
}
else
{ ; Mushy is is centered at screen.
GuiControl, Move, %bg%, % "x" bgX-=moveSpeed
}
Sleep,1
} until !GetKeyState(A_ThisHotkey, "P")
return
; Other labels
gameScreenGuiClose:
ExitApp
return
QuitRoutine:
ExitApp
return
; Functions
WM_MOVE(wParam,lParam,msg,hwnd)
{
; Pins mushyWin to gameScreen.
global mRel, gameScreen, mushyWin, mdX, mdY
if (hwnd=gameScreen)
{
WinGetPos,gX,gY,,,ahk_id %gameScreen%
WinGetPos,mX,mY,,,ahk_id %mushyWin%
WinMove,ahk_id %mushyWin%,,mdX+gX+mRel,mdY+gY
}
}