Animation Piech, CS106A, Stanford University
Our story so far… Piech, CS106A, Stanford University
Our story so far… Piech, CS106A, Stanford University
Learning Goals 1. Write animated programs 2. Center an object Piech, CS106A, Stanford University
You will be able to write Bouncing Ball Piech, CS106A, Stanford University
Learning Goals For Me 1. Speak slower Piech, CS106A, Stanford University
But First! private void run() { int x = 6 – 4 + 7 * 3; println(x); int y = (6 + 4 + 7) * 3; println(y); int z = 6 / 2 * 3; println(z); } Piech, CS106A, Stanford University
Move GRect Piech, CS106A, Stanford University
Animation Loop private void run() { // setup while(true) { // update world // pause pause(DELAY); } } Piech, CS106A, Stanford University
Animation Loop Make all the variables private void run() { you need. Add graphics // setup to the screen. while(true) { // update world // pause pause(DELAY); } } Piech, CS106A, Stanford University
Animation Loop private void run() { // setup The animation loop is a repetition of heartbeats while(true) { // update world // pause pause(DELAY); } } Piech, CS106A, Stanford University
Animation Loop private void run() { // setup while(true) { Each heart-beat, update // update world the world forward one frame // pause pause(DELAY); } } Piech, CS106A, Stanford University
Animation Loop private void run() { // setup while(true) { // update world If you don’t pause, // pause humans won’t be able pause(DELAY); to see it } } Piech, CS106A, Stanford University
Move To Center private void run() { // setup GRect r = new Grect(0, 250, 100, 100); r.setFilled(true); add(r); while(true) { // update world r.move(1, 0); // pause pause(DELAY); } } Piech, CS106A, Stanford University
Gravity Ball Piech, CS106A, Stanford University
Gravity Ball First heartbeat Velocity : how much the ball position changes each heartbeat Piech, CS106A, Stanford University
Gravity Ball First heartbeat vx vy The GOval move method takes in a change in x and a change in y Piech, CS106A, Stanford University
Gravity Ball Second heartbeat vx vy Piech, CS106A, Stanford University
Gravity Ball Third heartbeat vx vy Piech, CS106A, Stanford University
Gravity Ball What happens when we hit a wall? Piech, CS106A, Stanford University
Gravity Ball We have this velocity vx vy Piech, CS106A, Stanford University
Gravity Ball Our new velocity vx vy vy = vy * - DAMPING; Piech, CS106A, Stanford University
Gravity Ball Seventh Heartbeat vy vx vy = vy * - DAMPING; Piech, CS106A, Stanford University
Questions? Piech, CS106A, Stanford University
Gravity Ball Piech, CS106A, Stanford University
A Sticky Situation This is our new velocity The ball is above the bottom so we reverse its vy The ball is bellow the bottom so we reverse its vy Piech, CS106A, Stanford University
Centering Piech, CS106A, Stanford University
By Chris Piech, CS106A, Stanford University
Once upon a time… Piech, CS106A, Stanford University
x was looking for love… int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x Piech, CS106A, Stanford University
x was looking for love… int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x Piech, CS106A, Stanford University
x was looking for love… x was definitely int x = 5; looking for love if(lookingForLove()) { int y = 5; } println(x + y); 5 x Piech, CS106A, Stanford University
And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 y x Piech, CS106A, Stanford University
And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 Hi, I’m y 5 y x Piech, CS106A, Stanford University
“Wow!” Piech, CS106A, Stanford University
And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 Wow 5 y x Piech, CS106A, Stanford University
And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 We have so much 5 in common y x Piech, CS106A, Stanford University
And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 We both have 5 value 5! y x Piech, CS106A, Stanford University
And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 Maybe one day 5 we can… y x Piech, CS106A, Stanford University
And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 println together? 5 y x Piech, CS106A, Stanford University
They got along int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 y x Piech, CS106A, Stanford University
It was a beautiful match… Piech, CS106A, Stanford University
But then tragedy struck. Piech, CS106A, Stanford University
Tragedy Struck int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 y x Piech, CS106A, Stanford University
Tragedy Struck int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 y x Piech, CS106A, Stanford University
Tragedy Struck int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x Piech, CS106A, Stanford University
Noooooooooooooooo! Piech, CS106A, Stanford University
You see… Piech, CS106A, Stanford University
When a program exits the code block… int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x Piech, CS106A, Stanford University
Where a variable was declared… int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x Piech, CS106A, Stanford University
It gets deleted from memory! int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x Piech, CS106A, Stanford University
Since y was declared inside the if int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x Piech, CS106A, Stanford University
It gets deleted from memory here int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x Piech, CS106A, Stanford University
And doesn’t exist here. int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x Piech, CS106A, Stanford University
And doesn’t exist here. int x = 5; Error. Undefined if(lookingForLove()) { variable y. int y = 5; } println(x + y); 5 x Piech, CS106A, Stanford University
The End Piech, CS106A, Stanford University
Or is it? Piech, CS106A, Stanford University
Variables have a lifetime public void run(){ double v = 8; if ( condition ){ v = 4; … some code } … some other code } Piech, CS106A, Stanford University
Variables have a lifetime public void run(){ double v = 8; if ( condition ){ v = 4; … some code } … some other code } Piech, CS106A, Stanford University
Come to existence when declared public void run(){ double v = 8; Comes to life here if ( condition ){ v = 4; … some code 8 } v … some other code } Piech, CS106A, Stanford University
Live Until End of Code-Block public void run(){ double v = 8; if ( condition ){ This is the inner most code block in which it was v = 4; declared…. … some code 4 } v … some other code } Piech, CS106A, Stanford University
Variables have a lifetime public void run(){ double v = 8; if ( condition ){ Still alive here… v = 4; … some code 4 } v … some other code } Piech, CS106A, Stanford University
Live Until End of Code-Block public void run(){ double v = 8; if ( condition ){ v = 4; … some code 4 } v … some other code } It dies here (at the end of its code block) Piech, CS106A, Stanford University
Live Until End of Code-Block public void run(){ double v = 8; if ( condition ){ v = 4; … some code } … some other code } It dies here (at the end of its code block) Piech, CS106A, Stanford University
Chapter 2 Piech, CS106A, Stanford University
The programmer fixed her bug Piech, CS106A, Stanford University
x was looking for love… int x = 5; if(lookingForLove()) { int y = 5; println(x + y); } 5 x Piech, CS106A, Stanford University
x was looking for love… x was definitely int x = 5; looking for love if(lookingForLove()) { int y = 5; println(x + y); } 5 x Piech, CS106A, Stanford University
x met y int x = 5; if(lookingForLove()) { int y = 5; println(x + y); } 5 5 y x Piech, CS106A, Stanford University
Since they were both in scope… int x = 5; if(lookingForLove()) { int y = 5; println(x + y); } 5 5 y x Piech, CS106A, Stanford University
The story had a happy ending! Piech, CS106A, Stanford University
Recommend
More recommend