Overview
Learning Objectives
Evidence Statements
Product Outcomes
Materials
Pens/pencils for the students, fresh whiteboard markers for teachers
Class poster (List of rules, design recipe, course calendar)
Editing environment (WeScheme or DrRacket with the bootstrap-teachpack installed)
Student workbooks
Language Table
Signs for kids, entitled "update-world", "draw-world" and "big-bang"
Cutout images of the dog and ruby
Preparation
Seating arrangements: ideally clusters of desks/tables
The Ninja World 3 file [NW3.rkt from source-files.zip | WeScheme projected onto the board
- So far, we’ve been working with three main functions to make our game and animate our world: update-world, draw-world, and big-bang. At this point, your game might use a few more functions than these, but Ninja World is still as basic as you remember it.
Open the Ninja World 3 file and press "Run", to watch the dog and the ruby fly across the screen.
What is the contract for each of the functions in this game? What do they do?
Each time update-world is called, What changes about the dog? How does it change? What about the ruby?
Where on the screen does draw-world put the image of the dog? the ruby? The clouds?
If you were to call update-world on (make-world 0 640), What would you get back? What is the world that is produced? Where are the dog and ruby after that world is evaluated?
In this version of Ninja World, both the dog and the ruby are moving as they should. But that’s old news: you’ve already made the characters in your game move on their own. Let’s add more.This lesson is another opportunity to have students"act out the three main functions in Ninja World. Draw a box on the board with (make-world 0 640) in it, labelled "world". Ask for a volunteer, and given them the update-world nametag to wear. Ask for update-world’s Contract and Purpose Statement. Go through a few iterations of having update-world evaluate the world on the board, and each subsequent world that they update.Make sure to call them by name, i.e. "(update-world (make-world 0 640))"
(In the first example, the student should erase the 0 and write a 10, and erase the 640 and write a 635. If they are stuck, refer them back to the code.) Take another student, and give them the big-bang sign. They’ll start the whole animation, and will have a timer. Instruct the class to yell "tick!" every five seconds, and when they do, big-bang will give the current world to update-world, who will then update it and replace it with the new world. Let this go on for a few iterations, so the rest of the class can see the world structure being changed while they count down. Finally, give the draw-world sign to another volunteer, along with the cutouts of the dog and ruby. As before, ask for their name, domain, range. When they are called, they will be given a world structure and will place the image of the dog and the ruby at the appropriate spots on the board. Change the value of the world back to (make-world 0 640). Call on draw-world a few times with different worlds, so the class can see the dog and ruby moving accross the screen. When each volunteer has practiced, put all the functions together: On each "tick" the class makes, big-bang will call on update-world to update the current world, and then draw-world to draw that updated world. Again, go through a few iterations, so the class can see the world structure changing and characters moving in as a result. - The time is nigh: we need to get that ninja cat onto the screen so that our game is playable. She’ll be able to move up and down.
Do we need to change anything in the code to make this work?
What part of the game will we need to keep track of if Ninja Cat is moving up and down? What would be a good variable name for this?
We need to keep track of the cat’s y-coordinate, so let’s add a catY to the world.Where in the code is the world struct defined?
If the cat is in the center of the screen, what should the value of catY be?
Add another number to the world struct, representing the cat’s y-coordinate.
- Right now, even though the cat’s y-coordinate has been added to the world struct, there’s no function to take in keypresses and make it move. This is what we’re going to write next. But before we figure out how to write the function to move the cat, we need to actually add her into the game.
Which functions will need to change, now that the world structure is different?
Since the world now contains three things, you’ll need to change every single make-world in the code.Starting from the first line, go through the code and look for every instance of make-world, changing it to reflect the new world struct.
How do you get the catY out of the world?
We said that we’ll be writing another function to handle keypresses and moving the cat, so just add the catY to update-world. Don’t worry about changing its value.
Inside draw-world, use the put-image function to place the image of Ninja Cat on top of the other images in the game. If she starts in the center of the screen, on what x-coordinate will she be placed? What about her y-coordinate?
After the catY is added to the world struct, but before any of the functions have changed, try running the simulation again, giving the ninja cat cutout to the draw-world volunteer. The world now has three things in it (dogX, rubyX, and catY), but the updating and drawing functions will change only the dog and ruby’s positions. When big-bang starts the animation, only the dogX and rubyX will be updated. After a few iterations, tell the volunteers that you’ve pressed the "up" key. If draw-world begins moving the ninja cat cutout, refer them back to the code. You haven’t programmed anything to change catY yet.