Made an epic showcase website and API!
This commit is contained in:
parent
9e21e4dbdd
commit
1907fad7c5
89 changed files with 36444 additions and 3 deletions
21
projects/Day 19/README.md
Normal file
21
projects/Day 19/README.md
Normal file
|
|
@ -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
|
||||
|
||||

|
||||
|
||||
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
|
||||
|
||||

|
||||
|
||||
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.
|
||||
27
projects/Day 19/etch-a-sketch.py
Normal file
27
projects/Day 19/etch-a-sketch.py
Normal file
|
|
@ -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()
|
||||
43
projects/Day 19/turtle-race.py
Normal file
43
projects/Day 19/turtle-race.py
Normal file
|
|
@ -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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue