1 of 20

OpenBrackets

Intermediate Python

Week 3 Day 1

Welcome!

2 of 20

Welcome to class!

This is the Intermediate Python Programming class. Make sure you’re in the right place!

Schedule for today:

  • 45 minutes of instruction and exercises
  • 5 minutes for a break
  • 20 minutes of instruction and exercises
  • 20 minutes for a mini project with randomized art

3 of 20

Reminders

Aidan and Stanley are both out for the first 30 minutes of class, so Nan will be subbing in.

Please keep your camera on as much as possible! This helps us know how you’re doing and whether you’re following along. It will also help you stay more engaged.

Don’t be afraid to ask questions!

4 of 20

Asking Questions

Type in chat

Or

  1. Click Reactions
  2. Click Raise Hand

5 of 20

How are you doing?

  1. From a scale of 1 to 10, how’s your day so far and why?
  2. How would you place the emotes on this scale? (From left to right, these emotes are named lambdasomething, lambdacry, lambdagrimace, lambda, lambdacool, and lambdalaugh.)

1 2 3 4 5 6 7 8 9 10

6 of 20

Intro to Turtle : Part 2

Drawing

7 of 20

Drawing with Turtle

.goto(x, y) moves the Turtle to a new position (x, y) while drawing

The turtle starts at (0, 0)

Try drawing a square with your turtle! You’ll need to use .goto 4 times.

8 of 20

Exercise

  1. Create a Turtle and draw a square
  2. Create another Turtle and draw a triangle

Challenge: Pick some letters of the alphabet and draw them!

(-110, -20)

(-80, 100)

(0, 0)

(100, 100)

(100, 0)

(0, 100)

9 of 20

Changing Turtle Pen Color

Changing the turtle’s color will also change the color of the lines it draws. We learned how to change the turtle’s color last class.

We don’t have to change the color of the turtle right after we make it. You can draw something, change the color, and then draw something else.

my_turtle.color(new_color)

10 of 20

Exercise

Make each side of your square a different color!

Challenge: Pick some letters of the alphabet and draw them! If you’ve already done this, make each letter a different random color.

(-110, -20)

(-80, 100)

(0, 0)

(100, 100)

(100, 0)

(0, 100)

11 of 20

Pen Up/Down

In order to draw multiple separate shapes, we need to move the pen without drawing.

  • .penup()
    • Stops the pen from writing
    • This allows us to move the pen without drawing anything
  • .pendown()
    • Puts the pen back down, so we can start drawing again

12 of 20

Exercise

  • Draw two separate lines with the same turtle
    • Make sure the lines aren’t connected
  • The lines can be anywhere you want
    • If you don’t have ideas, try drawing a line from (-100, -100) to (-100, 100) and another from (100, -100) to (100, 100)
  • Remember to use .penup() and .pendown()

13 of 20

5 minute break!

  • Turn your camera off
  • Get a drink of water
  • Stand up and stretch

14 of 20

Writing text with Turtle

.write("text", False, "center", ("", 20, "normal"))

This is the text that we want to write.

Don’t worry about this for now. (If you’re curious, this parameter determines whether the turtle should move or not.)

This is the alignment for our text. “center” align means that our text will be centered around the turtle’s current position.

This controls what font name the text will be in. For now, we’ll leave this blank for default. (Using different fonts can cause system portability issues.)

This is the font size.

This is the font style. We can have “normal”, “bold”, “italic”, and “underline”.

15 of 20

Exercise

Write your name in the center of the screen.

  • Choose your own font size and style
  • Use “center” align
  • If you have time, try experimenting with different font sizes and styles
    • If you want to try different font names as well, here’s the list that we know Repl supports: DejaVu Sans Mono, FreeSerif, DejaVu Sans, FreeSans, DejaVu Serif, and FreeMono
    • Some others might be available too but no guarantees

16 of 20

Another method for moving

.forward(distance) will move the turtle forward by whatever distance you want

  • This depends on the direction the turtle is facing
    • Turtles start off facing to the right (positive x direction)�

.setheading(degrees) changes the direction the turtle is facing

  • 0 degrees is right (positive y)
  • 90 degrees is up (positive x)
  • 180 degrees is left (negative x)
  • 270 degrees is down (negative y)

90°

270°

180°

17 of 20

Getting the coordinate values

  • .xcor()
    • Returns the x position of the turtle
  • .ycor()
    • Returns the y position of the turtle

Move a turtle from the origin to the right by 10 pixels using a loop. When its x position becomes greater than or equal to 200, stop the turtle.

18 of 20

Moving Turtles at the Same Time

  • If we used goto, one turtle would have to finish moving before the next one moved
  • By going forward a bit each time, we can move the turtles at the same time
  • Technically the turtles still don’t move at the same time if we use forward, but they take turns moving so if the distance is short enough it looks simultaneous

Add a second turtle which starts below the first turtle and moves to the right until its x position is greater than 200.

19 of 20

Mini Project: Randomized Drawings

We can make random drawings with three different methods

  1. Draw lines between random coordinates
  2. Draw lines between random offsets
  3. Spin by a random amount and go forward by a random amount

For all of these, we’ll use a for loop to keep moving the turtle around. Then, depending on which method we’re using, we’ll write code to tell the turtle how to move.

20 of 20

Thanks for coming to class!

Please let us know if you have any questions!