Computing in the Life Sciences CPSC 301, Term 2, Winter 2016–2017

Lab 02

Lab 02:

Help a hungry animal find some food

Due: Sunday, Jan 22, 9:00am MARK: / 17

Student 1
(Submitter) / Name
8-digit ID
CS Account
Student 2 / Name
8-digit ID
CS Account
Approximate time to finish (minutes) / Before Lab
In Lab
After Lab

Last week you experimented a little with the Scratch programming language. This week you will construct a simple simulation using Scratch.

1.  Objectives

After this lab you will be able to:

1)  Create or import images to serve as sprite costumes.

2)  Place a food sprite on the stage using the mouse.

3)  Animate an animal sprite to move to and eat the food.

4)  Explain in plain English what small Scratch programs do.

5)  Use Scratch conditional blocks (eg: the “if” block) to execute some Scratch code only under certain conditions.

6)  Use Scratch loop blocks (eg: the “forever” block) to execute some Scratch code repeatedly.

2.  Learn about Pair Programming

The remaining labs in the course will be completed using a technique called pair programming. Pair programming is more than just an excuse to let your partner do all the work every other week—it will save you both time and get you both better results if you both engage with the process.

1)  Before lab: Read through the Pair Programming in CPSC 301 handout from the course web page.

2)  Before lab: Watch the Introduction to Pair Programming video (10 minutes), and perhaps print the Pair Programming Do’s and Don’ts handout which summarizes that material from the video in one handy page.

3.  [/2] Get Some Sprites

You are going to simulate an animal which is moving around to find some food, so the first step is to pick a good animal and some appropriate food. In fact, you need not pick animal and food—the “animal” could be anything capable of movement, and the “food” anything that the “animal” wants to move towards.

1)  [ /1] Before lab: Find a small image on the web of the animal that you want to simulate. What is the animal (describe it with words; you need not include the image here), and at what web page can you find the image again?

Answer

Find a small image on the web of some type of food that the animal likes. What is the food, and at what web page can you find the image again?

Answer

Note: Many images on the web are subject to copyright, which means that you cannot use them for your own purposes without the consent of the copyright owner. To avoid copyright concerns, you can choose images which have explicit and flexible usage rights; for example, all the images on Wikimedia commons are licenced for reuse under a variety of rules (although many require “share alike”, which means that you have to share anything to which they are added). You can also use the advanced search feature on Google to search for content with reuse rights. More information about copyright in the Canadian university environment can be found at UBC’s copyright website.

2)  [ /1] In lab: Convert your animal image into a Scratch sprite through the following steps:

a)  Put the animal and food image files into your Lab02 directory on your local machine. Remember to use the Z drive if you are using the lab machines.

b)  Open a new Scratch project by going to the Scratch website and clicking on “Create” at the top. You will arrive in the Scratch project editor.

c)  Close the tip bar on the right side of the editor.

d)  The sprites area of the Scratch project editor is in the lower left corner. Click on the folder shaped icon beside “New Sprite” in the upper right corner of this area to create a new sprite from a file (do not click on the folder shaped icon on the far left which adds a new backdrop from a file). Find your animal image on your local machine and open it to create a new sprite with your image as its first (and only) costume.

e)  You can edit the image/costume if you wish (eg: shrink it). To edit a costume, click on the sprite in the sprite area, and then select the “costumes” tab at the top middle of the editor. The right side of the project editor will then show the image which is the sprite’s costume, which you can edit using the tools. The gray checkerboard in the background is not part of the costume; it is there only so you can differentiate holes in the image from white parts of the image.

f)  After making any changes that you want, you can return to the “regular” view by selecting the “scripts” tab in the top middle of the editor.

g)  Import your food image as a separate sprite by following the same procedure. Show the TA your two sprites.

4.  [/4] Some Scratch to Get You Started

As discussed in class, being able to read and understand programs or pieces of programs is a valuable skill: we know from research that learning by example can be very effective (it is almost certainly how you learned to write in your native language), and it will also help you to solve your own programming problems by borrowing and adapting code that has been written by other people (hint: perhaps sooner than you think). Unfortunately, we cannot directly judge whether you “understand” a piece of code. Therefore, we ask you to summarize what the following pieces of code do in plain English. When explaining these pieces of code, try to give the big picture not the little details. Your goal should be the programmers’ equivalent of summarizing the theme of a novel, rather than just explaining the plot. You may assume that the sprite containing these scripts or script fragments is called “Sprite1” if you need to refer to it.

