The purpose of this project was to use lists to streamline code, especially in places where a single function is used several times on a set of objects. Using for loops and lists makes it possible to do a single operation on many objects with just a few lines of code. This type of algorithm was useful for this project where we drew pictures with moving elements. The elements move on a set track, so making several elements move at once was just a matter of putting those elements in a list and using a loop on that list.
I decided to make my moving image a simpler version of a ball factory:
I started by making a function, ramp_init, that draws a pair of ramps: one going up and one going down. I used the Zelle graphics library's Polygon function to draw a pair of triangles. Then I added those triangles, which are technically objects, to a list I called shapes. Then when want to draw the triangles I can simply use Zelle's draw function on that list. I essentially turned two drawing functions into one, which would save more time had I more shapes to draw.
Next I made a function to insert balls into my ramp machine, called ramp_animate. This function takes three parameters: the list of ramp shapes from earlier, a frame number, and the name of the window it will draw the picture in. When I ultimately call this function to draw my ball factory, do so in a for loop which calls ramp_animate for 100 consecutive frames, and each frame produces the next step in the animation. The ramp_animate function works like this: first it takes every element in the shapes list after the first two ( the ramps) and moves them relative to their current position using a set of if statements. Then ramp_animate draws two balls at the starting locations, the top of the first ramp and the bottom of the 2nd, and adds them to the list of shapes. Once the number of balls hits a certain point the function produces no more which I control with an if statement.
So ramp_animate runs something like this: it starts off trying to move the third shape on the list and all shapes thereafter, finding none it draws a pair of balls at the starting locations. For the next frame it runs again, this time moving that pair of balls and then drawing another pair. This continues until there are 11 balls at various places on the ramps. For the next frame it moves the first balls it created, now at the bottom of ramp one an the top of ramp 2, back to their starting locations, thus recycling the balls so the factory runs ad infinutum. The function knows when to move the ball back to its starting location, as opposed to moving it further up or down it's ramp, with an if statement conditional on the ball's location.
Thus I managed the somewhat complex movements of 11 balls by adding them to a list and having my function do things to the elements of that list based on their current positions. I could then make a ball factory made up of several instances of ramp_init, with an instance of ramp_animate working on each ramp_init. My final result, in gif form:
My biggest challenge in this project was in coding the movements of the balls precisely. It took much trail and error to make each ball move exactly where I wanted it when I wanted it there. I learned that using a pen and paper can be the best way to work out in my head the exact algorithm I hope to achieve before putting it into text wrangler. I also learned that lists can be very useful for applying functions to batches of inputs. Thank you to Kyle for helping me get past one of my biggest bugs which was figuring out I had to make my ramp_animate function move the balls before making the first pair, in order to make that pair first appear where I wanted them to.