1.11 Skill-builder: Ms. Pacman and loops

Over and over again

There are many times when you want to have a program do things many times in a row.

Remember when you had Otto the Robot repeat the same thing several times?

The programming structure that you use for this is called a loop.

For example

			
for (i=1; i<4; i++) {

	/*	this sets up a loop with a variable "i" with a starting value of 1
		anything inside the loop will trigger as long as "i<4" is true
		and it will automatically add one to i at the end of each loop
		
		So for the first loop
		...i=1,
		then i=2,
		then i=3

		...and then it stops when i=4, because "i<4" is no longer true
	*/	
	
	}
			
			
			

So knowing that, what will be the variable values for the following loops?

			
for (j=0; j<10; j++) {
	
	}
	
	
for (k=10; k>0; k--) {
	
	}
	
	
for (m=1; m<100; m=m*2) {
	
	}
	
	
			
			

Saving your work

Download the template and rename it to your last name, such as "1.11S-MsPacman-LastName.html".

You will also need to download a copy of the maze and Ms. Pacman as well to make it work.

The assignment

Look at the source code in the template of Ms. Pacman. Look at the loop structures in the function called mainProcedure. Each of these loops simply counts the number of times that Ms. Pacman is moved one pixel.

Modify these loop structures so that you can move Ms. Pacman step by step from point A to point B. Although the walls cannot block your movement (it is simply an image moving on top of another image), do not run into walls, Inky, Pinky, Blinky, or Clyde (yes, the ghosts have names).

Extend and expand

Can you modify the code to speed up and slow down the animation speed?

How would you change the direction that Ms. Pacman is facing in the animation?