Goals

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

Computer Science Principles Curriculum

Common Core Standards

Vocabulary

Overview

In this assignment, we will write a program to create a dialog between a player and a NPC (Non-Player Character). Depending on the player's choices, the program will put different objects on the screen. We will use a KeyboardListener and a MouseListener as well as several User Interface elements like Button or Label for the interaction between the player and the NPC. We will use different actions with parameters to write this program. A parameter is a variable or an object that is passed to an action which allows the action access to the variable/object. Download this Dialog Tree Program Template to complete in this lesson.

Goal 1: Write action headers

An action header will mark the beginning of the action and give important information about the action. It defines the action name, the number and the types of parameters the action requires, and the type of the return value if there is one. Our actions for this lesson should be written after the end of the CreateGame action. The word end marks the end of an action.

Example: the action ChooseShape() header

This example shows how to write the ChooseShape action header. You must put the word action first then the name of this action with the different parameters in parentheses. You can also add a return value by putting, after the parentheses, the word return and the type of the return value (this is not necessary in our case).

// this action will change the shape put on the screen depending on the boolean parameter (true for a box and false for a cylinder)
action ChooseShape(boolean shapeType)

end

Activity: Write action headers

In the main.quorum class, you must create 3 new actions which are ChooseShape, ChooseColor and ChooseItem. To do that put the term action before the name of this action. After the name you must define the different parameters in bracket (their type then their name).

Goal 2: Implement actions

Now we have to define the operation of this action. We write the code for everything our action will do between our header and the word end.

Example: Implementing the action ChooseShape()

This example shows how to implement the ChooseShape action. The first line of this action will remove the old model and create a new one at the same position. This is necessary, because if we don't do this the old model will stay on the screen above the new one. Next, we use the a new model to load a box or a cylinder depending on user choice (which we get from the parameter of this action). To finish, we change the variable model with the new one, add it and show it. Since we need to know the shape choice in other parts of the program, we save it in a class variable (shape).

//This action is used to give to the player the choice of shape he wants
action ChooseShape(boolean shapeType)
    Remove(model)
    Model newModel
    newModel:SetPosition(8,-15,10)

    if shapeType                    // equivalent to : if shapeType = true
        //Load Box
        newModel:LoadBox(3,3,3,color)
    else
        //Load Cylinder
        newModel:LoadCylinder(3,3,3,color)
    end
    model = newModel
    Add(model)
    model:Show()
    shape = shapeType
end

Activity: Build different actions

In this activity you will write 3 actions: action ChooseShape(boolean shapeType) has been written for you in the example above. Use that example to implement the remaining action, action ChooseColor(text colorName). In this action, you will change the color of the model depending on the text parameter. You have to do two things:

Next, write action ChooseItem(integer item)

In this part, you will put an item on the screen which will replace the previous shape. This item can be any of three objects (a sword, a stick, or a bow) but each item will have the same color and shape as the previous shape. In fact, it will be made of several shapes which are either boxes or cylinders in the chosen color).

Goal 3: Call actions

Now that we have prepared our actions we have to define where the program should use them. To do that we "call" the action where we need to use it. It means that the action will be executed when the program reads the "call".

Example: Call an action

This example explains how to "call" an action: ChooseShape() in this example.

// This is an example of how to call an action
me:ChooseShape(true)

Activity: Call different actions

We have created 3 different actions which change, respectively, the shape, the color, and the item chosen by the user. Now we must decide where to use them. Most of these calls will be in the ChangeStep() action at the end of the main.quorum class given in the template. This action defines the different steps of the dialog. For every step, it shows the element we need and hides the others, changes the text, and puts the label and the button and all that is associated to a particular step on the screen.

Sample Output

Now that you have done the three headers, the three implementations and called the three action you can run your program and verify that it works. The program should be a dialog with different choices that the user has to make either with the mouse or the keyboard. A shape will be put on the screen, then a color will be put on the shape, and finally, an item will be created using several of the chosen shapes with the chosen color.

Collaboration Activity:

Collaboration is an important part of programming. When you work with other programmers, you get other points-of-view that make your programs even better. The final part of this assignment requires Git, a great tool for collaborating. Head over to the Teams and Version Control Tutorial if you need to know how to use Git.

When you write your commit message, it should be short but indicate the changes that have been made. Think of it like an important email. Email and text messaging have created new ways for us to communicate and collaborate. Write a post on social media or email a friend about the benefits of SMS and email. While you are sharing, share your Quorum Game. Invite your friends to download and play your game.

Next Tutorial

In the next tutorial, we will discuss Challenge 4.1, which describes an introduction to line following with robots..