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:

15
 
1
function draw_person(x,y,c)
2
 color(c)
3
 width(2)
4
 circle(x,y,5)
5
 line(x,y-5,x,y-15)
6
 line(x,y-15,x-5,y-25)
7
 line(x,y-16,x+5,y-25)
8
 line(x-10,y-10,x+10,y-10)
9
end
10
11
pcls(0)
12
for x=0,100,10  do
13
  draw_person(x,0,4)
14
  pcls(0)
15
end

See your results here: