Chapter 10 Algorithms.

Slides:



Advertisements
Similar presentations
Create a Simple Game in Scratch
Advertisements

Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming.
 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.
A Christmas Scratch game
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Loops We have been using loops since week 2, our void draw(){ } is a loop A few drawbacks of draw() –It is endless –There is only one draw() –It updates.
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.
Games and Simulations O-O Programming in Java The Walker School
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.
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
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.
Review Loops – Condition – Index Functions – Definition – Call – Parameters – Return value.
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 Recap: variables & functions. Images. Pseudo-random processing.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
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.
LO: We’re learning to outline a program using Pseudo Code.
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.
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
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:
Scratch for Interactivity
Emerging Platform#1: Processing 3
Classwork: Examine and enhance falling drop(s) or make your own.
Loops BIS1523 – Lecture 10.
Chapter 8 Objects.
Building Java Programs
Building Java Programs
20 minutes maximum exhibits
Chapter 7 Functions.
Chapter 9 Arrays.
Chapter 6 Loops (iteration).
Chapter 7 Functions.
For Net Art Lecture 2 J Parker
Chapter 10 Algorithms.
Mouse Inputs in Processing
Chapter 5, Conditionals Brief Notes
Chapter 10 Algorithms.
More programming with "Processing"
Chapter 8 Objects.
Problem Solving Designing Algorithms.
Lecture 7: Introduction to Processing
Lecture 11: Classes AP Computer Science Principles
IAT 265 Lecture 2: Java Loops, Arrays Processing setup, draw, mouse Shapes Transformations, Push/Pop.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
LCC 6310 Computation as an Expressive Medium
Chapter 4, Variables Brief Notes
Creating a Simple Game in Scratch
Agenda for Unit 5: Control Structures
Chapter 9 Arrays.
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 bit by bit.

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.

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 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.

The Smaller Parts *Think about elements (nouns usually) *Think about behavior (verbs usually) 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 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. The algorithm for it is: 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

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. 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 function tests it with a 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"); }

Take a look of my copy of adding intersect() & intersecting() to the main page… Copy your example 10_2 folder and duplicate it. We will be modifying it. Open your example10_2 and add intersect and intersecting to the main page.

Do Example 10-4 by modifying 10-2 FYI, this is what you’ll be changing. In the Ball class: Add color variable: color c= color(100,50); Highlight() function Inside of void display() add fill(c) and c = color(100,50); 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.

We will do example 10-5 instead. Timer The millis() returns the number of milliseconds since a sketch started. * One millisecond is 1 one thousandth of a second. * So 1,000 ms = 1 second * 300 ms = about a 3rd of a second   An example is to change the background every 2 seconds. void draw() { if (millis() > 2000 ) { background(0); } } We will do example 10-5 instead.

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("830people.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.

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) and frameRate; 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 nicely be incorporated into the drop class.