US 12A — Computer Games — Lab #1

Sept. 29, 2008

Before Your Lab on Monday

Watch or read the following Scratch intros:

At the Start of Your Lab

Log onto a computer in the lab, either with your ICS account, or with a temporary, shared account, with username ics-temp and password Anteat3r.

During Your Lab

  1. Start up Scratch.

  2. The goal today is to get a version of the classic Pong game running in Scratch. This game has two sprites, a ball and a paddle. When you start Scratch, you'll see one sprite, Scratch the Cat, whose name is Sprite1. Change his name to Ball, as that will be his function today. (Don't worry, no animals will be hurt in the making of this computer game.)

  3. Now you need a paddle sprite. Click on the "Choose new sprite from file" icon (in the middle of the right third, it looks like an opening folder). Choose any sprite. When you return to the main screen, you'll see its name is Sprite2. Rename it to Paddle.

  4. Note that you can make a sprite larger or smaller by clicking on one of the "four arrow" buttons (to the right of the scissors) and then clicking on a sprite a few times. Adjust Ball and Paddle so that the sizes seem right to you.

  5. The job of Paddle is to respond to arrow key presses, and move accordingly. The job of Ball is to move around the stage (that's Scratchese for the game area) and bounce off the edges and the paddle.

  6. Let's work on Paddle first. Click on the Paddle sprite so that its "Scripts" area is showing in the middle of the Scratch window. In the upper left, click on the blue Motion button. On the left you'll see lots of options that can control the motion of a sprite. Drag "move 10 steps" to the Scripts area and double click on it. Try some different numeric values (including negative ones) and observe what happens. Can you use this command to make Paddle go up and down?

  7. No. Try another approach: drag "change x by 10" onto the Scripts area, and try different numbers. Then experiment with "change y by 10". That's the motion we want; the next step is to put this motion under control of the arrow keys. To do that, look in the Control and Sensing panels to find the commands you need, and assemble this block in the scripts area:
    when GREENFLAG clicked
    forever if key right arrow pressed?
        change x by 10
    To make this block of commands run, click on the green flag (instead of double clicking on the block of commands).

  8. Use the stamp tool (to the left of the scissors) and make three copies of this block, so that the four arrow keys all make Paddle move in the desired direction. (An alternative to the stamp tool is to right click and select "dulicate." You'll have to experiment with how much to move by.

  9. Now double click on Ball so that you can put together its script. To start, it should look like this:
    when GREENFLAG clicked
    point in direction "pick random from 0 to 359"
    forever
        move 7 steps
        if on edge, bounce
    That should get Ball moving, but it doesn't interact with Paddle. Here's the code to make that happen:
        if touching Paddle?
            point in direction direction - 180
    The second direction in the line above is a variable on the Motion pane, which you can drag into a subtract block from the Numbers pane. This block goes inside the forever block you put together above, at the end (after "if on edge, bounce").

  10. This script may not work perfectly; especially if you have an irregularly shaped Paddle you may see some funny bounces of the Ball. You can try putting
            move 7 steps
    at the end of the "if touching Paddle?" block. That should help the Ball bounce away from the Paddle so that the "if touching Paddle?" question is no longer true.

  11. At this point you have the basic bones of Pong, but it certainly isn't a Pong game. Your assignment for the remainder of the lab is to do some of the following:

  12. Congratulations, programmer! You've accomplished a lot today. Make sure the TA sees your final Pong program before you leave. If you want to save your Pong, your best bet is to use a USB drive, or save it to the desktop and then email it to yourself as an attachment with a Web email client. Then delete your file! All files stored on lab computers will be deleted at night.

Printing in the Labs

You won't have to do any printing for US 12 labs, but you may want hard copies of other work. Printing in the ICS labs costs 10 cents a page, and requires a print card. More information is here.

Using Scratch at Home

You will probably find it convenient to have Scratch at home or on a laptop.

  • Download Scratch (filling in the information on the first page you see is optional)