CSC 221: Computer Programming I
Fall 2009

HW 1: Programming in Scratch


The following exercises will introduce you to many of the features of Scratch. In designing, creating, and animating your sprites, you will be learning the fundamental concepts of programming, which we will later transfer to programming in Java.

In completing these exercises, you are allowed and even encouraged to work with a partner. Having someone to bounce ideas off of and to learn from is extrememly helpful as you begin the process of programming. However, you must be careful that the work you complete and turn in is your own. This means that, even if the two of you work out a design together, each of you must enter your own version of the code. Also, be sure to acknowledge your partner in any collaborative work that you hand in.

  1. Create a new sprite of your own choosing. You might draw the sprite entirely from scratch using the Paint Editor, or perhaps start with a photo and edit it. Then, animate that sprite so that it performs a specific dance sequence when the green flag is clicked. For example, you might add a sequence of blocks to the script of that sprite so that it moves 50 steps to the right, does a full rotation, then moves back 50 steps to the left.

    Once you have tested the behavior of your sprite, add a forever loop that encloses the dance steps. Thus, clicking on the green flag should cause the sprite to repeat the dance move over and over (until the stop sign is clicked to halt the script).

    Next, modify your sprite so that it has multiple costumes. As it moves, it should rotate through the costumes, yielding a more natural looking animation. Once you have done this, make multiple copies of the sprite on the stage (using the stamp button at the top of the stage) to create a group dance.

    Save your sprite in a project named lastnameDance where lastname is your last name (e.g., reedDance).

  2. Create another sprite whose animation is controlled directly by the user. When the user clicks on an arrow key (either up, down, left, or right), the sprite should move a set number of steps in that direction. When the user clicks on the space bar, the sprite should rotate a set amount in the clockwise direction. When the user clicks the mouse, the sprite should teleport (i.e., move directly) to the location of the mouse. You may add additional movements controlled by other keys.

    Once you have tested your control over the sprite, modify it so that the sprite can leave a trail as it moves. When the user clicks on the number 1, the pen should be placed down, meaning that any subsequent moves will draw a trail behind the sprite. Likewise, clicking on 2 should pick the pen up so that no trail is drawn. You may use other keys to control the color and size of the pen, as desired.

    Save your sprite in a project named lastnameDraw where lastname is your last name (e.g., reedDraw).

  3. For this exercise, you will create a simple version of the old video game Frogger. First, create a sprite (call it the frog, although it need not look like a frog) and animate it so that it moves along with the mouse (i.e., it repeatedly moves to the coordinates of the mouse). Then add several identical sprites (call them trucks) that move horizontally, back and forth across the stage at even intervals. If a truck sprite ever touches the frog, a sound should be played. Tinker with the number, placement, and speed of the truck sprites so that it is challenging to move the frog sprite from the bottom of the stage to the top, and back again. Feel free to elaborate.

    Save your project under the name lastnameFrogger where lastname is your last name (e.g., reedFrogger).

  4. Download the drop project from the class code directory and load it in Scratch. This project simulates dropping balls that can drift randomly to the left and right as they fall. The balls are sticky, so they stop falling as soon as they touch the floor or another ball. First, experiment, with the constants used to control the changes in x and y coordinates. If you change the value -5 in the "change y by" block (it should always be negative, though), how does that affect the simulation? Similarly, if you change the value 10 in the "change x by" block, how does that affect the simulation. Discuss your answers with your partner.

    Modify the simulation so that it keeps track of the number of balls dropped. You should add a variable called "number of balls" that initially is set to 0 when the simulation starts. Then, each time a ball is dropped, that variable should be changed by 1. Also, modify the script so that it stops the script as soon as the balls reach the top of the screen. Hint: one way to do this would be to check to see where the ball is when it stops falling. If it's y-position has not changed, then that means it was touching another ball before it even dropped.

    Save your project under the name lastnameDrop where lastname is your last name (e.g., reedDrop).

  5. Download the dice project from the class code directory and load it in Scratch. Then, modify the code so that in addition to counting doubles, it also counts the number of times a 7 is rolled.

    In testing your modified project, you may have to simulate many rolls before you get even a few 7's. Make this process simpler by creating a new button, labeled "Roll 10 Times". When clicked, this button should cause the dice to be rolled 10 times, with the appropriate counters updated appropriately. For extra credit, generalize this by having a variable on the stage that defines the number of rolls to be performed at the click of a button.

    Save your project under the name lastnameDice where lastname is your last name (e.g., reedDice).

  6. Using many of the same ideas from the dice simulator, create a project that behaves as a simple slot machine. There should be three identical sprites on the stage, each with 4 different costumes. When the player clicks on a button, each of the slot sprites should spin a random number of times. If all three sprites end with the same costume, then the player wins. Otherwise, the player loses. A variable should keep track of the player's bankroll. The bankroll should start with 20 tokens. Each spin should cost the player 1 token. A winning spin earns 13 tokens. Have fun and be creative.

    Save your project under the name lastnameSlots where lastname is your last name (e.g., reedSlots).