【发布时间】:2021-11-12 02:09:43
【问题描述】:
我的乌龟不会随着我的按键而移动。当您删除 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()
【问题讨论】:
-
__init__()仅在您启动播放器时被调用一次 -
你想要的是一个游戏循环gameprogrammingpatterns.com/game-loop.html