Suggestions for building a game in AHK

Ask gaming related questions (AHK v1.1 and older)
Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

Suggestions for building a game in AHK

Post by Miguel7 » 10 Oct 2014, 23:40

Happy Friday nite!

Now I get that this forum is probably more about how to use AHK to do things in games, and I know AHK is not necessarily the best for developing games, but it's something I've been wanting to figure out for some time. And I'm sure I'm not the first one to have looked into this. AHK is really that easy to program in, that if someone cam get it to do games, it'll just add another layer of Awesome to an already amazing language.

So I've got some ideas that I think will help me get there. I've gotten okay results using GUI controls as game objects, but as the docs say the "MoveDraw" command causes that weird blinking when called "rapidly and repeatedly". And if I use the regular "Move" command, it still works but looks kind of choppy. So that works for games like tic-tac-toe, checkers, maybe sudoku (btw I'm going 2b working on those) but scrolling shooters, platformers, etc. don't seem to be possible in AHK - at least, not in AHK alone.

But one of the things that make AHK so sick is that it can access COM, DLLs, and Windows APIs. It can query databases, send HTTP requests, and do all kinds of stuff I haven't learned yet. So I'm sure it can do games.

So my question for you guys is, what's been done already (and what worked/didn't)? I have a few ideas that I think would work, but if possible I'd like to avoid some of the pitfalls and start in the right direction. So here are my ideas:
1. Learn to use "gdip.ahk" (though I'll need to find a tutorial "for dummies" :lol: - anyone know a good one?)
2. Create a DLL in C#, containing a class that inherits from the Panel object. This object would have some typical drawing methods (like in System.Drawing does for C#, similar to AWT in Java or Canvas in HTML5) that might work with DllCall. I already know C# so it's not like I'd have to learn another language :lol: Not sure if that would work tho.
3. Maybe there's some kind of COM object out there that works in a similar way. There's so much more to COM than Excel w/VBA and Internet Explorer w/JavaScript. I bet there's an object that supports basic graphics and sound well enough to make a game work.
4. I could also try creating the page in HTML and using COM/JavaScript to move stuff around on the page

Any of these look like they could work, but I'm thinking some of you gamers out there have already tried some of these (or others I haven't thought of). So, any info on the subject would be great. Thanks! :)

Guest

Re: Suggestions for building a game in AHK

Post by Guest » 16 Oct 2014, 09:59

I think you should consider posting this in a different part of the forum. I always figured these gaming subforums were pretty much just here so the general populace doesn't have to listen to all the skiddies beg for custom anti-afk scripts.

User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Suggestions for building a game in AHK

Post by FanaticGuru » 16 Oct 2014, 18:43

This is a pretty cool example of a game in AHK.

http://www.autohotkey.com/board/topic/5 ... ssic-game/

I am going to repost the code just in case it gets lost on that site some day.

Code: Select all

; heart form for your girl friend...
; formR1:="0,-20,8,-28,20,-28,28,-20,28,-8,16,12,0,28,-16,12,-28,-8,-28,-20,-20,-28,-8,-28,0,-20"
; formR2:="0,-10,4,-14,10,-14,14,-10,14,-4,8,6,0,14,-8,6,-14,-4,-14,-10,-10,-14,-4,-14,0,-10"
; formR3:="0,-5,2,-7,5,-7,7,-5,7,-2,4,3,0,7,-4,3,-7,-2,-7,-5,-5,-7,-2,-7,0,-5"

#SingleInstance Force
#Persistent
#NoEnv
Process, Priority, , High
SetBatchLines, -1
OnExit, ExitSub

Left:="Left"        ; turn left
Right:="Right"      ; turn right
Up:="Up"            ; move forward
Down:="Down"        ; move backward
Space:="Space"      ; use weapon

VarSetCapacity(ptWin, 16, 0)
NumPut(W:=512, ptWin, 8) , NumPut(H:=384, ptWin, 12)
Gui, Show, w%W% h%H%, Ahkroid
  hdcWin := DllCall("GetDC", "UInt", hwnd:=WinExist("A"))
  hdcMem := DllCall("CreateCompatibleDC", "UInt", hdcWin)
  hbm := DllCall("CreateCompatibleBitmap", "uint", hdcWin, "int", W, "int", H)
DllCall("SelectObject", "uint", hdcMem, "uint", hbm)

