class logo

CS 122 - Day 3 Exercises

This page lists some problems for today's class. After today, you should be able to solve these problems on your own. Problems with this color are more challenging.

1) The Game

In this exercise we will create a simple game using Scratch. There are some new commands and concepts involved but the biggest focus is to learn to make your brain think like a computer so you can figure out how to implement the features on your own. The first section gives an overview of the game, it is followed by sections which break the tasks into more manageable chunks. I recommend trying to implement the game yourself after only reading the high level overview. If that seems intimidating, then look at the more detailed instructions.

We are going to implement a game when the cat runs around and collects fishes while trying to avoid the dog. There is a score keeper who keeps track of the score.

At the beginning of the game, the cat starts in the upper right corner of the screen, the dog starts in the lower left corner of the screen, and the 3 fish are randomly placed around the screen. The cat moves around collecting fish (by touching them) while the dog chases the cat. Each time the cat touches a fish, the fish disappears. If the cat gets all three fish, the score keeper announces that a point was scored, and all the sprites reset to their original locations. If the dog touches the cat, the score keeper announces that a point was lost, and all sprites reset to their original locations.

screenshot of game stage

The Cat The cat is controlled by moving the mouse. The cat points towards the mouse pointer and moves towards it slowly.

The Dog The dog just follows the cat. It points at it and moves slowly towards it.

The Fish The fish just sit there, and wait for the cat to touch them. When the cat collects a fish, that fish disappears until the round is over.

The score keeper The score keeper just stands around and waits for something to happen, and then announces when it does.

This game may sound daunting at first, but if you tackle it one piece at a time, I hope it will be pretty manageable. Once you get the basic version done, there are many ways to enhance it.

Show more detailed instructions

Show solution

Solution (Hide)

Download solution NOTE: my solution has a couple of bugs in it. I wanted to keep the code as simple as possible, so I let the cat and the dog keep moving after the round has ended. This can lead to the situation where the cat gets the last fish, then gets caught by the dog before it gets reset to it's original location. I decided it was better to have bugs like this than to have complicated code, but note that you can fix problems like these.

More Detail (Hide)

Let's start with the cat. We want the cat to follow the mouse pointer around. This is easier to do than you think, there is a command to point at the mouse pointer, then we can move a few steps, then we can wait a bit and do it again. Lather, rinse, repeat. I'd suggest moving between 5 and 8 steps each time, and waiting 0.05 seconds. Use the "repeat" command to do these steps over and over again.

The next step is to get the dog to follow the cat. This works in almost the exact same way, except that instead of pointing at the mouse pointer, the dog points at the cat. This is a place where naming your sprites (like cat and dog) will be helpful, otherwise it can be hard to keep track of which thing Sprite3 refers to... I recommend having the dog move at about half the rate of the cat, if it is much faster than that your game will be very difficult.

Cool, we're making good progress on the game, now lets add some fish. The fish just sit there and wait for the cat to touch them. When the cat does, they broadcast that fact and then disappear (using the hide command). Each fish can be constantly checking to see whether it is touching the cat in a repeat loop using an if command. When the fish is touching the cat, it should hide itself and broadcast a "fish collected" message.

The score keeper is triggered by the "fish collected" message. It should cause him to increment a variable that tracks how many fish have been collected (so he knows when the round ends). After incrementing the variable, he should check to see if the value in variable is equivalent to the number of fish on the screen (3 in my example). If it is, then he should broadcast the "round ended" message. All the other sprites should listen for this message (using the "when I receive" command) and should reset themselves when they get it. The cat and dog should move to their assigned location, the fish should show themselves using the "show" command.

Now we need to revisit the dog sprite. Every time the dog moves, it should check to see whether it is touching the cat. If it is, it should broadcast the "dog caught cat" message. The scorekeeper should announce this and broadcast the "round ended" message. Make sure the score keeper resets any other variables they are keeping track of.

That's all there is to it!

1.a) Evasive Fish

Have the fish move. Rather than just sitting there statically, have the fish wander around the screen. Perhaps the fish could slowly run away from the cat....

Consider adding random starting placement for the fish so they don't show up in the same spot each time. Look at the "pick random" command in the "Operator" category to do this.

1.b) Dog AI

Give the dog some smarts so it tries to go where the cat is going rather than where the cat is (Note, this one takes a little geometry and the use of some variables to track where the cat was so you can figure out where it is going.