Home Artists Posts Import Register

Content

We are releasing one Bach Invention portrait every day for 15 days, with Chris (Gruber_music) who remade the musics in Pico-8. (I can't prove it yet, but I'm fairly certain he used some form of black magic)

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

Files

J.S. Bach's Invention No. 12 in A Major, BWV 783 (Pico-8 Edition)

Invention No. 12 in A Major, BWV 783 composed by J.S. Bach (1685-1750). Music arrangement by Gruber (@gruber_music) Artwork/Animation/Code by Trasevol_Dog (@TRASEVOL_DOG) Support us on Patreon! https://www.patreon.com/Gruber99 https://www.patreon.com/trasevol_dog Arranged and animated in Pico-8. Pico-8 is a fantasy console for making, sharing and playing tiny games and other computer programs. https://www.lexaloffle.com/pico-8.php

Comments

Tim S

Does the loop interval set to 16 mean that every 16 frames the background repeats? Or is it more that it repeats every 16 pixels and dx can make that as fast or slow as you want?

punkcake

It's the latter! It doesn't have to be about repeating though, since you can also vary the tiles and offsets, and you can even use this pattern with the map() function. 16 is just a step value, while dx has the actual control over the motion.