Code:
import turtle
import random
import time
s = turtle.getscreen()
turtle.screensize(canvwidth=400, canvheight=400)
t = turtle.Turtle()
t.pensize(0)
t.shape('square')
t.color("black")
t.speed(0)
t.penup()
def moveu(num):
t.setheading(num)
t.forward(20)
s.onkey(lambda : moveu(90), 'w')
s.onkey(lambda : moveu(270), 's')
s.onkey(lambda : moveu(180), 'a')
s.onkey(lambda : moveu(0), 'd')
s.listen()
I am not close to done with this project but I have run into some problems. I want to create a game in the turtle
module. But I don't know how to prevent the block from moving backward. I have seen other people use t.direction
or something. But I have tried that and it didn't really work, maybe I'm just stupid and I did something wrong. How can I prevent the square from moving in the opposite direction?
right() and left() functions To change the direction the turtle is facing, you can use either the right() or left() function. These functions only work when you pass in a number value that specifies the number of degrees to turn.
left() method is used to change the direction of the turtle by the value of the argument that it takes. It gives the moving of the head of the turtle in a direction. The argument it takes is angle { a number (integer or float) }. So, it turns turtle left by angle units.
Just add turtle. done() at the end of your "Turtle code". With the same indentation as the turtle commands.
If you do not specify otherwise, the turtle starts its journey by pointing to the right, and the turtle can only move in the direction that it is facing. If you want the forward () function to move the turtle in a different direction, then you first have to specify the direction the turtle should be facing.
Maintain Direction of Travel. Always move a turtle in the same direction it was traveling when you saw it. Place the turtle at least 30 feet from the road (not on the roadside), so if startled by the experience, the turtle does not get disoriented and accidentally run back into the roadway, or freeze and get run over.
To change the direction the turtle is facing, you can use either the right () or left () function. These functions only work when you pass in a number value that specifies the number of degrees to turn. Let’s see a few examples of how to move the turtle up, down, left, and right using the right () and left () functions.
Turns the turtle left by angle. If degreeshas been called (the default), anglewill be used as a degree measure; if radianshas been called, anglewill be interpreted as a measure in radians. right(angle)
You can add the condition if (t.heading() + 180) % 360 != num:
, meaning if the opposite direction of the turtle's current heading direction isn't the direction the number passed into the function, then proceed:
import turtle
import random
import time
s = turtle.getscreen()
turtle.screensize(canvwidth=400, canvheight=400)
t = turtle.Turtle()
t.pensize(0)
t.shape('square')
t.color("black")
t.speed(0)
t.penup()
def moveu(num):
if (t.heading() + 180) % 360 != num:
t.setheading(num)
t.forward(20)
s.onkey(lambda : moveu(90), 'w')
s.onkey(lambda : moveu(270), 's')
s.onkey(lambda : moveu(180), 'a')
s.onkey(lambda : moveu(0), 'd')
s.listen()
turtle.mainloop()
I don't know how to prevent the block from moving backward.
As it is, your block is only moving forward. If you change the shape of your block from 'square'
to 'arrow'
you'll see that it is always moving in the direction it is facing. If you desire to limit it in some manner, we'll need a better description of what types of motion you want or don't want.
A simplified version of your code with some minor tweaks:
from turtle import Screen, Turtle
def moveu(angle):
turtle.setheading(angle)
turtle.forward(20)
screen = Screen()
screen.setup(400, 400)
turtle = Turtle()
turtle.shape('arrow')
turtle.speed('fastest')
turtle.penup()
screen.onkey(lambda: moveu(90), 'w')
screen.onkey(lambda: moveu(270), 's')
screen.onkey(lambda: moveu(180), 'a')
screen.onkey(lambda: moveu(0), 'd')
screen.listen()
screen.mainloop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With