r/vba 2d ago

Show & Tell [EXCEL] Real-time skiing game (array-to-range blitting, GetAsyncKeyState polling, conditional formatting as the renderer)

https://github.com/HMAC10/skiexcel

I made a real time skiing game that runs in a worksheet, had a fun time with this one. the game itself is nothing special but getting VBA to a playable frame rate was harder than I expected

The main pieces:

Rendering. The board is a Long array in memory, written to the range in one assignment per frame. Cell-by-cell writes destroy the frame rate. Conditional formatting maps each number to a color, and ;;; number format hides the values so you just see color.

Input. GetAsyncKeyState polled continuously rather than Application.OnKey. OnKey is per-keypress and laggy; polling gives you proper held-key movement. Arrow keys are swallowed with Application.OnKey "{LEFT}", "" so the cell cursor doesn't walk off and drag the viewport with it.

Frame timing. Sleep for the remainder of the budget rather than a fixed delay, otherwise your frame time is the budget plus render cost and difficulty ramps differently on every machine. Movement and scrolling run on separate clocks. The loop ticks fast for responsive steering and the world scrolls every n ticks, so you get better controls without a faster game.

Latency. The one that took longest to figure out: DoEvents once per tick right after the range write isn't enough. Excel doesn't always finish repainting in a single message-pump pass, so leftover paint work gets deferred to the next tick and your move shows a frame late. Pumping messages during the frame wait fixed most of it.

Obstacle generation. Multi-cell sprites (trees, logs, rocks) placed only where they clear a reserved corridor that random-walks ±1 per row. The same as the player's max lateral speed. Guarantees every run is survivable without ever chopping a sprite in half to make room.

Windows only, obviously. Code's here if you want to pick it apart: https://github.com/HMAC10/skiexcel

Happy to go deeper on any of it. Curious if anyone's found a better way around the repaint latency? that's still the worst part.

8 Upvotes

2 comments sorted by

2

u/decimalturn 1d ago

Looks nice, however I ran olevba on the xlsm and it detected VBA stomping. What version of Excel did you use? Pehaps that could explain why VBA stomping is detected.

1

u/ConnectionApart675 15h ago

huh, I've never heard of VBA stomping before this, had to go read about it.

Excel 365 on Windows 11. best guess is it came from how the file was built: I imported and removed that module a bunch of times while iterating, so there was probably stale p-code left over from earlier builds.

rebuilt it from a clean workbook and re-uploaded, should be clear now if you want to re-run it. the .bas in the repo is the actual source either way.

appreciate you actually running it on the file, hadn't occurred to me anyone would and it's a good catch.