微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AttributeError: 'Player' 对象没有属性 'go_up'

如何解决AttributeError: 'Player' 对象没有属性 'go_up'

当我在播放器类中明确定义播放器对象没有属性 go_up 时,我不断收到错误消息。

Error message: File "C:\Users\mctri\OneDrive\Desktop\Maze
game\src\main.py",line 134,in <module>
        turtle.onkey(player.go_up,"Up")
    AttributeError: 'Player' object has no attribute 'go_up'
import turtle
import random
import math
from tkinter import messageBox
import sys
import winsound

# Creating the window to display maze

win = turtle.Screen()
win.title("Maze game")
win.bgcolor("grey")
win.setup(width=900,height=900)


#Register shapes
turtle.register_shape("Images/wall.gif")
turtle.register_shape("Images/brick.gif")
turtle.register_shape("Images/brick1.gif")

# Creating blocks class
class Blocks(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape("Images/brick1.gif")
        self.penup()
        self.speed(0)
        messageBox.showinfo("Story","Welcome to an adventure"
                                     "To a scary adventure")

#Creating player class
class Player(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape("circle")
        self.color("red")
        self.penup()
        self.speed(0)
        self.gold = 0

#Player movement
def go_up(self):
    move_to_x = player.xcor()
    move_to_y = player.ycor() + 24
    if (move_to_x,move_to_y) not in walls:
        self.goto(move_to_x,move_to_y)

def go_down(self):
    move_to_x = player.xcor()
    move_to_y = player.ycor() - 24
    if (move_to_x,move_to_y)

def go_right(self):
    move_to_x = player.xcor() + 24
    move_to_y = player.xcor()
    if (move_to_x,move_to_y)

def go_left(self):
    move_to_x = player.xcor() -24
    move_to_y = player.xcor()
    if (move_to_x,move_to_y)










# Creating first map
map = [""]
map_1 = [
    "XXXXXXXXXXXXXXXXXXXXXXXXX","XP       XXXXX   XXXXXXXX","XXXXX    XXXXX   XXXXXXXX","XX                      X","XX          XXXXX  XXXXXX","XX          XXXXX  XX  XX","XX              X  XX  XX","XX     XX   XXXXX  XX  XX","XX     XX              XX","XX     XX     XXXXXXX  XX","XX     XX    TXXXXXXX  XX","XXXXXXXXX   XXXXXXX    XX","XXXXXXXXX        XXXXXXXX","X                      XX","X  XX     XXXXXXXXX    XX","X  XX     XXX    XX    XX","X  XX    TXXX    XX    XX","XXXXX   XXXXX          XX","XXXXX   XXXXX    XX    XX","XXXXX              XXXXXX","XX      XXXXXX     XXXXXX","XX      XXXXXX        XXX","XXXXXXXXXXXXXXXXXXXXXXXXX"
]

#Creating map setup function
def setup_maze(map):
    for y in range(len(map)):
        for x in range(len(map[y])):
            character = map[y][x]
            screen_x = -288 + (x * 24)
            screen_y = 288 - (y * 24)

            if character == "X":
                blocks.goto(screen_x,screen_y)
                blocks.stamp()

            if character == "P":
                player.goto(screen_x,screen_y)






#Append map
map.append(map_1)

#Creating class instance
blocks = Blocks()
player = Player()

#Keybinding
turtle.listen()
turtle.onkey(player.go_up,"Up")
turtle.onkey(player.go_down,"Down")
turtle.onkey(player.do_left,"Left")
turtle.onkey(player.go_right,"Right")

#Setup maze map
setup_maze(map[1])

#Create Walls
walls = []

turtle.done()

解决方法

go_up 实际上并不在 Player 类中,因为它的缩进:

class Player(turtle.Turtle):
    def __init__(self):
        ...

#Player movement
def go_up(self):
    ...

它必须与 __init__ 具有相同的缩进级别才能被视为 Player 类的一部分:

class Player(turtle.Turtle):
    def __init__(self):
        ...

    #Player movement
    def go_up(self):
        ...

同样适用于 go_downgo_right 等。

,

缩进用于对绑定到类的函数进行分组,也称为成员方法。请记住,它们应该是您的 __init__() 类构造函数的兄弟。

class Player(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape("circle")
        self.color("red")
        self.penup()
        self.speed(0)
        self.gold = 0
    
    #Player movement
    def go_up(self):
        move_to_x = player.xcor()
        move_to_y = player.ycor() + 24
        if (move_to_x,move_to_y) not in walls:
            self.goto(move_to_x,move_to_y)
    
    def go_down(self):
        move_to_x = player.xcor()
        move_to_y = player.ycor() - 24
        if (move_to_x,move_to_y)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。