1)  [ /1] Before lab:

Answer

2)  [ /1] Before lab:

Answer

3)  [ /1] Before lab:

Answer

4)  [ /1] Before lab:

Answer

5.  [/3] Put Some Food Somewhere

We need the sprite that will represent the food to appear at the point where we click the mouse. Before we click or when the food is eaten, the food sprite should be invisible (or almost invisible).

1)  [/1] In lab: Add blocks to the food sprite that put it wherever the mouse is clicked. If the mouse is clicked again before the food is eaten, then the food should jump to the new location (do not create multiple food items).

2)  [/1] Before lab: You have created a costume for the food sprite that is the food item that the animal wants to eat. But before the food is first placed by a mouse click or after the animal eats it, it should not appear on the stage. How can you make the food sprite invisible (or almost invisible)?

Answer

3)  [/1] In lab: Add scripts, costumes and/or sounds to the food sprite so that it is invisible (or almost invisible) after the green flag is clicked but before the mouse is clicked on the stage. Show the TA that your food sprite is (almost) invisible when you start the animation, appears when you click the mouse on the stage, and jumps when you click the mouse again.

4)  In lab: Save this Scratch project under the filename animal_eating.sb2 before you continue.

6.  [/4] Help your Animal Find and Eat the Food

The animal will move toward any food that is on the screen. When the animal reaches the food, the animal will eat the food.

1)  [/1] In lab: Add a script to your animal sprite that makes it move reasonably slowly toward the food sprite.

2)  [/1] In lab: What happens if the food and animal are not at the same location when the animation starts, but the mouse is never clicked on the stage?

Answer

If this behaviour is not reasonable for the animal, adjust your script accordingly. Do not use a stop-all block; the animal should be willing to chase the food again if the user clicks on the screen again.

3)  [/1] In lab: Add blocks to your food sprite that makes it disappear if the animal touches it. If the food is gone, the animal should stop moving. The food should reappear if the mouse is clicked on the stage again. If the food reappears, the animal should move toward it again.

4)  [/1] In lab: Add blocks to your animal sprite so that the animal expresses happiness (may say something or make a sound) when it eats the food. Add blocks to your animal sprite so that the animal expresses impatience if it cannot find food in 10 seconds.

5)  Show the TA that your animal feeding simulation does all the things listed above.

6)  In lab: Save this Scratch project (under the same filename animal_eating.sb2) before you leave the lab.

7.  [/4] Add More Features to Your Simulation

In lab or after lab: Now you will add more features to your simulation:

1)  [/2] Include more than one animal at a time. The food is eaten by the first animal that arrives (and so disappears until the mouse is clicked on the stage again). Make sure both animals respond appropriately when there is no food.

2)  [/2] Add blocks to your animal sprites so that they interact in some interesting way (eg: talk to one another, run away from one another, have a fight, …). How do your animals interact? How does that interaction affect their pursuit of the food?

Answer

3)  Save your Scratch project (under the same filename animal_eating.sb2) and remember to include it in your handin submission.

8.  Submission Checklist

Only one person in each group will submit the lab using the handin tool.

The assignment name you should use with handin to submit this assignment is form “lab02x” where x is the lower case last letter of your lab section, that is, a for section L2A, b for section L2B, c for section L2C, d for section L2D, e for section L2E, and f for section L2F.

While you may submit multiple times using handin (before the deadline), only your last submission will be graded. Therefore, you must submit and re-submit all the relevant files in a single zip archive. For this lab your submission archive should include

·  A completed version of this lab document. Do not forget to fill in the table at the top with your identity information, your partner’s identity information, and a rough estimate of how long each component of the lab took (to the nearest 15 minutes is fine).

·  Your Scratch project file animal_eating.sb2

Version: 13-Jan-17 CPSC 301: Lab 02 -- page 2
© 2016–2017 Jessica Dawson, Ian M. Mitchell, George Tsiknis