07-30-2022, 03:13 PM
Aye, the problem with the code was simply: There was no problem! I was simply curious to see how many people might end up banging their head all night over a trick question -- which is something which you guys end up having us poor devs do all the time. Somebody will report an issue, and at first glance, it appears to be an actual issue... Then, after spending *forever* digging into it, we finally realize, "Everything is working 100% as it should -- it's just that how it should work isn't how the user expects it to work!"
In this case, it's exactly as Mark (and Matt over at Discord, before him), has deduced: The explosion *is* working as intended -- the problem here is that we set the speed of the exploding pieces based upon the distance from the center. In this case, there's several transparent boxes created near the center of the screen, and as such, they only have a movement rate of about 0.4 pixels per loop. With a loop limit of 60, those blocks move all of 24 pixels per second, and have about 500 pixels to travel. Simple math tells us that they're not going to exit the screen for 20+ seconds -- which is *exactly* what we see happening in the program.
Visibly, it appears as if there's a glitch in the matrix -- the program runs, and within a few seconds all visible content is exploded off the screen, leaving it to appear to be doing nothing for the next dozen seconds. If you're an impatient programmer, it's easy to jump to a conclusion and say, "Welp, that's an endless loop!", as there's no visible confirmation of any changes happening inside the program or on the screen. In all actuality, the program *is* working 100% as intended; it's just that it's working with transparent boxes of pixels. Color the background as Mark has in his code above, and you can easily see what's going on here.
Lesson to be learned from this?
Not all code is "broken". Often, its just the user's expectations which is incorrect!
In this case, it's exactly as Mark (and Matt over at Discord, before him), has deduced: The explosion *is* working as intended -- the problem here is that we set the speed of the exploding pieces based upon the distance from the center. In this case, there's several transparent boxes created near the center of the screen, and as such, they only have a movement rate of about 0.4 pixels per loop. With a loop limit of 60, those blocks move all of 24 pixels per second, and have about 500 pixels to travel. Simple math tells us that they're not going to exit the screen for 20+ seconds -- which is *exactly* what we see happening in the program.
Visibly, it appears as if there's a glitch in the matrix -- the program runs, and within a few seconds all visible content is exploded off the screen, leaving it to appear to be doing nothing for the next dozen seconds. If you're an impatient programmer, it's easy to jump to a conclusion and say, "Welp, that's an endless loop!", as there's no visible confirmation of any changes happening inside the program or on the screen. In all actuality, the program *is* working 100% as intended; it's just that it's working with transparent boxes of pixels. Color the background as Mark has in his code above, and you can easily see what's going on here.
Lesson to be learned from this?
Not all code is "broken". Often, its just the user's expectations which is incorrect!