Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.

Slides:



Advertisements
Similar presentations
Using Microsoft PowerPoint in the Classroom
Advertisements

Create a Simple Game in Scratch
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Pages and boxes Building quick user interfaces. learning objectives o Build a quick UI with pages and boxes o understand how pages and boxes work o click.
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
Constructor and New Fields // Don't synch draw() with vertical retrace of monitor graphics.SynchronizeWithVerticalRetrace = false; IsFixedTimeStep = true;
© Calvin College, Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create.
Processing Processing is a simple programming environment that was created to make it easier to develop visually oriented applications with an emphasis.
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
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.
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
Guide to Programming with Python
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
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
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Click your mouse for next slide Flash – Introduction and Startup Many times on websites you will see animations of various sorts Many of these are created.
Locally Edited Animations We will need 3 files to help get us started at
Programming Games Simulated ballistic motion: cannon ball. Classwork: Final day for midterm project and all projects for first part of class. Homework:
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
AD 305 Electronic Visualization I : School of Art and Design : University of Illinois at Chicago : Spring 2007 Intro to Action Script 3 "The games of a.
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Programming games Show your version of Bo the dog. Start cannonball Preview: video, audio work session (cannonball) Homework: Cannonball with ball in a.
Game Maker Terminology
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
Programming games Show shaking screen, quiz show. Calculations Homework: (Finish basic Javascript Projects) Make proposal. Work on project.
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
B. RAMAMURTHY Simulating Motion and Implementing Animation.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
CIS 3.5 Lecture 2.2 More programming with "Processing"
Research Topics in Computational Science. Agenda Survey Overview.
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Lesson Two: Everything You Need to Know
Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.
Programming Games Show your rock-paper-scissors. Demonstrate bouncing ball. Demonstrate and examine Bo the dog. Homework: Modify Bo to make your own.
Computer Science I Arrays. Parallel structures. Classwork/Homework: Build your own bouncing things.
Computer Science I Recap: variables & functions. Images. Pseudo-random processing.
Computer Science I Looping. User input. Classwork/Homework: Incorporate looping & user input into a sketch.
G RAPHICS & I NTERACTIVE P ROGRAMMING Lecture 2 More Programming with Processing.
AD 305 Electronic Visualization I : School of Art and Design : University of Illinois at Chicago : Spring 2007 Action Script 12 "The games of a people.
Computer Science I 3D Classwork / Homework: plan and implement your own 3D example.
Computer Science I Share plans for virtual something. Text. Show my virtual dog. Classwork: Plans for your virtual something. Homework: start implementation.
Computer Science I Variables. Methods. Classwork/Homework: Re-do or do new drawing using variables & function(s). Zip folder and upload to Moodle dropbox.
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa.
Computer Science I Text input. Transformations. Yet another jigsaw. Classwork/Homework: Create new application making use of transformations.
Computer Science I Go over midterm. Reprise on curves. Table examples. The map function. Testing Strings. Fonts. Classwork/Homework: Complete midterm project.
Computer Science I Looking at code: "Where did Prof. Shablinsky go" Classwork/homework: midterm projects.
Computer Science I More class examples. Paths. Jigsaw. Tolerance. Classwork/homework: Your class project. Post proposal for midterm project.
Time to upload Virtual something.
Computer Science I Libraries. Sound. Saving frames as images. Classwork/Homework: Make sketch using sound. Add saving frames.
Conditionals.
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009 Intro to Action Script 11 "The games of a people reveal.
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.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Sound and more Animations
MOM! Phineas and Ferb are … Aims:
Scratch for Interactivity
Classwork: Examine and enhance falling drop(s) or make your own.
Computer Science I Variables. Methods.
Chapter 10 Algorithms.
Computer Science I Fonts. Building on one jigsaw to make another.
Chapter 10 Algorithms.
More programming with "Processing"
Lecture 7: Introduction to Processing
Chapter 10 Algorithms.
Presentation transcript:

Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.

