Animation Piech, CS106A, Stanford University Our story so far - - PowerPoint PPT Presentation

animation
SMART_READER_LITE
LIVE PREVIEW

Animation Piech, CS106A, Stanford University Our story so far - - PowerPoint PPT Presentation

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,


slide-1
SLIDE 1

Piech, CS106A, Stanford University

Animation

slide-2
SLIDE 2

Piech, CS106A, Stanford University

Our story so far…

slide-3
SLIDE 3

Piech, CS106A, Stanford University

Our story so far…

slide-4
SLIDE 4

Piech, CS106A, Stanford University

Learning Goals

  • 1. Write animated programs

2. Center an object

slide-5
SLIDE 5

Piech, CS106A, Stanford University

You will be able to write Bouncing Ball

slide-6
SLIDE 6

Piech, CS106A, Stanford University

Learning Goals For Me

  • 1. Speak slower
slide-7
SLIDE 7

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); }

slide-8
SLIDE 8

Piech, CS106A, Stanford University

Move GRect

slide-9
SLIDE 9

Piech, CS106A, Stanford University

Animation Loop

private void run() { // setup while(true) { // update world // pause pause(DELAY); } }

slide-10
SLIDE 10

Piech, CS106A, Stanford University

Animation Loop

private void run() { // setup while(true) { // update world // pause pause(DELAY); } } Make all the variables you need. Add graphics to the screen.

slide-11
SLIDE 11

Piech, CS106A, Stanford University

Animation Loop

private void run() { // setup while(true) { // update world // pause pause(DELAY); } } The animation loop is a repetition of heartbeats

slide-12
SLIDE 12

Piech, CS106A, Stanford University

Animation Loop

private void run() { // setup while(true) { // update world // pause pause(DELAY); } } Each heart-beat, update the world forward one frame

slide-13
SLIDE 13

Piech, CS106A, Stanford University

Animation Loop

private void run() { // setup while(true) { // update world // pause pause(DELAY); } } If you don’t pause, humans won’t be able to see it

slide-14
SLIDE 14

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); } }

slide-15
SLIDE 15

Piech, CS106A, Stanford University

Gravity Ball

slide-16
SLIDE 16

Piech, CS106A, Stanford University

Gravity Ball

First heartbeat Velocity: how much the ball position changes each heartbeat

slide-17
SLIDE 17

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

slide-18
SLIDE 18

Piech, CS106A, Stanford University

Gravity Ball

Second heartbeat

vx vy

slide-19
SLIDE 19

Piech, CS106A, Stanford University

Gravity Ball

Third heartbeat

vx vy

slide-20
SLIDE 20

Piech, CS106A, Stanford University

Gravity Ball

What happens when we hit a wall?

slide-21
SLIDE 21

Piech, CS106A, Stanford University

Gravity Ball

We have this velocity

vx vy

slide-22
SLIDE 22

Piech, CS106A, Stanford University

Gravity Ball

Our new velocity

vx vy vy = vy * -DAMPING;

slide-23
SLIDE 23

Piech, CS106A, Stanford University

Gravity Ball

Seventh Heartbeat

vx vy vy = vy * -DAMPING;

slide-24
SLIDE 24

Piech, CS106A, Stanford University

Questions?

slide-25
SLIDE 25

Piech, CS106A, Stanford University

Gravity Ball

slide-26
SLIDE 26

Piech, CS106A, Stanford University

A Sticky Situation

This is our new velocity

The ball is bellow the bottom so we reverse its vy The ball is above the bottom so we reverse its vy

slide-27
SLIDE 27

Piech, CS106A, Stanford University

Centering

slide-28
SLIDE 28

Piech, CS106A, Stanford University

By Chris

slide-29
SLIDE 29

Piech, CS106A, Stanford University

Once upon a time…

slide-30
SLIDE 30

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); x was looking for love… x 5

slide-31
SLIDE 31

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); x was looking for love… x 5

slide-32
SLIDE 32

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); x was looking for love… x 5

x was definitely looking for love

slide-33
SLIDE 33

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); And met y x 5 y 5

slide-34
SLIDE 34

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); And met y x 5 y 5

Hi, I’m y

slide-35
SLIDE 35

Piech, CS106A, Stanford University

“Wow!”

slide-36
SLIDE 36

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); And met y x 5 y 5

Wow

slide-37
SLIDE 37

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); And met y x 5 y 5

We have so much in common

slide-38
SLIDE 38

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); And met y x 5 y 5

We both have value 5!

slide-39
SLIDE 39

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); And met y x 5 y 5

Maybe one day we can…

slide-40
SLIDE 40

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); And met y x 5 y 5

println together?

slide-41
SLIDE 41

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); They got along x 5 y 5

slide-42
SLIDE 42

Piech, CS106A, Stanford University

It was a beautiful match…

slide-43
SLIDE 43

Piech, CS106A, Stanford University

But then tragedy struck.

slide-44
SLIDE 44

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); Tragedy Struck x 5 y 5

slide-45
SLIDE 45

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); Tragedy Struck x 5 y 5

slide-46
SLIDE 46

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); Tragedy Struck x 5

slide-47
SLIDE 47

Piech, CS106A, Stanford University

Noooooooooooooooo!

slide-48
SLIDE 48

Piech, CS106A, Stanford University

You see…

slide-49
SLIDE 49

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y);

When a program exits the code block…

x 5

slide-50
SLIDE 50

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); Where a variable was declared… x 5

slide-51
SLIDE 51

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); It gets deleted from memory! x 5

slide-52
SLIDE 52

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); Since y was declared inside the if x 5

slide-53
SLIDE 53

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); It gets deleted from memory here x 5

slide-54
SLIDE 54

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); And doesn’t exist here. x 5

slide-55
SLIDE 55

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); And doesn’t exist here. x 5

  • Error. Undefined

variable y.

slide-56
SLIDE 56

Piech, CS106A, Stanford University

The End

slide-57
SLIDE 57

Piech, CS106A, Stanford University

Or is it?

slide-58
SLIDE 58

Piech, CS106A, Stanford University

Variables have a lifetime

public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code }

slide-59
SLIDE 59

Piech, CS106A, Stanford University

public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code }

Variables have a lifetime

slide-60
SLIDE 60

Piech, CS106A, Stanford University

Come to existence when declared v

Comes to life here

8

public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code }

slide-61
SLIDE 61

Piech, CS106A, Stanford University

Live Until End of Code-Block

This is the inner most code block in which it was declared….

v 4

public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code }

slide-62
SLIDE 62

Piech, CS106A, Stanford University

Variables have a lifetime

Still alive here…

v 4

public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code }

slide-63
SLIDE 63

Piech, CS106A, Stanford University

Live Until End of Code-Block

It dies here (at the end of its code block)

v 4

public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code }

slide-64
SLIDE 64

Piech, CS106A, Stanford University

Live Until End of Code-Block

It dies here (at the end of its code block)

public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code }

slide-65
SLIDE 65

Piech, CS106A, Stanford University

Chapter 2

slide-66
SLIDE 66

Piech, CS106A, Stanford University

The programmer fixed her bug

slide-67
SLIDE 67

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; println(x + y); } x was looking for love… x 5

slide-68
SLIDE 68

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; println(x + y); } x was looking for love… x 5

x was definitely looking for love

slide-69
SLIDE 69

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; println(x + y); } x met y x 5 y 5

slide-70
SLIDE 70

Piech, CS106A, Stanford University

int x = 5; if(lookingForLove()) { int y = 5; println(x + y); } Since they were both in scope… x 5 y 5

slide-71
SLIDE 71

Piech, CS106A, Stanford University

The story had a happy ending!