Neverending loop - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Neverending loop (/showthread.php?tid=702) Pages:
1
2
|
Neverending loop - SMcNeill - 07-30-2022 So here's one that has me scratching my head, that maybe you guys can take a look at with a fresh set of eyes and sort out: Code: (Select All) Screen _NewImage(1280, 720, 32) This is supposed to be just a simple little routine which explodes a portion of the screen off the screen. It works as intended, except for the simple fact that it doesn't know when to stop working, resulting in an endless loop! Our main logic here comes from this little snippet of code: Do Cls , 0 finished = -1 For x = 0 To ax For y = 0 To ay Array(x, y).x = Array(x, y).x + Array(x, y).changex Array(x, y).y = Array(x, y).y + Array(x, y).changey If Array(x, y).x >= 0 And Array(x, y).y >= 0 And _ Array(x, y).x <= _Width And Array(x, y).y <= _Height Then finished = 0 _PutImage (Array(x, y).x, Array(x, y).y)-Step(pw, ph), Array(x, y).handle, 0, (0, 0)-(pw, ph) Next Next _Display _Limit 60 Loop Until finished Our DO loop. We clear the screen Set a flag for being finished The FOR loops change the X/Y coordinates IF we still have an X/Y coordinate on the screen, then we're not finished Draw the image in its new positon NEXT Display LOOP until finished ********************************************* So the question becomes, "Why isn't this simple logic working?" We set the flag by default every time, and only if we draw on screen do we clear that flag... Why is this running as a non-terminating loop? Enquiring, tired old eyes are going to bed, and hoping that maybe someone here will figure out what the heck is going wrong with such a simple process. RE: Neverending loop - Pete - 07-30-2022 It worked for me by changing line 7 to... _PRINTSTRING (284, 328), "Mediocre!" Okay, It does finish for me, but it takes about 20 seconds of looking at a blank screen before that happens, and "Finished" is in a very small font, displayed in the upper right left of the window. Oh, on my system, I can't get the height of the window to fit on my desktop. Pete RE: Neverending loop - Pete - 07-30-2022 Better? Code: (Select All) SCREEN _NEWIMAGE(1280, 720, 32) RE: Neverending loop - bplus - 07-30-2022 It does finish but takes longer than expected and never expected little tiny bitty message in red in top left corner. What Pete said! ;-)) This explains what happened visually by making something invisible, visible, I think? Code: (Select All) Option _Explicit RE: Neverending loop - bplus - 07-30-2022 Well I'll be @PhilOfPerth I can not Select All of the code I just posted! But I can Select All Pete's code. RE: Neverending loop - Pete - 07-30-2022 Good idea, Mark! I just tried mine by replacing Steve with blocks. You can hardly tell the difference! so a string of CHR$(219)s blows apart much like your background demo. In mine, by cutting the width by 4 and height by 2 only a few pieces of blocks are left on the screen, but the lettering (if substituted back in) would be gone. In any event, waiting until all the character particles (solid blocks or letters with a background) are off the screen simply takes too long, even if you remark out _LIMIT. Pete RE: Neverending loop - SMcNeill - 07-30-2022 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! RE: Neverending loop - bplus - 07-30-2022 Yeah, yesterday I started pitching a fit expecting a Typo to work as if it were typed correctly. LOL! (now) Option _Explicit didn't catch it because lblN was typed lblT and that was another DIM'd variable. RE: Neverending loop - Pete - 07-30-2022 Ben Franklin once said something along the lines of don't ruin an apology with an excuse. Hmmm, now why did this remind me of that??? I wonder who said, "Whatever you have to tell yourself to wake up in the morning." Maybe that was Mrs. Franklin? LOL at my estimate of taking 20sec to finish, and your calculation of 20 seconds to finish. In high school, I estimated my way to a A in algebra, too! Anyway, nice gag. Too bad you and Matt didn't catch any suckers this time around. Oh, and kudos to Mark for "visualizing" the unseen. Pete RE: Neverending loop - Kernelpanic - 07-30-2022 Quote: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... Yes, . . . I can there only think of one thing : |