【问题标题】:AttributeError: 'Player' object has no attribute 'go_up'AttributeError:“玩家”对象没有属性“go_up”
【发布时间】:2021-06-08 21:25:34
【问题描述】:

当我在播放器类中明确定义播放器对象没有属性 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) not in walls:
        self.goto(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) not in walls:
        self.goto(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) not in walls:
        self.goto(move_to_x, move_to_y)










# Creating first map
map = [""]
map_1 = [
    "XXXXXXXXXXXXXXXXXXXXXXXXX",
    "XP       XXXXX   XXXXXXXX",
    "XXXXX    XXXXX   XXXXXXXX",
    "XXXXX    XXXXX   XXXXXXXX",
    "XX                      X",
    "XX          XXXXX  XXXXXX",
    "XX          XXXXX  XX  XX",
    "XX              X  XX  XX",
    "XX          XXXXX  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()

【问题讨论】:

    标签: python python-turtle


    【解决方案1】:

    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_down、go_right 等也是如此。

    【讨论】:

      【解决方案2】:

      缩进用于对绑定到类的函数进行分组,也称为成员方法。请记住,它们应该是您的 __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) not in walls:
                  self.goto(move_to_x, move_to_y)
      

      【讨论】:

        猜你喜欢
        • 2016-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-01
        • 2012-12-01
        • 2021-04-19
        • 2021-11-22
        • 1970-01-01
        相关资源
        最近更新 更多