import turtle
import random

# Function to clear the screen based on user input
def clear_screen():
    response = screen.textinput("Clear Screen", "Type 'yes' to clear the page:")
    if response and response.lower() == "yes":
        screen.clearscreen()  # Clears the entire screen
        return "yes"
    else:
        return"no"
# Function to print out the pattern at the bottom of the sheet  
def plot_zigzag():
    border_turtle.speed(0)
    border_turtle.penup()
    border_turtle.goto(-270, -240)
    border_turtle.pendown()
    for i in range(18):
        border_turtle.left(60)
        border_turtle.forward(30)
        border_turtle.right(120)
        border_turtle.forward(30)
        border_turtle.left(60)

# Function to print out the title of the page
def title(text):
    # Title with color
    layout_turtle = turtle.Turtle()
    layout_turtle.penup()
    layout_turtle.goto(0, 250)
    layout_turtle.color("black")
    layout_turtle.write(text, align="center", font=("Arial", 24, "bold"))
    # Clear screen prompt
    layout_turtle.hideturtle()

def display_text(text):
    # Setup turtle for writing
    writer = turtle.Turtle()
    writer.hideturtle()
    writer.penup()
    writer.speed(0)

    # Set font size and spacing
    font = ("Arial", 16, "normal")
    line_spacing = 24  # Space between lines

    # Set initial position for text
    x_start = -300
    y_start = 235
    writer.goto(x_start, y_start)

    # Write text line by line
    for line in text.split("\n"):  # Split the text into lines based on newline characters
        writer.write(line, font=font)
        y_start -= line_spacing  # Move down for the next line
        writer.goto(x_start, y_start)  # Reset to the start of the new line

# Set up the screen
screen = turtle.Screen()
screen.title("Text Adventure Game Character Page")
screen.setup(width=800, height=600)
screen.bgcolor("lightgrey")

# Character Stats with random values
stats = {
    "Strength": random.randint(8, 18),
    "Dexterity": random.randint(8, 18),
    "Constitution": random.randint(8, 18),
    "Intelligence": random.randint(8, 18),
    "Wisdom": random.randint(8, 18),
    "Charisma": random.randint(8, 18)
}

# Create a Turtle instance for drawing
layout_turtle = turtle.Turtle()
layout_turtle.speed(0)

# Draw each stat in a bordered, yellow background rectangle
layout_turtle.penup()
y_position = 130  # Adjusted position for stats to fit below race/class
layout_turtle.pensize(3)
for stat, value in stats.items():
    # Draw grey rectangle with black border
    layout_turtle.goto(80, y_position)
    layout_turtle.color("black", "yellow")  # Border color black, fill color grey
    layout_turtle.pendown()
    layout_turtle.begin_fill()
    for _ in range(2):
        layout_turtle.forward(200)  # Width of the rectangle
        layout_turtle.right(90)
        layout_turtle.forward(50)   # Height of the rectangle
        layout_turtle.right(90)
    layout_turtle.end_fill()
    layout_turtle.penup()
   
    # Write stat name and value in the rectangle
    layout_turtle.goto(110, y_position - 35)
    layout_turtle.color("black")
    layout_turtle.write(f"{stat}: {value}", font=("Arial", 14, "normal"))
    
    y_position -= 55  # Move down for the next stat box


# Draw the border rectangle
layout_turtle.penup()
layout_turtle.goto(-300, 250)
layout_turtle.pendown()
layout_turtle.pensize(5)
layout_turtle.color("black")
for _ in range(2):
    layout_turtle.forward(600)
    layout_turtle.right(90)
    layout_turtle.forward(500)
    layout_turtle.right(90)
layout_turtle.penup()

# Title
layout_turtle.goto(0, 260)
layout_turtle.write("Epic Text Adventure Character Sheet", align="center",
                    font=("Arial", 20, "bold"))

