Lesson goal: Computer animation with functions and for-loops

Previous: Animate a single pixel | Home | Next: Animate with laws of physics

Functions, and their ability to run small chunks of code over and over again, are really useful to animate something more complicated than a single pixel. Why? Because we can use the function to draw a more complicated shape (like a person or car), then just use the function in a for-loop during the repeated drawing and erasing.

Now you try. Try fixing the for and color statements to make a pixel move.

Type your code here:

See your results here:

This code will not run. You have to fix the for x= for-loop to count over the screen positions that you want your pixel to move over (try starting at -100 and moving to 100). Your step size for this count will control the speed of the pixel. Next, select a color for which the pixel should be drawn in the color() line. Dismiss.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.