11 Animations
11 Animations
import tkinter
import tkinter
CANVAS_HEIGHT/2
CANVAS_HEIGHT/2 – SQUARE_SIZE/2
CANVAS_HEIGHT/2
start_y
end_y
Some “heavy
duty” variables
allow you to call
functions on them
(0, start_y)
(SQUARE_SIZE, end_y)
Woot!
* That’s not
Piech quite CS106A,
+ Sahami, toy story, butUniversity
Stanford it is a start…
Animation Loop
def main():
# setup
while True:
# update world
# pause
time.sleep(DELAY)
while True:
# update world
# pause
time.sleep(DELAY)
def main():
# setup
The animation loop is a
while True: repetition of heartbeats
# update world
# pause
time.sleep(DELAY)
def main():
# setup
while True:
Each heart-beat, update
# update world the world forward one
frame
# pause
time.sleep(DELAY)
def main():
# setup
while True:
# update world
def main():
# setup
canvas = make_canvas(CANVAS_WIDTH, CANVAS_HEIGHT)
r = canvas.create_rectangle(0, 0, 100, 100)
while not is_past_center(canvas, r):
# update world
canvas.move(r, 1, 0)
canvas.update()
# pause
time.sleep(DELAY)
change_x
change_y
change_x
change_y
change_x
change_y
change_x
change_y
change_y
change_x
change_y
change_x
change_y
change_x
change_y
change_x
change_y
change_x
change_y
change_x
change_y
change_x
def make_ball(canvas):
Key:
https://docs.google.com/document/d/1eBtnEilI3KHe
fFS-kSAOpXqeSXpbfTTMlmOgj6I9dvk/
Piech + Sahami, CS106A, Stanford University
def main():
canvas = make_canvas(…)
make_ball(canvas)
def make_ball(canvas):
canvas.create_rectangle( … , fill='blue')
stack heap
main
def make_ball(canvas):
canvas.create_rectangle( … , fill='blue')
stack heap
memory.com/42
main memory.com/42
canvas
def make_ball(canvas):
canvas.create_oval( … , fill='blue')
stack heap
memory.com/42
main
canvas 42
def make_ball(canvas):
canvas.create_oval( … , fill='blue')
stack heap
memory.com/42
main
canvas 42
make_ball
canvas
def make_ball(canvas):
canvas.create_oval( … , fill='blue')
stack heap
memory.com/42
main
canvas 42
make_ball
canvas 42
def make_ball(canvas):
canvas.create_oval( … , fill='blue')
stack heap
memory.com/42
main
canvas 42
make_ball
canvas 42
def make_ball(canvas):
canvas.create_oval( … , fill='blue')
stack heap
memory.com/42
main
canvas 42
make_ball
canvas 42
def make_ball(canvas):
canvas.create_oval( … , fill='blue')
stack heap
memory.com/42
main
canvas 42
make_ball
canvas 42
def make_ball(canvas):
canvas.create_oval( … , fill='blue')
stack heap
memory.com/42
main
canvas 42
boolean canvas
integer pixel
float SimpleImage
string list