猿问

Python 缺少参数

我的错在哪里


“这段代码只是我代码的一部分,我只是复制了其中的一部分”


import turtle


wn=turtle.Screen()

wn.bgcolor("black")

wn.title("Pacman")

wn.setup(900,700)

class Pacman(turtle.Turtle):

    def __init__(self):

        turtle.Turtle.__init__(self)

        self.shape("square")

        self.color("yellow")

        self.penup()

        self.speed(0)

    def up(self):

        self.goto(self.xcor(),self.ycor()+24)

    def down(self):

        self.goto(self.xcor(),self.ycor()-24)

    def left(self):

        self.goto(self.xcor()-24,self.ycor())

    def right(self):

        self.goto(self.xcor()+24,self.ycor())


wn.listen()

wn.onkey(Pacman.down, "Down")

wn.onkey(Pacman.up, "Up")

wn.onkey(Pacman.right, "Right")

wn.onkey(Pacman.left, "Left")

wn.tracer(0)


while True:

   wn.update()

失败


Exception in Tkinter callback

Traceback (most recent call last):

File "C:\Users\asus\AppData\Local\Programs\Python\Python37- 

32\lib\tkinter\__init__.py", line 1702, in __call__


return self.func(*args)


File "C:\Users\asus\AppData\Local\Programs\Python\Python37- 

32\lib\turtle.py", line 686, in eventfun


fun()

TypeError: up() missing 1 required positional argument: 'self'

当我单击右、下、上或左按钮方块不动并且在控制台中写入此失败时


千巷猫影
浏览 208回答 3
3回答

繁星淼淼

“Pacman”是一个类模板,所以你不能像那样调用它的方法。您应该创建一个 Pacman 对象(像这样):new_pacman = Pacman()然后你可以像这样运行你的函数:new_pacman.up()或者(如果我没记错的话)你可以试试这个,我认为这也应该有效:Pacman().up() which

qq_笑_17

做pacman_instance = Pacman()正确的事wn.listen()问题是您正在尝试对类而不是类的实例进行操作

呼啦一阵风

如果你只是Pacman = Pacman()在 line 之前添加wn.listen(),它会起作用!问题是您正在尝试访问未实例化的类,并且使用此行创建此实例,一切都会完美运行;)
随时随地看视频慕课网APP

相关分类

Python
我要回答