Using Lists and Functions to Create Dialogue

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

CS0004: Introduction to Programming Select Case Statements and Selection Input.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Calvin and Hobbes Teach Properties and Functions Created by Daniel MacDonald under the direction of Professor Susan Rodger Duke University June 2013.
Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008.
The switch Statement, DecimalFormat, and Introduction to Looping
Shorter of two objects and changing color Functions, events and setting the color Susan Rodger, Duke University June 2008.
Checking for Collisions Ellen Yuan Under the direction of Professor Susan Rodger at Duke University June 2014.
Line up By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Making a Timer in Alice.
Making a Boat Racing Game in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2010.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Shorter of two objects and changing color V2 Functions, events and setting the color in sequence and randomly This is a modification of the Changing Color.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
CREATING A TEST IN WORD 2007 Also creating and using equations in Word 2007 Jeff Klamm Tec 539.
Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
By Melissa Dalis Professor Susan Rodger Duke University June 2011 Multiplication Table.
A Simple Quiz: Ask User Functions. By Lana Dyck under the direction of Professor Susan Rodger Duke University June 2009, added Part 2 July 2011.
Making a Timer in Alice By Jenna Hayes under the direction of Professor Susan Rodger Duke University July
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Can I get your number? By Melissa Dalis Professor Susan Rodger Duke University June 2011.
1 Quiz Template: Using the ‘ask user’ functions By Deborah Nelson Duke University Under the direction of Professor Susan Rodger July 2009.
Balancing the scales: Inequalities By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Nonvisual Arrays by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Ready, SET, go! By Melissa Dalis Professor Susan Rodger Duke University July 2011.
31/01/ Selection If selection construct.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
By Melissa Dalis Professor Susan Rodger Duke University June 2011
The Essentials of Alice (Bunny)
Class-Level Variables in Alice
Getting Started With Alice: The Basics
Chapter 5: Structured Programming
A Simple Quiz for Alice 3.2:
Variables.
Comparing objects and changing color
Chapter 3: Variables, Functions, Math, and Strings
Chapter 3: Variables, Functions, Math, and Strings
The switch Statement, and Introduction to Looping
ECS10 10/10
Computer Programming I
Chapter 5: Control Structures II
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops
Changing Color, Using Text Objects, and Random Selection in Alice
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Let's Race! Typing on the Home Row
Microsoft Visual Basic 2005 BASICS
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Conditional Statements
Michelle Venable-Foster Barb Ericson Jan 2007
A Simple Quiz for Alice 3.2:
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Chapter (3) - Looping Questions.
Introduction to TouchDevelop
CIS 16 Application Development Programming with Visual Basic
Arrays
Introduction to Problem Solving and Control Statements
Using Functions
Tutorial for Arrays and Lists
Getting Started With Alice: The Basics
Relational Expressions
under the direction of Professor Susan Rodger
Restricting Events Creating Conditional Events in Alice By Henry Qin
Presentation transcript:

Using Lists and Functions to Create Dialogue

Using Lists and Functions to Create Dialogue Alice's default “say” method allows you to create dialogue, but it has limitations. For example, what if you wanted to give a user as much time as they needed to read a character's lines, but also wanted another user to be able to skip through them very quickly? One way to do this is to use a list, a counter, and a while statement. You'll learn to store a character's lines in a list, then let the user select which line to display based on a count variable.

Part 1: Crouching Tiger, Chatty Boar First, go to “add objects”. Go to “Animals” and add the Tiger and Boar objects to your world. These will be our conversation partners. Make sure they're reasonably close to each other, like above.

Now we'll create some dialogue for the boar Now we'll create some dialogue for the boar. Go to the boar's properties and create a new variable. Make it a string, check make a List, and call it dialogueList. Add the five lines shown in the image by clicking “new item” and then clicking on “default string” next to each item to edit it.

Now for the tricky part: making the boar actually speak Now for the tricky part: making the boar actually speak. We'll do this by having it say whatever line from its dialogue corresponds to the value of a specific variable. By changing the value of this variable, we can change the line said by the boar. Start by making a variable in world (NOT world.my first method!) called count. Make it a number and give it a starting value of 0.

Next, create an event to increment count by 1 when the enter key is typed. (Drag-and-drop count from the world properties page into the box that says <None>.) We will use count to keep track of the boar's lines. Now that we can change its value, it's time to make the boar talk. Create a while loop.

Go to the boar's methods, then drag say into the while loop Go to the boar's methods, then drag say into the while loop. Choose “Hello” to start. Go to the boar's properties and drag “dialogueList” into the box where it says “hello”. Select “ith item from list” on the menu that appears, then go to expressions and select count. This means the boar will say the item from dialogueList that corresponds to count. When count is 1, the boar will say item 1, etc.

If you press play now, the boar should speak! The line it's saying should change when you press enter. However, press enter too many times, and... !!!

As we have it set up now, if you increase count until it is a larger number than the number of the last item in the list, then Alice will still search the list for the item with the corresponding number, but will not find it and will crash. We need to change the while loop so that it will stop executing once count passes the last item in dialogueList. To do this, change the while loop's condition (the box next to while) so that the loop will only execute when count is less than 6 (the number of lines in dialogueList. Drag count in (from properties, under world) and select “count <”, then type in 6.

Congratulations, the world should now be working properly!

Part 2: Introducing Dialogue and Functions Now the boar is speaking properly. However, if you think you're up to some more challenging work, you can make this a proper conversation.

Go to the tiger's properties this time and create a new dialogueList variable. Make it a list of strings. Add 12 items. Write responses to the boar's dialogue, but only in odd items. Leave the rest as default string.

Now go to the boar's properties, delete its old dialogueList, and make a new one. Add 12 items, just like before. Put the boar's old dialogue back in, but only in even items. Leave the rest as default string.

Since 12 total lines (6 from the boar and 6 from the tiger) will now be said, we're going to increment count up to 12 this time. However, we will print boar lines only when count is even, and tiger lines only when count is odd. First, let's make an isEven function in world to check if count is even. Set its return type to boolean. Create a parameter for isEven. Make it a number called input.

input will be the number which we are trying to identify as even or odd. If it's even, the function should return true; if odd, it should return false. Start by creating an if/else statement that returns true if the condition is met and false otherwise. A common way of determining if a number is even is to see if it leaves a remainder when divided by 2. The IEEERemainder function under world lets you do this in Alice.

Drag an a==b (a is equal to b) function into the box next to “if” where it says “true”. For the first option, select input (under expressions), and for the second option, enter 0. Next, drag an IEEERemainder of a/b function into the box where it says input in the if/else statement. Select 2 from the menu.

Your if/else statement should now look like this. If it does, your function is complete!

Now to tie it all together. Go back to world Now to tie it all together. Go back to world.my first method and delete “count < 6” from the while loop you have. Add a second loop in the same format as the loop for the boar's dialogue in which the tiger says lines from its own dialogueList. Time to use that new function! Go to world functions and drag isEven over “true” in both loops. In the loop with the tiger, click the second drop-down arrow by isEven, which will open up a menu. Click on logic, then “world.isEven ==”, then false.

Click the drop-down arrows next to “world Click the drop-down arrows next to “world.isEven” in each condition to change “input” to count (under expressions). Finally, drag both loops into a third while loop. Drag count from world properties into the condition of this loop (where it currently says “true”), choose “count <”, and enter 12.

Congratulations!