138k views
1 vote
Write a program to draw ghosts on the screen. You must do this by writing a function called draw_ghost, which takes three parameters, the x location of the ghost, the y location of the ghost and the color of the ghost. X and y for the ghost define where the center of the head should go.

(CODEHS, PYTHON)

1 Answer

5 votes

Answer:

Here is an example of how you can draw a ghost using the Python turtle module:

import turtle

def draw_ghost(x, y, color):

# Move the turtle to the specified position

turtle.penup()

turtle.goto(x, y)

turtle.pendown()

# Set the turtle's color

turtle.color(color)

# Draw the ghost's head

turtle.begin_fill()

turtle.circle(50)

turtle.end_fill()

# Draw the ghost's eyes

turtle.penup()

turtle.goto(x - 20, y + 20)

turtle.pendown()

turtle.dot(10, "white")

turtle.penup()

turtle.goto(x + 20, y + 20)

turtle.pendown()

turtle.dot(10, "white")

# Draw the ghost's mouth

turtle.penup()

turtle.goto(x - 25, y - 25)

turtle.pendown()

turtle.width(10)

turtle.goto(x + 25, y - 25)

# Reset the turtle's state

turtle.penup()

turtle.home()

turtle.pendown()

turtle.width(1)

To use this function, you can call it with the desired x and y coordinates for the ghost's head, as well as the color you want the ghost to be. For example:

draw_ghost(100, 100, "purple")

draw_ghost(-100, -100, "orange")

turtle.done()

This will draw two ghosts, one at (100, 100) and one at (-100, -100), with the specified colors.

Step-by-step explanation:

User Limey
by
7.2k points