1 of 14

OpenBrackets

Intermediate Python

Week 8 Day 2

Welcome!

2 of 14

Welcome to class!

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

Schedule for today:

  • 60 minutes for Step 5 and part of Step 6
  • 10 minutes for a break
  • 20 minutes for finishing Step 6

3 of 14

Reminders

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 14

Asking Questions

Type in chat

Or

  1. Click Reactions
  2. Click Raise Hand

5 of 14

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, lambdalaugh, and lambdacool.)

1 2 3 4 5 6 7 8 9 10

6 of 14

Steps for Snake

  1. Set up the Repl and turtle screen
  2. Create the snake head and let the user control it
  3. Stop the game if the player runs out of bounds
  4. Create a piece of food in a random spot on the screen
  5. Let the snake eat food
  6. When the snake eats, make it longer
  7. Stop the game if the snake runs into itself
  8. Add a scoreboard
  9. Reset the game and add a high score

7 of 14

Step 5: Eating food

  1. Check if the snake head is currently eating the food
    1. We need this check to happen before the head moves in the loop
    2. If the distance between turtles is >20, then the turtle is ‘eaten’
  2. If the food gets eaten, move it to another random spot on the screen

Optional: Make sure the next random spot isn’t close enough to the head to be eaten instantly

8 of 14

10 minute break!

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

9 of 14

Step 6a: Creating snake segments

The segments of the snake are the green parts. The head doesn’t count as a segment.

Each segment will be a square turtle. The color is your choice.

We’ll store our snake segments in a list. Since our snake starts with just a head, we won’t have any segments at the start of the game. Create an empty list for the segments.

0

1

2

10 of 14

Step 6b: Moving snake segments, part 1

This is probably the most difficult part of the game, but give it your best shot!

  1. Add a “fake” segment to the list
    1. Put its position at (-20, 0)
    2. Remember, we use .append to add something to the end of a list
  2. Right before we move the head, move the segment to the head’s position
    • Don’t worry about any other segments for now
  3. Test your code. Once it’s working, get rid of the fake segment

11 of 14

Step 6c: Adding snake segments

When a piece of food is eaten, we need to add another segment. This segment will go at the end of the snake. Then the snake will move.

  1. Find the position of the end of the snake
    1. Hint: if there are any segments, the end of the snake will be the last segment. Otherwise, it’ll be the head.
  2. If food gets eaten, add a new segment in that position
    • We add things to a list with .append

12 of 14

Step 6d: Moving snake segments, part 2

Right now, only the first segment is moving.

  1. Write a for loop that goes backwards through each segment index, skipping 0
    1. for i in reversed(range(1, len(segments)):
  2. Move each segment to the position of the segment before it in the list

13 of 14

Extra: using .pos()

.pos() gives us a pair of numbers as the full position of the turtle

We can use

turtle1.goto(turtle2.pos())

instead of

turtle1.goto(turtle2.xcor(), turtle2.ycor())

14 of 14

Thanks for coming to class!

Please let us know if you have any questions!