diff --git a/Day 19/README.md b/Day 19/README.md new file mode 100644 index 0000000..4291c24 --- /dev/null +++ b/Day 19/README.md @@ -0,0 +1,21 @@ +# Day 19 - Instances, State, and Higher Order Functions + +- Built an etch-a-sketch while understanding higher order functions +- Simulated a turtle race with randomized steps using class instances + +## Etch-a-Sketch + +![Day_19_Etch_A_Sketch](https://user-images.githubusercontent.com/30333942/128927402-10006522-2640-4569-b6d0-706cca4fc811.gif) + +This etch-a-sketch uses higher order functions to run when keys are pressed on the keyboard. +- **w** - Move forwards +- **s** - Move backwards +- **a** - Turn left +- **d** - Turn right +- **c** - Reset etch-a-sketch + +## The Great Turtle Race + +![Day_19_The_Great_Turtle_Race](https://user-images.githubusercontent.com/30333942/128927073-cf125780-b3ce-432e-8808-c4ac4d2c6e34.gif) + +The Great Turtle Race uses multiple instances of the Turtle object from [the turtle module](https://docs.python.org/3/library/turtle.html) that move by a random number of steps to the edge of the screen. diff --git a/Day 19/etch-a-sketch.py b/Day 19/etch-a-sketch.py new file mode 100644 index 0000000..baad05e --- /dev/null +++ b/Day 19/etch-a-sketch.py @@ -0,0 +1,27 @@ +from turtle import Turtle, Screen + +arrow = Turtle() +screen = Screen() + +def move_forwards(): + arrow.forward(10) + +def move_backwards(): + arrow.backward(10) + +def clockwise(): + arrow.right(10) + +def counter_clockwise(): + arrow.left(10) + +def clear_drawing(): + arrow.reset() + +screen.listen() +screen.onkey(key="w", fun=move_forwards) # higher order function +screen.onkey(key="s", fun=move_backwards) +screen.onkey(key="d", fun=clockwise) +screen.onkey(key="a", fun=counter_clockwise) +screen.onkey(key="c", fun=clear_drawing) +screen.exitonclick() \ No newline at end of file diff --git a/Day 19/turtle-race.py b/Day 19/turtle-race.py new file mode 100644 index 0000000..8901312 --- /dev/null +++ b/Day 19/turtle-race.py @@ -0,0 +1,43 @@ +from turtle import Turtle, Screen +import random + +# Game settings +is_race_on = False +screen = Screen() +screen.title("The Great Turtle Race!") +screen.setup(width=500, height=400) + +# Get user guess +user_selection = "" +while not user_selection: + user_selection = screen.textinput(title="Who will win?", prompt="Which turtle will win the race?\nType blue, green, red, orange, turquoise, or purple.\nEnter a color: ") + +# Setup turtle objects after input +colors = ["blue", "green", "turquoise", "orange", "red", "purple"] +turtles = [Turtle(shape="turtle") for turtle in range(6)] +for i in range(6): + turtles[i].color(colors[i]) + turtles[i].penup() + turtles[i].goto(x=-230, y=-100+(i*40)) + +# Start race! +result = Turtle() +result.penup() +result.hideturtle() +result.goto(x=0, y=100) +is_race_on = True +while is_race_on: + for turtle in turtles: + # Move turtle forward a random distance + rand_distance = random.randint(0, 10) + turtle.forward(rand_distance) + # Check if a turtle won the race + if turtle.xcor() > 230: + is_race_on = False + winning_color = turtle.pencolor() + if winning_color == user_selection: + result.write(f"You've won! The {winning_color} turtle is the winner!", align="center", font=("Open Sans", 16, "normal")) + else: + result.write(f"You've lost! The {winning_color} turtle is the winner!", align="center", font=("Open Sans", 16, "normal")) + +screen.exitonclick() \ No newline at end of file