Animation is producing a succession of (static) pictures fast enough that our mind interprets the pictures as showing motion. – Visual music We produce animation in Processing by using the draw function. – Default frameRate of 60 times/sec. This is a goal. If draw has too much to do, may not be this fast. You can experiment. – Erase display, using background( ) each time.

Bouncing ball planning We know How to draw a circle at a given location, with horizontal and vertical chords the same and some set amount. We know how to erase the display. We don't quite know how to bounce… We do know how to check the value of a variable against another variable and take action depending on a condition.

Planning, cont. Define variables bx, by for the position of the ball and dx, dy for the increments (aka deltas) each frame. The setup function sets the values of the variables. The draw function does all the work – Changing the values of bx and by – Checking values against the wall values. Changing dx and/or dy in certain situations.

The if statement The if statement is used to check a condition and do something. – A condition uses various operators to return a true or false value: (dis > limit) (dis >= limit) (dis == limit) NOTE double equal sign!! (dis < limit) (dis <= limit) Combinations: ((xval >= b1) && (xval <= b2))

The = sign versus == The = sign is part of an assignment statement. The == sign checks a condition.

The if versus if-else (pseudo-code) if (today == 1) { do_something } if (today==1) { do_something } else {do_something_else}

Checking on hitting the walls THERE ARE NO WALLS AND THERE IS NO BALL. We write code to keep track of position of ball. My approach is to do the two horizontal checks together using a compound condition and the two vertical checks together using a compound condition. The if-true clause changes the delta for the NEXT iteration, that is, the next frame.

//bouncing ball float bx,by,dx,dy; int balldiam = 20; void setup() { size(800,600); bx = width/2; by = height/2; dx = 1; dy = 2; }

void draw(){ background(0); //erase window ellipse(bx,by,balldiam,balldiam); bx = bx+dx; if ((bx>=width)||(bx<=0)) {dx = -dx;} by = by + dy; if ((by>=height)||(by<=0)) {dy = -dy;} }

Improvement void draw(){ background(0); //erase window drawBallThenMoveAndCheck(); } void drawBallThenMoveAndCheck() { ellipse(bx,by,balldiam, balldiam); bx = bx+dx; if ((bx>=width)||(bx<=0)) {dx = -dx;} by = by + dy; if ((by>=height)||(by<=0)) {dy = -dy;} }

Why … Do I think this is an improvement? The name of the new method says what it does: – Draw ball, then… – Move ball, then… – Checks if the NEXT move should be different The draw method erases the screen and then does the action.

Fancy Random choice of color User (player) actions Pressing arrow keys changes velocity – In physics, velocity is a vector: speed + direction. – In our example, the dx and dy indicate the changes in the horizontal and vertical position. – Note: d stands for delta. Delta refers to change. These are conventions. We can (fortunately or unfortunately) name things whatever we want. – Need to see how to detect which key.

Random color Many ways One way: fill(random(255),random(255),random(255)); The random function returns a random floating point number from 0 up to but not including the parameter.

Random color Choose from a set list of colors Use the datatype color Use an array for the list of colors color[] colors = { color(223,115,255), //heliotrope color(114,47,55), //puce color(255,255,240) //ivory };

In setup Call random once to get an index into the array fill(colors[int(random(colors.length)))]); Match up the ( ) and the [ ] call of function fill, setting the fill color index into array colors call of int function to truncate to integer call of function random with the value colors.length Note: code will still work if I add another color.

void keyPressed() { //use arrow keys to change deltas (velocity) if (keyCode==UP) { dy = dy -.5; println("dy decreased, now ",dy); } if (keyCode==DOWN) { dy = dy +.5; } if (keyCode==LEFT) { dx = dx -.5; } if (keyCode==RIGHT) { dx = dx +.5; }

Classwork Get the plain example working and then modify (but keep it a bouncing something) – Draw something other than a circle. – Draw the walls. – Slow down or speed up when hitting a wall. – Do something when it hits a wall – Do something in response to mouse click – ? Look at the fancier thing. Can you just add the keyPressed method to your example? Do you want to?

Homework Do your own program involving – Use of random – Use of animation – Optional: Images Font(s) and text You must use variables and you must create your own functions (methods)