Goals

In this assignment we will practice the following computer science concepts:

Computer Science Principles Curriculum

Common Core Standards

Overview

In this assignment, you will write a program that plays a simple race between a tortoise and a hare. You will tell the tortoise and the hare where to run to during the race. Download this zip folder for a template to help you get started.

Understanding the Provided Template

Open the provided template and navigate to the main.quorum file.

There is an action already defined for you. This action begins on the line that starts with action Update(number seconds). This action updates your game window on each frame of the game.

Here is what the action looks like in its entirety on lines 44 through 47.

action Update(number seconds)
 hare:Update(seconds)
 tortoise:Update(seconds)
end

You write your code to move the tortoise and the hare around the race track in the action labeled action CreateGame. You can find this action starts on line 18 through line 22.

Here is what the action looks like, on lines 34 through 35.

action CreateGame
end

Goal 1: Calling Actions on Objects

Navigate to the CreateGame action on line 18. You write your code here. The first thing you need to do is to set up the background and Sprite objects for the race.

Example: Load and add the background Drawable.

racetrack:Load("racetrack.png")
Add(racetrack)

Activity: Load and add the tortoise and hare Sprite objects.


Once you load and add the objects, you can set the initial position. This is done using the SetInitialPosition action, passing the starting coordinates. Since the race starts at the bottom left corner, or (0, 0), this should be the initial position.

Example: Set the initial position for the hare Sprite.

// this line of code tells the hare that its initial position is the coordinate (0, 0)
hare:SetInitialPosition(0, 0)

Activity: Set the initial position for the tortoise.


Once you have set up your Sprite objects, give each a speed by using the SetSpeed actions. The SetSpeed action takes a number parameter to set as the speed. The speed of the Sprite objects is represented by how many pixels they will move across the screen in a second. This means that if the number you pass is too small, then you may have a hard time noticing the movement. If the value is too large, your image will appear to jump across the screen rather than have a smooth movement. Good values to choose are between 50 and 300.

Example: Set the speed for the hare by calling SetSpeed(number).

//This gives your hare an initial speed of 100 pixels per second
hare:SetSpeed(100)

Activity: Set the speed for the tortoise.


Your Sprite objects are now ready to begin racing! But they need to be told where to go. The race is to go counter-clockwise starting from the lower left corner to the lower right corner, to the upper right corner, to the upper left corner, and back to the bottom left corner.Tell the Sprite objects which coordinates to move to by using a series of MoveTo(x, y) commands, passing the x and y coordinates of the screen location they should run to.

Example: Tell the hare and tortoise Sprite objects to go to the position (1440, 770).

// this line tells the hare to move to the coordinate (1440, 0)
hare:MoveTo(1440, 0)
// this line tells the tortoise to move to the coordinate (1440, 0)
tortoise:MoveTo(1440, 0)

Activity: Write the additional MoveTo commands to finish the race.

The MoveTo actions take place in the order you give them in your program, so you should give them in the correct order so that the race goes in the proper direction for the tortoise and hare. Make sure that the tortoise and hare do not go off the screen and that they do not cut across the grassy part of the track. They should stick to the rectangular dirt border along the edge of the screen.

You can now compile and run your program. The race should pop up in a separate window. If there are any errors, fix them and try again.

Next Tutorial

In the next tutorial, we will discuss Assignment 2.5, which describes how work More Movement, Tones and L.E.D. in Quorum..