Every few months a game comes along which is so addictive, players can’t seem to put it down – no matter how frustrating it may get. Last year one of those games was Super Hexagon. After fighting his way through several levels, [Val] decided that designing a bot to beat the game would be more efficient than doing it himself. Having played a few rounds of Super Hexagon ourselves, we can’t fault him on that front!
At its core, Super Hexagon is a simple game. Walls move from the screen edges toward a ship located near the center of the screen. The player uses the arrow keys to “orbit” the ship around a central shape. Avoid getting crushed by the walls, and you’re golden. However, the entire game board is constantly spinning, expanding, contracting, flashing, and generally doing things to disorient the player while ever more complex wall patterns move in to kill you. In short, Super Hexagaon makes Touhou bullet hell games look like a cakewalk.
The first step in beating the game is to capture the screen. [Val] tried Fraps and VLC, but lags of 2 seconds or more were not going to work. Then [Val] turned to DLL Injection. Super Hexagon calls the OpenGL function glutSwapBuffers() to implement double buffering. Every frame of the game is rendered in the background. Once rendering is complete glutSwapBuffers() is called to swap the buffers, and the process starts over again. [Val] changed the game code such that his own frame capture function would be called instead of glutSwapBuffers(). Once he was done capturing the game’s video buffer, [Val] then called the real glutSwapBuffers() function. It worked perfectly.
Now that he had an image, [Val] used OpenCV to process it. Although game is graphically very noisy, there are only a few colors used at any one time. It didn’t take much work to come up with an algorithm which would create a binary image of the walls and the ship itself.
[Val] cast rays from the center of each wall through the center of the screen. The ray which was longest before intersecting another wall would be the best escape route. This simple solution worked, but only for about 40 seconds. At that point, Super Hexagon would start throwing more complex patterns, and the AI would fail. The final solution was to create an accessibility condition which also took into account how much space was available between the various approaching walls. This new version of the AI was able to beat the game.
So was this a more efficient method than grinding through Super Hexagon manually? Since [Val] now knows all about DLL injection and OpenCV, we sure think it was!
Click past the break to see the [Val’s] bot in action!
19 thoughts on “Beating Super Hexagon With OpenCV And DLL Injection”
Awesome ! A shame there are no video and data on beating the “after level 6” patterns. If you have time to waste it’s not an impossible game to beat (normal way).
I reran the bot just to see, and I recorded the level 6: https://www.youtube.com/watch?v=WHK-p1dpJSs
After the level 6, it does not do so good, and I never played this high, so I don’t know why.
Note, there is a video of the developer that shows that it is possible to beat that final section.
It’s not really that hard. I can beat it pretty consistently.
I suspect the reason it fails is that the wall speed is variable and even goes backwards in level 6
The wall speed has an effect on the path to safety, and I guess until now you didn’t need for your algorithm to take varying wall speed into consideration
Judging by the few frames before the crash, looks like the bot was so fast in steering the craft, that it drove it into a wall
I’d like to see that with video stabilization centered on the ship.
Does this remind anyone a little of polybias?
No, because **Polybius** (get the name right, please) was never actually a real game in the first place. It was nothing more than an urban legend for which random people made games after the fact.
Can’t beat the game? Hack the game. Kobayashi Maru.
One of my favourite games. i have beaten all levels except of the last one after around 11 hours and i hate the fact that this game is limited to 60fps. This is a game where a 144hz screen will definetly help.
The dll injection method is very interesting and probably the only way to get the required low delay.
Good work. but as always, playing for yourself is the fair and fun way ;)
Its not the only way, and it will likely introduce quite a lot of lag. but its the easiest, unfortunately the best other ways require access to private api’s , and others knowledge of pci architecture and deswizzling. windows 8 has desktop duplication, and there are some other dxgi ways of capturing the front buffer , for windowed apps.
You could probably do it with a monster FPGA by analyzing vga signal.
OR get high fps camera and analyze the data on another computer (go Pro). Then use ardunio to send control commands ;)
aww. well. if i saw it right he only played the third level. the end game screen appears after the first three levels but there are three more (extreme mode).
This is what i mentioned with beating all levels. The “last” one is the third level in double? speed. This is where that concept could really help as i can even feel the delay in different keyboards and controllers and did not finished level 6 yet.
Maybe there is still to much delay for the last one.
*update* sorry for that. Just seen the other video on his site and he did finish the last one successfully.
That is impressive.
IMHO coding a winning strategy just because you can beats ‘the fleshy’ alternative hands down.
Sick hack ,hahaha watch it while on shrooms…awe so me
Btw can i use the dll injector from http://dll-injector.com to re-create this procress?
Please be kind and respectful to help make the comments section excellent. (Comment Policy)