#100DaysOfCode: Python Day 19
Instances, States and Higher Order Functions
·May 30, 2021·
2 min read
#100DaysOfCode Day 19
☕️ I CREATED MY OWN TURTLE RACE BET GAME! ☕️
CLICK HERE and press "Run" to try it out.
Yesterday I created a turtle racing game. I also learned about higher order functions, event listeners, object states and object instances. Look at the code to better understand those concepts.
- When you add in GUI, games get a little tougher to build as there is so much more to consider.
- The fact that we can have multiple instances of an object and have it do our bidding is why object oriented programming is so powerful!
TO THE CODE!!!
from turtle import Turtle, Screen tim = Turtle() screen = Screen() # This is a function as an input def move_forwards(): tim.forward(10) screen.listen() # We bind an event listener using a higher order function as we passing the function to a function screen.onkey(key="space", fun=move_forwards) screen.exitonclick() # timmy and tommy and separate instances of turtle. timmy and tommy states are different # in terms of their colours (state is not limited to how they look but how they behaviour too) timmy = Turtle() timmy.color("green") tommy = Turtle() tommy.color("purple")
Making this game was a surprise, and if you love horse betting, you will love this game!
Want to stay up to date with my learning journey? Just sign up for my newsletter.