X:=W/2 , Y:=H/2 , Vx:=Vy:=Va:=cd:=score:=0 , shield:="||||||||||" , nRoid:=6
  formS1:="0,0,10,5,0,-15,-10,5,0,0"
  formS2:="0,10,10,5,0,-15,-10,5,0,10"
VarSetCapacity(ptShip, 40)

  formB1:="-1,0,0,-1,1,0,0,1,-1,0"
VarSetCapacity(ptBall, 40)

  formR1:="-12,-28,0,-20,16,-28,28,-16,16,-8,28,8,12,28,-8,20,-16,28,-28,16,-20,0,-28,-16,-12,-28"
  formR2:="-6,-14,0,-10,8,-14,14,-8,8,-4,14,4,6,14,-4,10,-8,14,-14,8,-10,0,-14,-8,-6,-14"
  formR3:="-3,-7,-0,-5,4,-7,7,-4,4,-2,7,2,3,7,-2,5,-4,7,-7,4,-5,0,-7,-4,-3,-7"
VarSetCapacity(ptRoid, 104)
loop %nRoid%
  listRoid .= "|1," rand(0, W, X) "," rand(0, H, Y) "," rand(0.0, 6)-3 "," rand(0.0, 6)-3

SetTimer, Update, 25
return

; --------------------------------

Update:
Critical

DllCall("FillRect", "uint", hdcMem, "uint", &ptWin, "uint", 0)

DllCall("TextOut", "uint", hdcMem, "uint", 16, "uint", 8, "uint", &score, "uint", StrLen(score))
DllCall("TextOut", "uint", hdcMem, "uint", 16, "uint", 24, "uint", &shield, "uint", StrLen(shield))

; ----

Va += GetKeyState(Left) ? -0.1 : GetKeyState(Right) ? 0.1 : 0
formS0:="" , form := GetKeyState(Up) ? formS2 : formS1
loop, parse, form, `,
  a_index & 1 ? i1:=a_loopfield : formS0 .= i1*cos(Va)-a_loopfield*sin(Va) "," i1*sin(Va)+a_loopfield*cos(Va) ","

Vs := GetKeyState(Up) ? 0.4 : GetKeyState(Down) ? -0.2 : 0
Vx:=Vx*0.95+sin(Va)*Vs , Vy:=Vy*0.95-cos(Va)*Vs
X:= X<0 ? W : X>W ? 0 : X+Vx , Y:= Y<0 ? H : Y>H ? 0 : Y+Vy

DllCall("DeleteObject", "UInt", hShip)
loop, parse, formS0, `,
    NumPut(a_loopfield+(a_index & 1 ? X : Y), ptShip, a_index*4-4)
DllCall("Polyline", "uint", hdcMem, "uint", &ptShip, "int", 5)
hShip := DllCall("CreatePolygonRgn", "uint", &ptShip, "int", 4, "int", 1)

; ----

cd-= cd>0 ? 1 : 0
if ( GetKeyState(Space) && cd=0 )
  cd:=5 , listBall .= "|" a_tickcount "," X+sin(Va)*10 "," Y-cos(Va)*10 "," Va

loop, parse, listBall, |
{
  if a_index=1
    continue
  loop, parse, a_loopfield, `,
    i%a_index%:=a_loopfield
  if (i1+1000 < a_tickcount) {
    StringReplace, listBall, listBall, % "|" i1 "," i2 "," i3 "," i4 ,
    continue
  }
    iX:=i2+sin(i4)*8 , iY:=i3-cos(i4)*8
    iX:= iX<0 ? W : iX>W ? 0 : iX , iY:= iY<0 ? H : iY>H ? 0 : iY
    StringReplace, listBall, listBall, % "|" i1 "," i2 "," i3 "," i4 , % "|" i1 "," iX "," iY "," i4
    loop, parse, formB1, `,
      NumPut(a_loopfield+(a_index & 1 ? iX : iY), ptBall, a_index*4-4)
    DllCall("Polyline", "uint", hdcMem, "uint", &ptBall, "int", 5)
}

; ----

