ecter

hi, hello, have a nice day

  • whatever you want

Neocities (currently empty)
ecter.neocities.org/
Discord (cuz you follow me)
ecter0413#0

posts from @ecter tagged #adventure games

also:

YellowAfterlife
@YellowAfterlife

I bet you've already heard about details+summary trick, or maybe even fancier tricks.

But what if you want to make a visual novel or a text-based adventure?
Nesting a pile of elements is kind of high-effort, especially if you want multi-directional transitions between them.

Fortunately, there's a simpler solution: divs in a scrollable div. Take a look:

You are in a dungeon.
You are in a dungeon.
There's a painting on the wall, a cabinet, and a door.
A sturdy wooden door. It is locked.
It's an oil painting of eggbug. It is art.
A wooden cabinet. Something sparkles inside.
A golden key! You put it in your pocket.
You are in a dungeon.
There's a painting on the wall, a cabinet, and a door.
You have a key.
It's an oil painting of eggbug. Perhaps you will meet again...
A wooden cabinet. You already took the key from it.
A sturdy wooden door. It is locked, but you have a key now.
You did it! You escaped the dungeon!
:eggbug:
Click here to restart

Neat, yeah?

Here's how it works
  • We have a container div for our game. It should have a position: relative style.
    It'll be a point of reference for setting sizes for the rest of the stuff, basically.
  • Inside it, we have a div with overflow: hidden style.
    This will prevent a scrollbar from appearing and spoiling the whole game to the reader.
  • Inside that, we have a div with display: flex (so that elements stack horizontally inside) and width that's large enough to fit (number of pages * 100%).
    Here, I have 11 pages and I don't like weird numbers so I used 2000%.
  • Inside that, we have the actual pages, each of which should use width that comes out back to 100% after being multiplied by the width of the container (so, in this case, 5%).
  • Pages have id attributes.
    Cohost will prepend your id with user-content-.
    I also suggest prepending your id with something unique so that different games don't fight each other on a page.
  • Links point at those ids - for example, href="#user-content-mygame-start" for a page with id="mygame-start".

As result, when you click on a link, the browser tries to bring the linked element into view, which scrolls the container to the said page. Since each page is exactly container-sized, there isn't much ambiguity as to what to do.

We do, however, have to account for the header's height (always 4rem tall?), which I added as padding to pages here. I bet there's a better solution.

This is simple enough that it can be written by hand, though I still recommend using prechoster so that you don't have to copy-paste your page style.

Thanks for reading!


ecter
@ecter

Just escaped the dungeon. It's a cool demo, i like it <3