Question

I have a hangman game, im suppose to draw one part of the hangman every wrong letter, but I do not know how to draw one step of the turtle at a time

That's what I have:

def drawsturtle(userInput,word):

then the steps I have to make:

import turtle
hangman = turtle.Turtle()
hangman.circle(45)
hangman.right(90)
hangman.forward(150)
....

How can I write the code so that every userInput not in word one of those steps gets drawn? Thanks everyone

Était-ce utile?

La solution

If you defined a count variable to track the number of incorrect guesses, you could define a function to draw out the required parts. In the example below I assumed 3 incorrect guesses. I've added a 3 second pause so you can see the output.

import turtle, time

hangman = turtle.Turtle()

def draw_hangman(count):
    if count >= 1:
        hangman.circle(45)
    if count >= 2:   
        hangman.right(90)
    if count == 3:
        hangman.forward(150)
    time.sleep(3)

draw_hangman(3)

Autres conseils

Once you have code to detect that an incorrect letter has been guessed, you can call the turtle.write method to write out the incorrect letter.

Refer to the method signature below:

 turtle.write(arg, move=False, align="left", font=("Arial", 8, "normal"))
    Parameters: 

        arg – object to be written to the TurtleScreen
        move – True/False
        align – one of the strings “left”, “center” or right”
        font – a triple (fontname, fontsize, fonttype)

    Write text - the string representation of arg - at the current turtle position according to align (“left”, “center” or right”) and with the given font. If move is true, the pen is moved to the bottom-right corner of the text. By default, move is False.

For more details:

https://docs.python.org/2.7/library/turtle.html

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top