这个代码应该使用左右箭头键将播放器向左和向右移动,但是当我尝试按箭头键时,播放器正在消失。我该怎么解决这个问题?你知道吗
import turtle
wn=turtle.Screen()
wn.title("falling skies")
wn.bgcolor("pink")
wn.setup(width=800,height=600)
wn.tracer(0)
#add player
player = turtle.Turtle()
player.speed(0)
player.shape("square")
player.color("blue")
player.penup()
player.goto(0,-250)
player.direction="stop"
#functions
def go_left():
player.direction="left"
def go_right():
player.direction="right"
#keyboard
wn.listen()
wn.onkeypress(go_left,"Left")
wn.onkeypress(go_right,"Right")
while True:
wn.update()
if player.direction == "left":
x = player.xcor()
x -= 3
player.setx(x)
if player.direction == "right":
x = player.xcor()
x += 3
player.setx(x)
wn.mainloop()
File "C:\Users\Harshitha.P\AppData\Local\Programs\Python\Python37\mine.py", line 34, in player.setx(x) File "C:\Users\Harshitha.P\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 1808, in setx self._goto(Vec2D(x, self._position[1])) File "C:\Users\Harshitha.P\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 3158, in _goto screen._pointlist(self.currentLineItem), File "C:\Users\Harshitha.P\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 755, in _pointlist cl = self.cv.coords(item) File "", line 1, in coords File "C:\Users\Harshitha.P\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 2469, in coords self.tk.call((self._w, 'coords') + args))] _tkinter.TclError: invalid command name ".!canvas"
对@patel的另一种解释可能与YouTube上的“坠落的天空”视频turtorial一致,那就是让播放器自己移动,直到到达窗口的一边,或者另一边,然后停止:
相关问题 更多 >
编程相关推荐