乌龟对象不会移动到goto(x,y)的新位置



早上好,

我已经创建了一个乌龟对象(一个桨(,我打算从预定的位置从左向右移动。我正在使用乌龟.goto(x,y(来实现这一点,但它不起作用。我在函数go_left/go_right中添加了一条print语句,只是想看看代码是否正常工作,是的,新的x位置正在设置,但桨板不会移动。我不知道为什么会发生这种事。有什么想法吗?。谢谢你的帮助。

from turtle import Screen, Turtle
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor('black')
screen.title('Testing Paddle')
screen.tracer(0)
# Creating a paddle shape and registering it to the turtle shape custom list
screen.register_shape("paddle", ((0, 0), (15, 0), (15, 80), (0, 80)))
class Paddle(Turtle):
def __init__(self, position):
super().__init__()
self.shape('paddle')
self.color('magenta')
self.penup()
self.goto(position)
def go_left(self):
new_x = self.xcor() - 20
self.goto(new_x, self.ycor())
print(new_x, self.ycor())
def go_right(self):
new_x = self.xcor() + 20
self.goto(new_x, self.ycor())
print(new_x, self.ycor())
paddle = Paddle((-40, -240))
screen.listen()
game_is_on = True
screen.onkey(fun=paddle.go_left, key='Left')
screen.onkey(fun=paddle.go_right, key='Right')
screen.update()
screen.mainloop()

您设置

screen.tracer(0)

所以现在它不会自动更新屏幕,你必须自己做。

你必须使用

screen.update()

在改变位置之后。(每次goto()等之后(


编辑:

Turtle有一个问题:按键后,系统在启动前会有较长的延迟,将其视为重复按键,按键后球拍会有一定的延迟。这需要不同的方法来处理键。它需要使用onkeypress来提高速度(不移动桨(,使用onkeyrelease来降低速度。代码必须使用ontimer来重复代码,该代码将检查当前速度并移动围场。

from turtle import Screen, Turtle
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor('black')
screen.title('Testing Paddle')
screen.tracer(0)
# Creating a paddle shape and registering it to the turtle shape custom list
screen.register_shape("paddle", ((0, 0), (15, 0), (15, 80), (0, 80)))
class Paddle(Turtle):
def __init__(self, position, color='magenta'):
super().__init__()
self.shape('paddle')
self.color(color)
self.penup()
self.goto(position)
self.speed = 0

def on_press_left(self):
self.speed -= 20

def on_press_right(self):
self.speed += 20
def on_release_left(self):
self.speed += 20

def on_release_right(self):
self.speed -= 20
def update(self):
if self.speed != 0:
new_x = self.xcor() + self.speed

# pad width: 80

if new_x < -300:
new_x = -300
if new_x > 300 - 80:  
new_x = 300 - 80

self.goto(new_x, self.ycor())
# ---
def update():
enemy.speed = -paddle.speed  # moves enemy in different direction

paddle.update()
enemy.update()

screen.update()
screen.ontimer(fun=update, t=25)  # repeat after 25ms (0.025s)
# --- main ---
game_is_on = True
paddle = Paddle((-40, -240))
enemy  = Paddle((-40, 240+15), 'red')
screen.listen()
screen.onkeypress(fun=paddle.on_press_left, key='Left')
screen.onkeypress(fun=paddle.on_press_right, key='Right')
screen.onkeyrelease(fun=paddle.on_release_left, key='Left')
screen.onkeyrelease(fun=paddle.on_release_right, key='Right')
screen.ontimer(fun=update, t=25)  # execute after 25ms (0.025s)
screen.mainloop()

您也必须更新屏幕才能显示乌龟的新位置

将这一行添加到函数screen.update()的最后一行

修改后的代码

from turtle import Screen, Turtle
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor('black')
screen.title('Testing Paddle')
screen.tracer(0)
# Creating a paddle shape and registering it to the turtle shape custom list
screen.register_shape("paddle", ((0, 0), (15, 0), (15, 80), (0, 80)))
class Paddle(Turtle):
def __init__(self, position):
super().__init__()
self.shape('paddle')
self.color('magenta')
self.penup()
self.goto(position)
def go_left(self):
new_x = self.xcor() - 20
self.goto(new_x, self.ycor())
print(new_x, self.ycor())
screen.update() # edit

def go_right(self):
new_x = self.xcor() + 20
self.goto(new_x, self.ycor())
print(new_x, self.ycor())
screen.update() # edit
paddle = Paddle((-40, -240))
screen.listen()
game_is_on = True
screen.onkey(fun=paddle.go_left, key='Left')
screen.onkey(fun=paddle.go_right, key='Right')
screen.update()
screen.mainloop()

相关内容

最新更新