Mathematical Expressions, Conditional Statements, Control Structures

Slides:



Advertisements
Similar presentations
Repetition everywhere – comparing while in a method and as an event Susan Rodger Duke University July 2010 modified July 2011.
Advertisements

Lets Play Catch! Keeping Score in Alice By Francine Wolfe Duke University Professor Susan Rodger May 2010.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Repetition Structures
Fall 2009ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.
CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Fall 2009ACS-1805 Ron McFadyen1 Functions A function is a collection of statement, similar to a method, but a function is defined to return a value to.
Creating a 3D Interactive Story Prof. Susan Rodger Duke University Feb. 24, 2007.
Calvin and Hobbes Teach Properties and Functions Created by Daniel MacDonald under the direction of Professor Susan Rodger Duke University June 2013.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Line up By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running o Stored in.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley This week: Whew!!! The last homework was tough! The homework for this week.
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
GIRLS Robotic Camp. Let’s Begin Meet and Greet – Camp leaders introduce themselves – Students introduce themselves.
Making a Boat Racing Game in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2010.
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.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Checking for Collisions: Alternative Method Erin Taylor Under the Direction of Susan Rodger July 2015 Duke University.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Teaching a character to walk in more than one world: Parameters and Inheritance. By Lana Dyck under the direction of Professor Susan Rodger Duke University.
Four Fundamental Pieces Instruction Control Structure Function Expression.
Tutorial on Kangaroo visiting his animal friends - parameters, events.
Scene changes By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June
By Melissa Dalis Professor Susan Rodger Duke University June 2011 Multiplication Table.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops Susan Rodger Duke University July 2011.
Alice 3.0 A Guided Tour Mike R-D YRDSB. Agenda General Walkthrough of Alice 3.0 Loops, Variables, Threads Methods / Properties Arrays Misc.
Balancing the scales: Inequalities By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Functions Sec 8-11 Web Design. Objectives The Student will: Understand what a function is Know the difference between a method and a function Be able.
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Functions, Loops, and Parameters.
Repetition Structures Chapter 5 Part The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop.
Tutorial for Arrays and Lists. Description This presentation will cover the basics of using Arrays and Lists in an Alice world It uses a set of chickens.
Programming: Simple Control Structures
Simple Collision Detection By David Yan Under the direction of Professor Susan Rodger and Chari Distler Duke University, June 2015.
Creating a 3D Interactive Story Prof. Susan Rodger Duke University July 19, 2007.
Tutorial on Kangaroo visiting his animal friends - parameters, events Susan Rodger Duke University June 2009.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Obj: Programming: Simple Control Structures HW: Read section 3 – 2 AC3 D2 Do Now: 1.Log on to Alice. Open the file firstEncounter.a2w located in the folder.
5 Event Handling Interactive Programming Suggested Reading Interaction: Events and Event Handling, Supplemental Text for CPSC 203 Distributed this term.
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.
Programming: Simple Control Structures Sec 46 Web Design.
Repetition everywhere – comparing while in a method and as an event Susan Rodger Duke University July 2010.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean.
Princess & Dragon Part 3: A Knight Comes Riding In—Cameras & Events By Elizabeth Liang under the direction of Professor Susan Rodger Duke University June.
Jonathon Kuo Under the Direction of Dr. Susan Rodger
Creating a UFO Rescue Game in Alice
Programming: Simple Control Structures
Programming: Simple Control Structures
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops
Simple Control Structures
Control Structures
Repetition Control Structures
Let's Race! Typing on the Home Row
Creating a UFO Rescue Game in Alice
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Programming: Simple Control Structures
Doing things more than once
Programming: Simple Control Structures
Programming: Simple Control Structures
Using Functions
Computer Science Core Concepts
Programming: Simple Control Structures
Programming: Simple Control Structures
Presentation transcript:

Mathematical Expressions, Conditional Statements, Control Structures Alice Mathematical Expressions, Conditional Statements, Control Structures

Coverage Arithmetic Expressions Built-in Functions Conditional Execution If/Then Statements Control Structures Loops

Functions Alice provides a set of built-in functions Similar to methods but like a mathematical function Takes some input and computes some output E.g. SquareRoot(25) We say the function returns the value 5 and has an input parameter of 25 Built-in functions give us information about properties of objects and relationship to other objects (e.g. distance, position in world, width)

Functions Click on the functions tab after selecting an object

Arithmetic Function Example Make the alien on wheels move to another object If we use are not careful it will move right through the object We can experiment to find a good number More generally, we could change the distance it moves to the function that computes the distance between the two objects Drag the distance function over the “1 meter” box

Arithmetic Function Example Now the alien moves to the middle of the object; how to fix this?

Arithmetic Function Example Should move a distance: (distance to object) – (Object width)/2 – (alien width)/2 We can perform the math by clicking on the down arrow

If-Then Single Path If Statement To create, drag “If/Else” from bottom to the code window Is Condition True? True False Do This Action Single Path If Statement

If-Then-Else If / Else Statement Is Condition True? True False Do This Action Do Something Else If / Else Statement

If/Else Example If the alien is smaller than the object Else Move around it Else Move right through it Can resize the object to check both cases

Relational Operators Relational operators are functions of the world object A == B A is equal to B A != B A is not equal to B A > B A greater than B A < B A less than B A >= B A gte B A <= B A lte B

If/Else Example If the alien’s height < 2 then Else Move around it Else Move right through it Change the number 2 to test both cases

Repetition Sometimes you want to repeat some code multiple times Example: Make a flying cow Should wiggle front legs together as it flies, like a dolphin kick Remember how to do this?

Flying Cow Problem: We would like the cow to wiggle her legs a total of 5 times, but it is a pain to copy the turn code 4 more times

Repetition We can use a loop to repeat any block as many times as we like Use LOOP to repeat something some known number of times Use WHILE to repeat something while some condition is true Once the condition becomes false, the loop will stop

Version 1 Off a bit, why? Consider that we want the legs to move together like a dolphin kick, not running

Version 2 The leg turning is done at the same time; requires another “Do Together” block To try: Change Number of times to Infinity

Flying Cow Reminder: Can also move camera around in the scene

While Loop Is Condition True? True False Do This Action

While Loop Example Make the alien try to randomly get close to the object While (alien’s distance to the object > some value) Turn some random amount Go forward 1 meter Say “Found it!”

Random Roll While Loop code Many other versions possible

More Complex Loop If we click on the “Complex” loop then it shows additional loop options Most notably there is a loop “index” This is a variable that holds a number for the current iteration of the loop Sometimes we may want to do processing based on the index Loop index from NUM1 to NUM2 Loop body

More Complex Loop Example The opacity property sets how transparent an object is 0 = invisible 1 = fully solid Use the complicated loop to make the cow Fade from invisible to visible Fade from visible to invisible

Class Exercise Make a kangaroo jump onto a crate. The legs should make a hopping motion. Use the height/width of the objects to guide the forward and upward movements.

Class Exercise 2 Create a new event Let the mouse move any object Create a loop so the kangaroo turns toward the crate and moves toward it ½ meter until it is close to the crate, then it stops and says “GOT IT” Use the mouse to move the crate around; the kangaroo should chase it