Made an etch-a-sketch and turtle race game!

Update README.md
This commit is contained in:
rzmk 2021-08-10 15:52:50 -04:00
parent bc3301c6a7
commit 8f4c47043d
3 changed files with 91 additions and 0 deletions

21
Day 19/README.md Normal file
View 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
![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.

27
Day 19/etch-a-sketch.py Normal file
View 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
Day 19/turtle-race.py Normal file
View 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()