Chapter 10 Algorithms.

Slides:



Advertisements
Similar presentations
Create a Simple Game in Scratch
Advertisements

Create a Simple Game in Scratch
 Functions breakdown: Functions purpose Modularity Declaring and defining a function Calling a function Parameter passing Returning a value Reusability.
Emerging Platform#5: Processing 2 B. Ramamurthy 6/13/2014B. Ramamurthy, CS6511.
Lets Play Catch! Keeping Score in Alice By Francine Wolfe Duke University Professor Susan Rodger May 2010.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
Lesson Three: Organization
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Locally Edited Animations We will need 3 files to help get us started at
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Creating a Simple Game in Scratch Barb Ericson Georgia Tech June 2008.
Mouse Inputs in Processing. Interacting with the Mouse mouseX and mouseY: pg mouseXmouseY –The position of the mouse in the canvas pmouseX and.
Lesson Two: Everything You Need to Know
Create a Halloween Computer Game in Scratch Stephanie Smullen and Dawn Ellis Barb Ericson October 2008.
Computer Science I Looping. User input. Classwork/Homework: Incorporate looping & user input into a sketch.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
Lecture 7 Conditional Scripting and Importing/Exporting.
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
Review Expressions and operators Iteration – while-loop – for-loop.
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Create a Halloween Computer Game in Scratch
MOM! Phineas and Ferb are … Aims:
Some of Chap 17.
Scratch for Interactivity
Emerging Platform#1: Processing 3
Classwork: Examine and enhance falling drop(s) or make your own.
Loops BIS1523 – Lecture 10.
Chapter 14, Translate & Rotate
Introduction to C Language
Chapter 8 Objects.
20 minutes maximum exhibits
Chapter 7 Functions.
Scratch for Interactivity
Chapter 9 Arrays.
Chapter 6 Loops (iteration).
Chapter 7 Functions.
Learning Objective LO: We’re learning to understand when it is appropriate to use particular data types.
For Net Art Lecture 2 J Parker
Mouse Inputs in Processing
Chapter 5, Conditionals Brief Notes
Chapter 10 Algorithms.
More programming with "Processing"
A Few Notes Starting August 29, 2018
Introduction to Problem Solving & Programming using Processing 2
Chapter 8 Objects.
Problem Solving Designing Algorithms.
Lecture 7: Introduction to Processing
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Problem Solving & Programming using Processing 2
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
LCC 6310 Computation as an Expressive Medium
Chapter 10 Algorithms.
Chapter 4, Variables Brief Notes
Creating a Simple Game in Scratch
Agenda for Unit 5: Control Structures
Chapter 9 Arrays.
Introduction to Problem Solving & Programming using Processing 2
Presentation transcript:

Chapter 10 Algorithms

In this Chapter… Learn how to construct a program that does more than one thing. Employ cumulative learning of drawing shapes, interactivity, variables, loops, conditions, functions, objects, arrays, and more. Think of what YOU want to do with your projects and beyond… The primary strategy is to break down into smaller programs; then integrate together in the end.

What to Expect The chapter uses a generic example that serves as a good guide for creating larger programs. It is a game; albeit simple. It involves: Interactivity Multiple objects (using arrays) A goal We will go through chapter in an incremental fashion.

A couple of definitions algorithm – A logical sequence of steps required to perform a task. pseudocode – An outline of a program, written in a form that can easily be converted into real programming statements. Algorithm example Allow users to interact by adding balls to screen Use different size balls, varying between small and moderate. Pseudocode example Add mouse press function with append to the array Use random for sizing between 10 – 50px More info…

The algorithm process Idea – Start with an idea Parts – break into smaller parts Write the pseudocode for each part Implement the algorithm with code Take the and functionality of each algorithm and build a class Integration – Integrate all classes into a larger program/algorithm.

An algorithm example – Sum a list of numbers: The algorithm might be: Create variable to hold Sum Create Counter and set it at 0 Create a loop that cycles through the array. Each time: Calculate the Sum of the first item Increase the counter by 1 Solution saved in Sum Display solution

Then translate algorithm to code by using while() or for() int [] nums = {3, 10, 1 }; int sum = 0; int i = 0; while(i < nums.length) { sum = sum + nums[i]; i++ ; } println(sum); text(sum, 10, 20);

