Turtle Graphics

# Turtle Graphics

## Turtle Graphics

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Turtle Graphics Victor Norman CS104 Calvin College

3. Quick Introduction to Objects • You have to instantiate the object, making a variable refer to it: • turt = turtle.Turtle() • You call (or "invoke") methods on it: • turt.forward(10) • turt.doSomething(33, 22, 11) • It is like asking the object to do something to itself: give me back a value or move yourself, or draw something, or change a value. • The object stores its own state (or characteristics, properties, or attributes).

4. Clicker Question

5. for Loop Syntax • Pattern: for <var> in <sequence>: <body> # multiple statements • Examples: for aVal in [3, 11, 22, 0, -3]: print(aVal) for aVal in [“I’m”, “a”, “lumberjack”, “and”, “I’m”, “ok”]: print(aVal)

6. for Loop Examples for val in [0, 1, 2, 3, 4, 5]: turt.forward(50) turt.left(60) total = 0 for val in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]: total = total + val print(total) total = 0 for val in range(1, 21): total = total + val print(total)

7. range(start, stop, step) • Built-in function • Generates list of integers starting at start, going up to (but not including) stop, by increments of step. • Can skip start  uses 0. E.g., range(10, 2) • Can skip step  uses 1. E.g., range(10)

8. range() Examples range(30) • generates list of numbers 0, 1, 2, …, 29. range(2, 30) • generates list of numbers 2, 3, 4, 5, …, 29. range(-3, 3, 2) • generates list of numbers -3, -1, 1. range(20, 0, -1) • generates list of numbers 20, 19, 18, 17, …, 2, 1.

9. Clicker Questions

10. Assignments • Do the few TuringsCraft questions before lab on Thursday.