import turtle
import random
 
# Screen
wn = turtle.Screen()
wn.title("Field Goal Challenge")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0)
 
# Score
score = 0
 
# Goal Posts
goal = turtle.Turtle()
goal.hideturtle()
goal.color("yellow")
goal.pensize(5)
goal.penup()
goal.goto(-50, 200)
goal.pendown()
goal.goto(-50, 260)
goal.goto(50, 260)
goal.goto(50, 200)
 
# Football
ball = turtle.Turtle()
ball.shape("circle")
ball.color("brown")
ball.penup()
ball.goto(-250, -200)
ball.dx = 5
 
# Scoreboard
pen = turtle.Turtle()
pen.hideturtle()
pen.penup()
pen.goto(0, 260)
pen.write("Score: 0", align="center", font=("Arial", 16, "bold"))
 
# Move ball side to side
def move_ball():
ball.setx(ball.xcor() + ball.dx)
if ball.xcor() > 260 or ball.xcor() < -260:
ball.dx *= -1
 
# Kick function
def kick():
global score
x_pos = ball.xcor()
 
# Between goal posts = score
if -50 < x_pos < 50:
score += 1
pen.clear()
pen.write(f"Score: {score}", align="center", font=("Arial", 16, "bold"))
ball.goto(random.randint(-250, 250), -200)
 
if score == 5:
pen.goto(0, 0)
pen.write("YOU WIN! 🏆", align="center", font=("Arial", 28, "bold"))
ball.hideturtle()
else:
ball.goto(random.randint(-250, 250), -200)
 
# Controls
wn.listen()
wn.onkey(kick, "space")
 
# Game Loop
while True:
wn.update()
move_ball()