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
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)
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
Related
I am new to Python and trying to build a simple game. In the game is just one ant and this ant follows simple rules: if ant is on white field, he paints the tile black (or other color), turns right, moves forward. If ant is on colored tile, remove color, turn left, move forward.
Currently I am trying to do this with turtle and stamps. Stamp ID and coordinates are stored in a dict, a loop is checking if ant position is in dict keys and there is the problem: when the ant moves to the same position, where the first stamp is, this check return False and the ant never escape the loop.
from turtle import Turtle, Screen
screen = Screen()
screen.setup(height=900, width=1000)
screen.title("The Ant")
ant = Turtle()
ant.color("brown")
ant.pencolor("black")
ant.penup()
kleks_liste = {}
game_running = True
ant.shape("square")
ant.setpos(5, 5)
kleks_liste[ant.pos()] = ant.stamp()
ant.shape("classic")
ant.forward(20)
while game_running:
pos_key = ant.pos()
if pos_key in kleks_liste.keys():
ant.clearstamp(kleks_liste[pos_key])
kleks_liste.pop(pos_key)
ant.left(90.00)
ant.forward(20.00)
else:
ant.right(90.00)
ant.shape("square")
kleks_liste[pos_key] = ant.stamp()
ant.shape("classic")
ant.forward(20.00)
screen.exitonclick()
What am I doing wrong? If I comment out ant.forward in the else section, the check works and returns True. Can someone explain the behavior?
The Vec2D returned by pos() contains floats which need to be compared with an epsilon. One workaround is to cast these values to integers, either with int or round. This shouldn't lose accuracy since you're working on a grid.
Instead of pos_key = ant.pos(), try pos_key = tuple(map(int, ant.pos())). Don't forget to change the first stamp key outside the loop too.
As an aside, you can use if pos_key in kleks_liste: without .keys().
I am using turtle graphics in Python to make a game. I want to use these functions to make the spaceship (represented by a turtle) move left and right:
def moveL():
newpos = f.xcor() - 20
f.goto(newpos)
def moveR():
newpos = f.xcor() + 20
f.goto(newpos)
(Here, f is the turtle instance, representing a fighter ship.)
But I keep getting that error in the title. I tried putting both xcor's into parenthesis and putting "iter" before them, but that didn't fix it. Any help is appreciated.
I have stored turtle-graphics functions in list and am using random functions to call it to create a random-path, however code does not work.
Can someone please have a look on this and provide suggestion.
from turtle import Turtle
from turtle import Screen
import random
pen = Turtle()
pen.pensize(8)
pen.speed(10)
window = Screen()
window.colormode(255)
moves=[pen.forward(30),pen.backward(30)]
turns=[pen.right(90),pen.left(90)]
is_true = True
while is_true:
pen.color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
random.choice(turns)
random.choice(moves)
window.exitonclick()
I would say the issue here is that you are using functions as data when you could simply use data as data. That is, giving forward() a negative distance is the same as backward(). Giving left() a negative angle is the same as right(). So we can simply do:
from turtle import Screen, Turtle
from random import random, choice
DISTANCES = [30, -30]
ANGLES = [90, -90]
def move():
turtle.color(random(), random(), random())
turtle.left(choice(ANGLES))
turtle.forward(choice(DISTANCES))
screen.ontimer(move, 10)
screen = Screen()
turtle = Turtle()
turtle.pensize(8)
turtle.speed('fastest')
move()
screen.exitonclick()
I also dealt the next issue, your implicit while True:. The way you structured your code, the exitonclick() is never reached and doesn't work. Now it works as we've kept both the drawing and exitonclick() in the event loop.
You execute the methods only when you define the two list. Change the relevant part of the code like this
moves=[pen.forward, pen.backward]
turns=[pen.right, pen.left]
while True:
pen.color(random.randint(0,255), random.randint(0,255), random.randint(0,255))
random.choice(turns)(90)
random.choice(moves)(30)
The first problem is that you don't list function calls in the lists moves and turns but the results of the calls. The second problem is that you don't call functions after the random.choice calls. What you are actually getting from this is the flickering effect of the visible pen tip that endlessly changes the color.
On way to fix it is already shown in buran's answer. Another way that keeps the turn and move arguments out of the loop is the following, here lambda : transforms the function calls into anonymous functions to which references are stored in moves and turns:
Another option is to extract the actual moves and turns into functions
from turtle import Turtle
from turtle import Screen
import random
pen = Turtle()
pen.pensize(8)
pen.speed(10)
window = Screen()
window.colormode(255)
moves=[lambda : pen.forward(30), lambda : pen.backward(30)]
turns=[lambda : pen.right(90), lambda : pen.left(90)]
for _ in range(100):
pen.color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
random.choice(turns)()
random.choice(moves)()
window.exitonclick()
I decided to only draw 10 lines, so the turtle will most likely remain on screen. For a better way to get rid of the while True loop (including explanation) see cdlane's answer!
Drawing = input('''Select a drawing:
1. Square
2. Triangle
3. Star
''')
Colour = input("What colour would you like (red/green/blue)? ")
import turtle
if Drawing == 1:
import turtle
for i in range(5):
turtle.forward(150)
turtle.right(90)
turtle.color("{Colour}")
turtle.done()
Inputs give a string, not an integer, so the change would be simple. Make the 1 in your if statement a string by putting quote marks around it. if Drawing == "1".
As in having two Turtles moving at once. For example, I import two turtles, then try to have both of them move forward alongside each other. How can I do this?
bob = turtle.Turtle()
john = turtle.Turtle()
def move_turtles(ammount):
for i in range(ammount // 10):
bob.forward(10)
john.forward(10)
move_turtles(100)
There's no way to move them at the same time, although you can use something like that. It moves the turtles by 10 points each, so it gives the impression that they are moving together, but they are actually moving separately by little ammounts. It repeats the operation (ammount //10) times, and moves 10 in each iteration, so if you were to give 50 as an input, it would move 5 times 10 points, resulting in 50. You can then customize the function to move by a little a turtle so they don't overlap and so on.
You can move multiple turtles independently at the same time using timer events -- you can even have them move at different rates, both in time and space:
import turtle
turtle.setworldcoordinates(0, -100, 100, 100)
bob = turtle.Turtle(shape="turtle")
bob.penup()
bob.sety(20)
john = turtle.Turtle(shape="turtle")
john.penup()
john.sety(-20)
def move_bob():
bob.forward(1)
if bob.xcor() < 90:
turtle.ontimer(move_bob, 75)
def move_john():
john.forward(2)
if john.xcor() < 90:
turtle.ontimer(move_john, 100)
move_bob()
move_john()
turtle.exitonclick()
Other folks also use threads to achieve this but timer events are built into the turtle module.
There is a way to control frames using the Turtle module - you need to change the screen attributes.
screen = turtle.Screen()
screen.tracer(0)
This method makes all turtle movement invisible until you run screen.update(), and then every turtle will be visually updated at the same time on the screen. In your case, you can write screen.update() after the movement of both turtles, and they will appear to move at the same time.