OpenBrackets
Intermediate Python
Week 5 Day 1 & 2
Welcome!
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.
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!
Asking Questions
Type in chat
Or
How are you doing?
1 2 3 4 5 6 7 8 9 10
Individual Check-Ins
While we’re doing check-ins, here are some extra ideas
Functions Review
Using functions
print("hello!")
print()
random_num = random.randint(1, 10)
Functions as people’s phone numbers
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
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.
5 minute break!
Intro to Turtle: Part 4
Handling Keyboard Events
Functions are variables
output = print
output("Hello!")
get_input = input
name = get_input("What's your name? ")
Try running this code in your Repl!
Why is this useful? Keyboard Events!
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
Keyboard Events
Exercises
Block Collector : Part 1
Game Structure
Learning Targets
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.
Create a repl and setup
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().
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.
Block Collector Initialization - part 1
Code this in the main program body, outside of the while loop. This is our setup code.
Thanks for coming to class!
Please let us know if you have any questions!