Log In  


Cart #44826 | 2017-10-01 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
9


I wasn't sure what forum this would apply to as it's an asset for others to learn from or use/Repurpose so I put it in the collab forum. Code is pretty compact and verbosely commented so even the very basic user of Pico-8 can get a grasp of what I did and how.

UPDATE: Updated to 2.0! Includes McBoffin's suggestions, And includes simple "Warp" effect as well now! Everything's broken up into functions for ease of copying the functions to your own program, And commented up the wazoo for ease of tinkering!

9


1

I like it! Super simple and super effective!

I noticed that on the first loop, the colors of the stars are matched to their speed (speed 1 to color 13, speed 2 to color 6, and speed 3 to color 7), but after that first loop, a new speed is chosen, but the color remains the same. So after a few loops, the colors and speeds are all mixed, instead of in strict layers. (Also, I noticed when a star was given a new speed, it wasn't being given an integer speed like the first time.)

You could keep that strict layering, and even lose an entire item of stored data by using the following code (my comments in ALL CAPS):

function _init()
 stars={}
 cols={13,6,7} --ADDED THIS TABLE TO ASSIGN COLORS TO SPEEDS

 local totstars=125 

 for i=1,totstars do

  local tc=flr(rnd(3)+1) --PICK A RANDOM SPEED AS BEFORE

  --BUT NOW SKIP THE WHOLE ASSIGNING COLOR BASED ON SPEED THING

  add(stars,{
   x=rnd(128),-- random "x" pos.
   y=rnd(128),-- random "y" pos.
   sp=tc     -- speed from 1-3.
             -- DON'T STORE THE COLOR!
  })

 end
end

function _draw()
 cls()
 for st in all(stars) do
  pset(st.x, st.y, cols[st.sp]) --GRAB THE COLOR BASED ON THE SPEED
 end
end

function _update()
 for st in all(stars) do
  st.y+=st.sp
  if (st.y>=128) then
   st.y=0
   st.sp=flr(rnd(3)+1) --CHANGED THIS TO GET A NEW *INTEGER* SPEED
  end
 end
end

Thanks! I'll update it a lil' when I get the chance tomorrow. Still learning my way around how this all works. For instance I didn't know about that bit you did above with cols[st.spd]. Learn something new every day, That's the goal!

Forgetting that FLR statement was the usual sort of oversight you run into when staying up too late for "Just one more routine!" thanks for catching that for me!


Thanks! always nice to see new takes on classics.


@penthouse_guy:

Every time I learn the nuances of a new language/scripting system the first thing I always try to do is a parallaxing routine because they're easy enough to do on most hardwares. Good way to test draw limits by dumping a ton of stars too.


1

I actually liked how the original looked better, as there was more variance in star speed, whereas the new version has 3 distinct layers/speeds. I would recommend changing @MBoffin's code just slightly so that instead of using flr() when choosing new speeds (in both places), you ONLY use flr() on the speed when indexing the color.

pset(st.x, st.y, cols[st.sp])

becomes

pset(st.x, st.y, cols[flr(st.sp)])

This way, you get the memory savings and different layers of color, but now the speeds are varied and more interesting to look at. You get speed/color categories instead of 3 layers with each star per layer moving at the same speed.


3

Hiya! Sorry to dig this up hahaha

I'm using this for my game (properly credited :) ) and went through the function to cut some tokens out for space. I also needed not only parallaxing stars but a simple static starry sky, so modified the function a little bit so that the warping is a function variable and can simply be static.

So I thought I'd share it here if you or anyone else would be interested in it. Thanks for the awesome snippet!

--I deleted all comments just so I could easily work with it. Look at the main cartridge's code for a commented version!

function _init_stars()

 stars={}

 warp=0 --just declaring
 starminx,starminy=0,0
 starmaxx,starmaxy=127,127

 metal={13,6,7}
 cool={1,12,7}

 local totstars=128

 for i=1,totstars do

  local rndspd=flr(rnd(3)+1)

  add(stars,{
   x=rnd(128),
   y=rnd(128),
   spd=rndspd
  })

 end

end

function _update_stars(_warp) --now if you want to call it, you have to put a "warp speed"

 if _warp<=2 then --just a soft block so that you can't go over 2. 
  warp=_warp      --Since I didn't use the high speed I also didn't mess with it.
 else
  warp=2
 end

  for st in all(stars) do

   if warp>0 then
    st.y+=st.spd+3*(warp-1)
   end

   if (st.y>=starmaxy) then
    st.y=starminy
    st.x=(flr(rnd(128)))
    st.spd=flr(rnd(3)+1)
   end
  end

end 

function _draw_stars()

 for st in all(stars) do

  if warp==2 then

   if st.spd==3 then

    line(st.x, st.y-6, st.x, st.y-10, cool[1])  --All this can probably be made into a for loop.
    line(st.x, st.y-1, st.x, st.y-5, cool[2])   -- But I had enough tokens already :)
    pset(st.x, st.y, cool[3])
   elseif st.spd==2 then
    line(st.x, st.y-1, st.x, st.y-2, cool[1])
    pset(st.x, st.y, cool[2])
   elseif st.spd==1 then
    pset(st.x, st.y, cool[1])
   end

  else 

   pset(st.x, st.y, metal[st.spd])

  end
 end
end


[Please log in to post a comment]