Skip to content

Instantly share code, notes, and snippets.

@fastfingertips
Created March 10, 2024 00:26
Show Gist options
  • Save fastfingertips/3c3b2de0c4e7e816a78b830dc1592a4e to your computer and use it in GitHub Desktop.
Save fastfingertips/3c3b2de0c4e7e816a78b830dc1592a4e to your computer and use it in GitHub Desktop.
import random
import turtle
# instances
screen = turtle.Screen()
count_down_turtle = turtle.Turtle()
score_turtle = turtle.Turtle()
# colors
screen.bgcolor("black")
count_down_turtle.color("dark gray")
score_turtle.color("cadet blue")
FONT = ('Arial', 20, 'normal')
game_over = False
turtle_list = []
score = 0
def setup_score_turtle():
score_turtle.hideturtle() # hide arrow
score_turtle.penup() # don't draw a line
top_height = screen.window_height() / 2 # positive height/2 is the top of the screen
y = top_height - top_height / 10 # decreasing a bit so text will be visible
score_turtle.setposition(0, y)
score_turtle.write('Score: 0', align='center', font=FONT)
def make_turtle(x, y, grid_size=10):
def handle_click(x, y):
global score
score += 1
score_turtle.clear()
score_turtle.write(f"Score: {score}", align="center", font=FONT)
print(x, y)
t = turtle.Turtle("turtle")
t.shapesize(2, 2)
t.color("green")
t.penup() # don't draw
t.onclick(handle_click)
t.goto(x * grid_size, y * grid_size)
turtle_list.append(t)
def setup_turtles():
x_coordinates = range(-20, 21, 10)
y_coordinates = range(-20, 21, 10)
for x in x_coordinates:
for y in y_coordinates:
make_turtle(x, y)
def hide_turtles():
for t in turtle_list:
t.hideturtle()
def show_turtles_randomly():
if not game_over:
hide_turtles()
random.choice(turtle_list).showturtle()
screen.ontimer(show_turtles_randomly, 500)
def countdown(time):
global game_over
count_down_turtle.hideturtle()
count_down_turtle.penup()
top_height = screen.window_height() / 2
count_down_turtle.setposition(0, (top_height - top_height / 10) - 30)
count_down_turtle.clear()
if time:
count_down_turtle.write(f"Time: {time}", align="center", font=FONT)
screen.ontimer(lambda: countdown(time - 1), 1000)
else:
game_over = True
count_down_turtle.clear()
hide_turtles()
if score:
count_down_turtle.color("green")
count_down_turtle.write("You Win!", align='center', font=FONT)
else:
count_down_turtle.color("crimson")
count_down_turtle.write("Game Over!", align='center', font=FONT)
def start_game_up():
global game_over
game_over = False
turtle.tracer(0)
setup_score_turtle()
setup_turtles()
hide_turtles()
show_turtles_randomly()
turtle.tracer(1)
screen.ontimer(lambda: countdown(10), 10)
start_game_up()
turtle.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment