为什么Python不连接到我的class属性?

2024-09-28 05:27:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个类似MMORPG的游戏,但它是单人游戏。我试图将玩家类放在一个脚本上,MaelstromPlayer,它的子类MaelstromMove使用另一个脚本连接到它,因为我很快就要添加MaelstromBattle和MaelstromInv类

问题是我无法访问,更不用说调整,MaelstromPlayer类属性。我想知道是什么导致了这个问题,以及如何解决它,或者甚至这是可能的

如果您想知道,我从这个网站https://mail.python.org/pipermail/python-list/2012-January/618880.html获得了“跨多个文件分发类”的方法

这是我为MaelstromPlayer编写的代码,该文件存储父类

class Player(object):
    def __init__(self):
        self.place = int("001")
        self.name = input("What name do you want?")
        self.knownplaces={}
        self.knownplaces[int("001")]="Ruby City"
        self.knownplaces[int("002")]="Ruby Inn"
        self.knownplaces[int("003")]="Ruby Forests"
        self.knownplaces[int("004")]="Ruby Countryside"
        self.knownplaces[int("005")]="Witch Hideout"
        self.mode="moving"
    def __str__(self):
        rep = self.movepossible

这是我为MaelstromMove编写的代码,我在其中存储PlayerMove的子类

import sys
sys.path.append('F:\Maelstrom\Python\MaelstromPlayer.py')
from MaelstromPlayer import Player

class PlayerMoving(Player):
    def __init__(Player):
        print('Welcome')
    def movepossible(Player,position):
        #001--Ruby City
        #002--Ruby Inn
        #003--Ruby Forests
        #004--Ruby Countryside
        #005--Witch Hideout
        if position==int("001"):
            possible=[int("002"),int("003")]
            return possible
        elif position==int("002"):
            possible=[int("001")]
            return possible
        elif position==int("003"):
            possible=[int("001"),int("004")]
            return possible
        elif position==int("004"):
            possible=[int("001"),int("003"),int("005")]
            return possible
        elif position==int("005"):
            possible=[int("004")]
            return possible
        else:
            return null
    def move(Player,position):
        if Player.mode=="moving":
            position=int(position)
            possiblewords=[]
            print('Choose between paths:')
            possible = Player.movepossible(position)
            for m in range(0,len(possible),1):
                possiblewords.append(Places.knownplaces[possible[m]])
            for n in range(0,len(possiblewords),1):
                print(str(n+1)+':'+str(possiblewords[n]))

            choice=input('Make your choice...')
                #choice=int(choice)
            if choice == '':
                choice = int('9999999999')
            if int(choice) <= len(possiblewords):
                Player.place=possible[int(choice)-int('1')]
            print("\n")
            print("\n")
    def showposition(Player):
        print('You are at '+Player.knownplaces[int(Player.place)])

test = Player()
while True:
    place = test.place
    test.move(place)
    test.showposition()

sys.path方法来自这里:http://www.daveoncode.com/2017/03/07/how-to-solve-python-modulenotfound-no-module-named-import-error/

请提供帮助,最好是代码示例,谢谢


Tags: selfreturnifdefpositionplaceintplayer
1条回答
网友
1楼 · 发布于 2024-09-28 05:27:02

您的问题是,您从不调用PlayerMoving类(用于底部的测试),只调用Player类。同样在PlayerMoving类中,您重写了Player类的__init__方法。要确保仍然调用Player.__init__,请执行以下操作:

class PlayerMoving(Player):
    def __init__(self):
        print('Welcome')
        super().__init__()

还有一些问题:

  • 不要使用int("001"),这是低效的、毫无意义的,而且是糟糕的做法
  • 用于创建您的dictdoself.knownplaces = {1: "Ruby City", 2: "Ruby Inn", ...} # etc
  • null没有在python中定义,也许您的意思是None,但是如果不返回任何其他内容,您将自动return None
  • 你的while循环永远不会结束。把一个break子句放在或什么东西里
  • 不要使用Player作为第一个方法参数。使用self就像在每个python类示例中一样1{a2}{a3}{a4}{a5}

最后,您是否注意到https://mail.python.org/pipermail/python-list/2012-January/618880.html中的整个线程都是关于您应该如何从不这样做的

相关问题 更多 >

    热门问题