乌龟为什么不转?
Why is the turtle not turning?
我的乌龟不会随着我的按键移动。当您删除 self.forward 部分时,它会很好地转动,但是当它移动时它不起作用。我怎样才能让乌龟在移动时转动。另外,之前在我的代码中不起作用的是什么。此外,未加载标记为背景的图像。我该如何解决?谢谢。代码:
import turtle
import turtle as trtl
color = input("Please select color of turtle: ")
# classes
class enemy(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self, shape='square')
self.color('white')
self.turtlesize(2)
self.penup()
self.goto(0, 240)
class player(turtle.Turtle):
def __init__(self, x=True):
turtle.Turtle.__init__(self, shape='turtle')
self.x = x
self.color(color)
self.turtlesize(2)
self.hideturtle()
self.penup()
self.goto(4.440892098500626e-14, -180.0)
self.setheading(90)
self.showturtle()
while self.x:
self.forward(10)
self.speed('slowest')
def turning_left(self):
self.x = False
self.left(30)
self.x = True
def turning_right(self):
self.x = False
self.right(30)
self.x = True
enemy1 = enemy()
shooter = player()
# controls
wn = trtl.Screen()
wn.onkeypress(shooter.turning_right, 'Right')
wn.onkeypress(shooter.turning_left, 'Left')
wn.onkeypress(shooter.turning_right, 'd')
wn.onkeypress(shooter.turning_left, 'a')
wn.listen()
wn.bgpic('backround.gif')
turtle.done()
下面是对您的代码的进一步修改的简化,使乌龟不断向前移动,但您可以通过键盘让它转动:
from turtle import Screen, Turtle
screen = Screen()
class player(Turtle):
def __init__(self):
super().__init__(shape='turtle')
self.hideturtle()
self.turtlesize(2)
self.setheading(90)
self.penup()
self.backward(180.0)
self.showturtle()
self.move()
def move(self):
self.forward(1)
screen.ontimer(self.move)
def turn_left(self):
self.left(15)
def turn_right(self):
self.right(15)
shooter = player()
screen.onkeypress(shooter.turn_right, 'Right')
screen.onkeypress(shooter.turn_left, 'Left')
screen.listen()
screen.mainloop()
我的乌龟不会随着我的按键移动。当您删除 self.forward 部分时,它会很好地转动,但是当它移动时它不起作用。我怎样才能让乌龟在移动时转动。另外,之前在我的代码中不起作用的是什么。此外,未加载标记为背景的图像。我该如何解决?谢谢。代码:
import turtle
import turtle as trtl
color = input("Please select color of turtle: ")
# classes
class enemy(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self, shape='square')
self.color('white')
self.turtlesize(2)
self.penup()
self.goto(0, 240)
class player(turtle.Turtle):
def __init__(self, x=True):
turtle.Turtle.__init__(self, shape='turtle')
self.x = x
self.color(color)
self.turtlesize(2)
self.hideturtle()
self.penup()
self.goto(4.440892098500626e-14, -180.0)
self.setheading(90)
self.showturtle()
while self.x:
self.forward(10)
self.speed('slowest')
def turning_left(self):
self.x = False
self.left(30)
self.x = True
def turning_right(self):
self.x = False
self.right(30)
self.x = True
enemy1 = enemy()
shooter = player()
# controls
wn = trtl.Screen()
wn.onkeypress(shooter.turning_right, 'Right')
wn.onkeypress(shooter.turning_left, 'Left')
wn.onkeypress(shooter.turning_right, 'd')
wn.onkeypress(shooter.turning_left, 'a')
wn.listen()
wn.bgpic('backround.gif')
turtle.done()
下面是对您的代码的进一步修改的简化,使乌龟不断向前移动,但您可以通过键盘让它转动:
from turtle import Screen, Turtle
screen = Screen()
class player(Turtle):
def __init__(self):
super().__init__(shape='turtle')
self.hideturtle()
self.turtlesize(2)
self.setheading(90)
self.penup()
self.backward(180.0)
self.showturtle()
self.move()
def move(self):
self.forward(1)
screen.ontimer(self.move)
def turn_left(self):
self.left(15)
def turn_right(self):
self.right(15)
shooter = player()
screen.onkeypress(shooter.turn_right, 'Right')
screen.onkeypress(shooter.turn_left, 'Left')
screen.listen()
screen.mainloop()