1 of 28

OpenBrackets

Intermediate Python

Week 5 Day 1 & 2

Welcome!

2 of 28

Welcome to class!

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

Today we’ll be continuing the material we didn’t get to last class.

3 of 28

Reminders

Our teachers today are Aidan and Stanley.

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 28

Asking Questions

Type in chat

Or

  1. Click Reactions
  2. Click Raise Hand

5 of 28

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 pkingcry, pkingD, pkingthink, pkinghey, pkinglove, and pkinghype.)

1 2 3 4 5 6 7 8 9 10

6 of 28

Individual Check-Ins

  • We’ll be doing individual check-ins in breakout rooms for the first part of class
  • Please have your Turtle Race Repl ready to share
    • It’s fine if you didn’t finish everything
    • This is just to figure out how everybody’s doing
    • If you need help or debugging, now’s a great time to ask for it!
  • While we’re checking in, continue working on Turtle Race
    • If you’re already done, there are some extensions on the next slide

7 of 28

While we’re doing check-ins, here are some extra ideas

  • Make the winning turtle spin
    • You can use a loop and .right for this
    • If we wanted my_turtle to spin to the right (clockwise) by 45 degrees, we’d write my_turtle.right(45)
  • Have the turtles change speed when they move
    • Add a random number to each turtle’s speed
  • Change the finish line to be a checkerboard instead of just a line
  • Make your game work on all screen sizes
    • This is the hardest one, so try the others first
    • Hint: use screen.screensize()

8 of 28

Functions Review

9 of 28

Using functions

  • To use a function, we put the function name and then parentheses after it
    • The parentheses mean that we want Python to call the function
    • Sometimes, we put things inside the parentheses, if the function asks for them
    • If the function’s from a library, we need to add the library name

print("hello!")

print()

random_num = random.randint(1, 10)

10 of 28

Functions as people’s phone numbers

  • We can think of each function as somebody’s phone number
  • Imagine you know a person named Alice
    • Alice is very very good at adding 5 to a number
    • Whenever we want to add 5 to a number, we can call Alice and ask her to help us
    • This is similar to how we call a function to perform a specific job
  • Imagine we know another person named Bob
    • Bob is very good at saying hello to people
    • He never gets tired of saying hi, no matter how many times he needs to do it
    • If we need to say hello to a user, we can call Bob and ask him to do it for us

11 of 28

Creating functions

def say_hi():

print("hello there!")

Use def to tell python we’re defining a function

Function name

Parentheses without any parameters

Colon (very important!)

Make sure to indent your code!

Code inside the function runs when we call it

12 of 28

Creating functions

def add_five(number):

return number + 5

Use def to tell python we’re defining a function

Function name

Parentheses with a parameter inside

Colon (very important!)

Make sure to indent your code!

At the end of a function, we can tell it to return a value. Then we can use this value in a variable.

13 of 28

5 minute break!

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

14 of 28

Intro to Turtle: Part 4

Handling Keyboard Events

15 of 28

Functions are variables

  • Main concept: we can assign a function to a variable!
  • Here, we do NOT want to put parentheses
    • We’re talking about the function itself, not its output
    • We’re not trying to call the function
  • Continuing our phone number analogy:
    • When we put parentheses, we were calling the person on the other end of the phone and asking for their help
    • Now, we’re passing along a phone number to somebody else, in case they want to call that person
  • We’ll see why this is useful in a moment

16 of 28

output = print

output("Hello!")

get_input = input

name = get_input("What's your name? ")

Try running this code in your Repl!

17 of 28

Why is this useful? Keyboard Events!

  • With turtle, we can run a function each time the user presses a key
    • This is helpful for creating games with keyboard controls
  • This is where functions as variables is useful
    • We can tell turtle which function to call when a key is pressed
    • To do this, we give our function to another function

18 of 28

def print_something():

print("right arrow pressed")

screen.listen()

screen.onkey(print_something, "Right")

Use the screen variable

Tell turtle to listen for key presses

Call the...

...print_something function...

... when the Right arrow is pressed

19 of 28

Keyboard Events

  • We can have different functions called for different keys
    • For example, we can have the turtle go in different directions depending on which arrow key is pressed
  • Key codes tell turtle which key to listen for
    • The arrow keys are "Up", "Down", "Left", and "Right"
    • The letters are all lowercase ("w", "a", "s", "d")
    • These are case-sensitive!

20 of 28

Exercises

  1. Define a function called move_forward that moves the turtle forward by 20 pixels
  2. Define a function called move_backward that moves the turtle backward by 20 pixels
    1. .backward(20) moves the turtle backwards by 20 pixels
  3. Tell turtle to listen for keyboard presses
  4. Tell turtle to call move_forward when the Right arrow is pressed
    • Remember, the key code for the Right arrow is "Right"
  5. Tell turtle to call move_backward when the Left arrow is pressed
  6. Test your code!

21 of 28

Block Collector : Part 1

Game Structure

22 of 28

Learning Targets

  • Learn why an infinite loop can be used to manage game logic
  • Implement an infinite loop to manage game state
  • Learn how and why to use screen.tracer(0) and screen.update()

23 of 28

Block Collector (Simplified Snake)

In Block Collector, the player moves a block around the screen and collects other “food” blocks.

We’ll create this using the turtle library, and the player will control their block using the arrow keys.

For those of you who want an extra challenge, we’ll also discuss how to implement a timer that tracks how long it takes for the player to collect all the food blocks.

24 of 28

Create a repl and setup

  1. Import random
  2. Import turtle
  3. Setup your Screen
    1. screen = turtle.Screen()
    2. screen.setup(1.0, 1.0)

25 of 28

Game structure

Unlike TurtleRace, we’re going to be using an infinite loop to run our game logic. This lets us keep the game running forever, since the player might never finish.

screen.tracer(0)

while True:

screen.update()

Remember how in TurtleRace, the turtles visibly moved from the center to their positions?

screen.tracer(0) tells turtle to NOT update until we want it to update. Then, in the loop, we call update().

26 of 28

Game structure

Right now, the computer runs the loop as quickly as it can. We can add a small delay inside the loop to slow things down.

import time

time.sleep(0.001) # sleep for 1 millisecond

.sleep() is a function in the library time that allows us to pause the program.

27 of 28

Block Collector Initialization - part 1

Code this in the main program body, outside of the while loop. This is our setup code.

  1. Setup the game structure
  2. Create a player turtle
  3. Create key events for the player
    1. Arrow keys: “Right”, “Left”, “Up”, “Down”
    2. Change the player direction (.setheading())
    3. .setheading(0) points the turtle to the right
    4. .setheading(90) points the turtle up
    5. .setheading(180) points the turtle to the left
    6. .setheading(270) points the turtle down

28 of 28

Thanks for coming to class!

Please let us know if you have any questions!