I searched everywhere in the forums and didn’t find any codes that have pixels “stacking” on themselves. So, I hope I didn’t reinvent the wheel by creating this code. It’s also my first PB pattern as I’m new to your world. I am fluent in PHP (and QBasic ) and have some experience with JS. So it’s not all foreign to me.
I’ve been working on this for a while now and have worked out most of the kinks myself. After several failed attempts of trial and error… along with reading tons of codes on this form… I’ve finally decided to ask for help.
Finally, here’s my problem.
Start the pattern and it appears to skip the first lap/cycle completely (Even after the pattern is complete and it starts over). On the second lap/cycle, it will light up and hold the last block, but it will always be red. This is because the array didn’t get update on the first lap, so the last pixel in the array is always set to 0. I believe this is because of two things.
- The pixel array is offset by 1 because the array starts at [0] and we start counting at 1.
-
pixel[pixels - 1] = h
is called in the ELSE portion of render, but not in the IF.
I’ve tried adjusting to the offset of the array by adding or subtracting 1 from different vars, but none of them fix the problem. Just just move the problem left or right.
I’ve also tried to add pixel[pixels - 1] = h
in the IF section of render. This enables the last pixel in the array to be captured, but then throws an array out of bounds error. This is because of how I’m counting… Say I have 16 pixels total. The array will be pixel[15] but on the first lap, it’s looking for pixel[16]. I’ve tried to adjust for this many ways without success.
I must be missing something here and I’m probably doing this the hardest way possible. I’m sure there’s codes I don’t know about yet that would make this cleaner and shorter. Feel free to tear it apart and tell me how I could (or should) have done it.
//Block Stacker by Nurples
//##### Settings #####//
//All settings are 0 ~ 1 value
speed = .005 //set the speed the block falls (Larger number = faster fall speed)
colorspeed = .1 //set the color change speed (larger number = slower color change)
BlockBrightness = .5 //set brightness for moving block (larger number = brighter)
StackBrightness = .5 //set brightness for stack of blocks (larger number = brighter)
//##### End Settings #####//
//set our vars
block = 0
counter = 0
pixels = pixelCount
pixel = array(pixelCount) //create an array to hold the stacked block's color
export function beforeRender(delta) {
block += delta * speed //Build the block speed
if (block >= pixels) {
block = 0 //Reset block location to the start
counter++ //Count how many times we've reached the end (laps)(hit the stack)
pixels = pixels - 1 //reduce pixel count each round to create the stacking effect
if(counter >= pixels){ //reset our lap counter
counter = 0
}
}
if (pixels < 0){pixels = pixelCount} // Reset pixels so our loop doesn't just go black
}
export function render(index) {
h = time(colorspeed) //Set the moving block color
s = 1 //Set the saturation
v = 0 //Set the brightness to 0 so unused blocks are off
if (index <= pixels){ //Decide if the falling block is above the stack
if (index == floor(block)){ //track the falling block
v = BlockBrightness //Set the brightness of the moving block
//pixel[pixels - 1] = h //Set the color of the top stacked block
}
}
else
{
pixel[pixels] = h //Set the color of the top stacked block
h = pixel[index] //Display the saved color or remaining blocks
v = StackBrightness //Set the brightness of the stacked blocks
}
hsv(h, s, v)
}
export var block
export var counter
export var pixels
export var pixel
I’ve added extra vars just to give users more flexibility. Once i get it working properly, I plan to make it so the block sizes can be changed to more than just one pixel. I also want to make it 2D so that the blocks can be wider than one pixel also. Feel free to throw tips at me for these idea, but please don’t give me the answers. I’ll never learn that way.
Thanks in advance!!!
Nurples