I am trying to make a game similar to a MMORPG but is single player. I am trying to put the Player class on one script, the MaelstromPlayer, and it's child class MaelstromMove to connect to it using another script because I am adding the MaelstromBattle and MaelstromInv classes soon.
The problem is that I could not access, not to mention adjust, the MaelstromPlayer class attributes. I wonder what caused this problem and how to solve it, or even if this is possible.
In case you were wondering, I got the method of "Distributing A Class Across Multiple Files" from this website https://mail.python.org/pipermail/python-list/2012-January/618880.html .
This is my code for MaelstromPlayer, the file where I store the parent class.
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
This is my code for MaelstromMove, where I store the child class of 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()
The sys.path method is from here: http://www.daveoncode.com/2017/03/07/how-to-solve-python-modulenotfound-no-module-named-import-error/ .
Please help, preferably with a code sample, thank you.
Your problem is that you never call the the PlayerMoving
class (for your test at the bottom), just the Player
class. Also in the PlayerMoving
class you override the Player
class' __init__
method. To make sure the Player.__init__
is still called, do something like this:
class PlayerMoving(Player):
def __init__(self):
print('Welcome')
super().__init__()
Some additional problems:
int("001")
, it's inefficient, pointless and just bad practice.dict
do self.knownplaces = {1: "Ruby City", 2: "Ruby Inn", ...} # etc
null
isn't defined in python, perhaps you mean None
but you'll automatically return None
if you don't return anything else. while
loop will never end. Put a break
clause in or something.Player
as the first method argument. Use self
like you'll find in every python class example ever 1 2 3 4 5 .Finally, have you noticed that that entire thread from https://mail.python.org/pipermail/python-list/2012-January/618880.html is about how you should never do this?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.