This chapter breaks down the solution even more Developing an idea Breaking down idea Working out algorithm for each part Writing code for each part Creating algorithm for putting parts together Integration of parts. You should use this strategy in all of your projects…

Description of Rain Game The object of the game is to catch raindrops before they hit the ground. New drops will drop from the top of the screen from time to time. They will be located randomly across the screen. The player must catch the raindrop with the mouse before they hit the bottom of the screen.

Individual Parts of the Program Program a circle controlled by the mouse to catch rain A collision detection program. If the rain catcher catches a raindrop. A timing program that executes a function every N seconds Combine: A program with circles falling from top to bottom

Without a class would be void setup() { size(400, 400); } void draw() { background(255); stroke(0); fill(170); ellipse(mouseX, mouseY, 64, 64); INSTEAD… lets use Example 10-1 to create a Catcher class.

If ball hits edge, reverse direction Before doing the intersection test, let’s simply get the bouncing ball class. Note the pseudocode: x and y locations radius Speed for x and y Constructor: Set radius with argument Randomly pick location Randomly pick speed Increment x by speed in x direction Increment y by speed in y direction If ball hits edge, reverse direction Display by drawing circle at x and y location

The Bouncing Ball Class (in examples 10-2 & 10-3) Copy/Paste the class and examine line-by-line. Then type the main program. Reason…

First introduced on page 133 Another look at dist() First introduced on page 133

Compare distance with sum of radii If the distance between the center of circle1 and circle 2 is less than the sum of the radius of circle 1 and circle 2, then they intersect.

//on page 198, this function measures distance to see if intersecting. //it will be incorporated into Ball class boolean intersect(float x1, float y1, float x2, float y2, float r1, float r2) { float distance = dist(x1, y1, x2, y2); if (distance < r1+r2) { return true; }else { return false; } //on page 198, this boolean variable tests with specific objects ball 1 and ball 2 boolean intersecting = intersect(ball1.x, ball1.y, ball2.x, ball2.y, ball1.r, ball2.r); if(intersecting) { println("The circles are intersecting"); }

Example 10-4 uses the same functionality and logic by placing the intersect function into the Ball class.

Do Example 10-4 by modifying 10-2 …This is what you’ll be changing. In the Ball class: Add color variable: color c= color(100,50); Add the highlight() function Inside of display(): - change to fill(c) - add c =color(100,50) to reset color after ball displays Add the boolean intersect() function as shown on page 201 In the main page: Add an if statement for if intersect(); then highlight() For an extra challenge, have the word “POW” appear on the page when they intersect.

Timing The millis() returns the number of milliseconds since a sketch started. 1,000 ms = 1 second Example 10-5 Type example 10-5 from scratch. Or Look at the example I put on class website: called “Ex10-5 with Pictures” (We’ll cover this later in Chap. 15)

Example 10-6 with a Remix To save time, copy the Timer class from the textbook website. Discuss what’s happening with Timer class For main program, instead of changing backgrounds, display names of classmates. I will explain Download the text file from class website

Main program for example 10-6 remix

A 2nd example of importing text //Another Example of text display String [] people; int x= 0; int y= 10; void setup() { size(200,300); people = loadStrings("830class.txt"); } void draw() { fill(#3355CC, 20); rect(0,0,width, height); for(int i = 0; i < people.length; i++) { fill(#654321); text(people[i], 10, y ); y += 15; noLoop(); //keeps the names from repeating println("There are "+ people.length ) ; printArray(people);

This is example 10-7, but it’s best as a class float x, y; void setup() { size(400,400); background(100); x = width/2; y = 0; } void draw() { background(#ccff99); fill(#00ff55); noStroke(); ellipse(x, y, 16, 16); y++; Instead, type the class as shown on page 205. Then do Exercise 10-3 in order to use the class. No need to do the above in class because you did it for homework. Let’s talk about it.

Pages 206 – 207: 1,000 balls See pages 206-207. (No need to type this time) Notice all balls drop at once although it seems like they don’t. This is due to varied speed at random(1, 5). In 10-8 (from textbook), the totalDrops variable allows one to drop at a time.

Fancier Raindrop Type example 10-9 and let’s discuss what it is doing. Once the visually pleasing rain is designed, it can easily be incorporated into the drop class. Calculating size of circles 2 * 2 = 4 3 * 2 = 6 4 * 2 = 8 5 * 2 = 10 6 * 2 = 12 7 * 2 = 14