loop, parse, listRoid, |
{
  id:=a_index-1
  if a_index=1
    continue
  loop, parse, a_loopfield, `,
    i%a_index%:=a_loopfield
  iX:= i2+i4<0 ? W : i2+i4>W ? 0 : i2+i4 , iY:= i3+i5<0 ? H : i3+i5>H ? 0 : i3+i5
  StringReplace, listRoid, listRoid, % "|" i1 "," i2 "," i3 "," i4 "," i5 , % "|" i1 "," iX "," iY "," i4 "," i5
  loop, parse, formR%i1%, `,
    NumPut(a_loopfield+(a_index & 1 ? iX : iY), ptRoid, a_index*4-4)
  DllCall("Polyline", "uint", hdcMem, "uint", &ptRoid, "int", 13)
  hRoid := DllCall("CreatePolygonRgn", "uint", &ptRoid, "int", 12, "int", 1)

  loop, parse, listBall, |
  {
    if a_index=1
      continue
    loop, parse, a_loopfield, `,
      t%a_index%:=a_loopfield
    if ( DllCall("PtInRegion", "uint", hRoid, "uint", t2, "uint", t3) <> 0 )
    {
      StringReplace, listBall, listBall, % "|" t1 "," t2 "," t3 "," t4 ,
      StringReplace, listRoid, listRoid, % "|" i1 "," iX "," iY "," i4 "," i5 ,
      score+=i1*5
      if (i1<3) {
        i1++
        loop 2
          listRoid .= "|" i1 "," iX "," iY "," rand(0.0, 6)-3 "," rand(0.0, 6)-3
      }
    }
  }

  if ( DllCall("CombineRgn", "uint", hRoid, "uint", hRoid, "uint", hShip, "int", 1) <> 1 ) {
  Vx:=i4 , Vy:=i5
  shield := SubStr(shield, 1, -1)
    if (shield="") {
      msgbox %score% points
      Reload
    }
  StringReplace, listRoid, listRoid, % "|" i1 "," iX "," iY "," i4 "," i5 ,
    if (i1<3) {
      i1++
      loop 2
        listRoid .= "|" i1 "," iX "," iY "," rand(0.0, 6)-3 "," rand(0.0, 6)-3
    }
  }

DllCall("DeleteObject", "UInt", hRoid)
}

; -----

if (id<nRoid-1) {
nRoid++ , shield.="|"
loop %nRoid%
  listRoid .= "|1," rand(0, W, X) "," rand(0, H, Y) "," rand(0.0, 6)-3 "," rand(0.0, 6)-3
}

DllCall("BitBlt", "uint", hdcWin, "int", 0, "int", 0, "int", W, "int", H, "uint", hdcMem, "int", 0, "int", 0, "uint", 0xCC0020)
return

; --------------------------------

Rand(min=0, max=1, mask="") {
loop {
  Random, result, min, max
  if ( mask="" || result<mask-100 || result>mask+100 )
    return result
  }
}

ExitSub:
guiclose:
  DllCall("DeleteObject", "UInt", hShip)
  DllCall("DeleteObject", "UInt", hRoid)
  DllCall("DeleteObject", "UInt", hbm)
  DllCall("DeleteDC", "UInt", hdcMem)
  DllCall("ReleaseDC", "UInt", hwnd, "UInt", hdcWin)
exitapp
Must run in 32 bit AHK, will not work in 64 bit AHK.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

Re: Suggestions for building a game in AHK

Post by Miguel7 » 17 Oct 2014, 02:33

Hey thanks for the info! This is definitely a solid lead. Going by the names of the DllCalls ("FillRect", "CreatePolygonRgn", etc.) it appears that some of the drawing functions are available for AHK to use, so I'll study them to find out how they work. Once I know that, I should be able to create an object that can be used to do stuff like this (making drawing stuff to the screen - and games in AHK - a lot easier). I'll keep you guys posted! :)

PS - how would I move this topic to another section of the forum? I'm cool with however you guys want me to do it.

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Suggestions for building a game in AHK

Post by jNizM » 17 Oct 2014, 02:50

OpenGL in AutoHotkey (German Thread)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

Re: Suggestions for building a game in AHK

Post by Miguel7 » 17 Oct 2014, 03:37

Well, I don't speak German, but I've heard of OpenGL (didn't know AHK could use it tho - thanks for the tip!). I bet it's accessed either with COM or DllCall, and either way it's definitely worth a shot! Now I got two great leads.

Muchas gracias - :lol: como dije no hablo aleman.
(Thank you very much - LOL, like I said I don't speak German).

Post Reply

Return to “Gaming Help (v1)”