我目前正在尝试在Visual Studio Code中学习python。我正在使用python 3.8.2,我正在遵循一个可靠的教程来帮助我更好地学习python(我在视觉上学得最好。)我正在尝试的项目是“太空侵略者”游戏,但我始终遇到问题错误代码为(模块'turtle'没有'listen'成员')和('模块'turtle'没有'onkey'成员)行。不是全部都在一行上,也可以在其中导入turtle,它不会复制到代码中。相信我,它在那里。我还安装了python扩展。[Python错误] [1]
import os
#Set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Space Invaders")
#Draw boarder
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)
for side in range (4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()
#Create the player turtle
player =turtle.Turtle()
player.color('blue')
player.shape('triangle')
player.penup()
player.speed(0)
player.setposition(0, -250)
player.setheading(90)
playerspeed = 15
#Move the player left and right
def move_left():
x = player.xcor()
x-=playerspeed
player.setx(x)
#Create keyboard bindings
turtle.listen()
turtle.onkey(move_left, "Left")
wn.mainloop() ```
[1]: https://i.stack.imgur.com/6u7IB.png
答案 0 :(得分:0)
您需要import turtle
靠近顶部,然后取消缩进:
turtle.listen ()
turtle.onkey(move_left, "Left")
为我工作;)