# Draw border with color for Race and Class
border_turtle = turtle.Turtle()
border_turtle.penup()
border_turtle.goto(80, 220)
border_turtle.pendown()
border_turtle.pensize(4)
border_turtle.color("black","lightgreen")
border_turtle.begin_fill()
for _ in range(2):
    border_turtle.forward(200)
    border_turtle.right(90)
    border_turtle.forward(80)
    border_turtle.right(90)
border_turtle.end_fill()
border_turtle.penup()


# Character Information Area (e.g., Race and Class)
layout_turtle.goto(100, 180)
layout_turtle.write("Race: Elf", font=("Arial", 16, "normal"))
layout_turtle.goto(100, 150)
layout_turtle.write("Class: Ranger", font=("Arial", 16, "normal"))

# Draw the character portrait rectangle
layout_turtle.goto(-275, 200)
layout_turtle.pendown()
layout_turtle.color("black")
layout_turtle.begin_fill()
layout_turtle.fillcolor("white")
for _ in range(2):
    layout_turtle.forward(150)
    layout_turtle.right(90)
    layout_turtle.forward(150)
    layout_turtle.right(90)
layout_turtle.end_fill()
layout_turtle.penup()

# Set the Character portrait Name label
layout_turtle.goto(-280, 210)
layout_turtle.color("black")
layout_turtle.write("Arannis the Brave", font=("Arial", 16, "italic"))


# Add the .gif image as a turtle shape for the character portrait
try:
    screen.addshape("character.gif")  # Add the GIF image as a turtle shape
except Exception as e:
    print("Image could not be loaded:", e)

# Character Portrait - Moved further down to avoid overlap with the character name
portrait_turtle = turtle.Turtle()
portrait_turtle.penup()
portrait_turtle.shape("character.gif")  # Set turtle shape to the resized gif image
portrait_turtle.goto(-200, 125)  # Adjusted position for the portrait
portrait_turtle.stamp()  # Stamp the image to keep it on screen

plot_zigzag()

# Hide the turtle
layout_turtle.hideturtle()
# Character Description Box at the bottom left with more precise formatting to fit
portrait_turtle.penup()
layout_turtle.goto(-280, -35)
layout_turtle.color("black", "orange")
layout_turtle.pendown()
layout_turtle.begin_fill()
for _ in range(2):
    layout_turtle.forward(350)  # Width of description box
    layout_turtle.right(90)
    layout_turtle.forward(160)  # Height of description box
    layout_turtle.right(90)
layout_turtle.end_fill()
layout_turtle.penup()

# Write character description text within the box with adjusted line breaks
layout_turtle.goto(-260, -190)
layout_turtle.color("black")
character_description = (
    "Arannis the Brave:\n"
    "\n"
    "Arannis is a skilled Elven Ranger,\n"
    "hailing from the ancient woods.\n"
    "With unmatched agility and a keen\n"
    "sense of wisdom, Arannis protects \n"
    "the realm from dark forces lurking \n"
    "beyond the forests."
)
layout_turtle.write(character_description, align="left", font=("Arial", 12, "bold"))
# Keep the window open

answer = clear_screen()

if answer == "yes":
    screen.bgcolor("lightblue")
    title("Turtle Adventure ")
    
    intro_text = """
    The moon hangs low in the ink-black sky, its pale
    light barely piercing the dense canopy of towering trees that
    surround you. Shadows dance and flicker with every subtle movement
    of the wind, their shapes twisting into eerie forms that vanish
    as quickly as they appear.

    You find yourself in the heart of an ancient forest. The air is
    heavy with the scent of damp earth and pine, and the soft crunch
    of fallen leaves beneath your feet is the only sound you can control.
    All around you, strange noises fill the night: a distant howl, the
    rustling of unseen creatures, and the occasional snap of a branch
    too close for comfort.

    You are alone—or at least, you think you are.

    Will you brave the unknown depths of the forest, or will fear
    root you to the spot?

    Your adventure begins now..."""

    display_text(intro_text)

answer = clear_screen()

turtle.done()