Pico-8 Bach Inventions Effects ~ Day 12: Scrolling Background (Patreon)
Content
Hi everyone!
I had to stop writing these for a few days but I'm back to it now!
The 12th portrait doesn't feature anything very impressive in terms of code, so instead we'll go over something super simple and super useful: making things 'scroll' in the background, using 'for' loops!
We will only look at scrolling things horizontally but doing it vertically is basically the same thing, and you can also do both at the same time, it's all the exact same logic!
First off we need our scroll value. Here I'll call it 'dx' and it will depend on the time. (in a game, you might want to make it depend on the player's position for example) Note that you could diminish that value on different layers of scrolling to create parallax!
Next, we have our 'x' 'for' loop in which we're drawing our scrolling elements. In this example it's particularly simple because we are always drawing the same sprites, at the same intervals, but you could use random - or predefined - sprites and offsets, by storing them during the _init() and then finding them back in our 'x' loop, with the index of any item depending on 'dx' and the interval of the 'for' loop.
Anyhow, the first parameter of our 'for' loop is the most interesting! In this example it's '-(dx%16)', with 16 being our loop interval. Just these 8 characters will offset the whole loop in a continuous, smooth fashion from frame to frame. The second parameter is 127, the last 'x' pixel coordinate possible on the screen. And the third parameter is of course our loop interval, 16. The line looks like this: 'for x=-(dx%16),127,16 do'.
In this loop you can draw anything you want. In the instance I've been describing, I put a 'y' 'for' loop inside of the 'x' loop and there I'm drawing the floor tiles. I have another 'x' loop which draws the pillars, only this one has an interval of 64.
And that's it! It's really simple but it can add a lot to an animation or a game, especially if you do several layers and use parallax!
Do ask any question you might have!
As usual, the downloads for this portrait are available to my 5$+ supporters over there!
Take care and have a nice week-end!
TRASEVOL_DOG