私はPython 3で遊んでいて、RPGスタイルのシステムを組み合わせていますが、以前はPythonで実際に使っていないのでOOPに苦労しています。関数変数を使ってクラス変数にアクセスする
私は具体的には以下の機能で苦労しています:
def choosestat(statname, max, min):
# Takes the name of the stat
# as mentioned in the Player/Character class and
# a min/max value for the length. Allows the player
# to set their stats for the char.
print("Your stat choices are: " + str(stats))
choice = int(input("Please select a strength score ("+min+":"+max+")\n"))
if type(choice) == int and choice < (max+1) and choice > (min-1):
self.statname = stats[choice-1]
stats.pop(choice-1)
else:
print("Please select a valid option.\n")
これは、選択された量にplayerchar.strengthを設定するには、以下から引っ張っする必要があります。しかし
class Character(object):
# A Class for ALL characters - player, enemy etc.
def __init__(self, name, hp, armor, damage, strength,
intelligence, wisdom, dexterity, constitution, charisma,
inventory, profession):
self.name = name
self.hp = hp
self.armor = armor
self.damage = damage
self.strength = strength
self.intelligence = intelligence
self.wisdom = wisdom
self.dexterity = dexterity
self.constitution = constitution
self.charisma = charisma
self.inventory = inventory
self.profession = profession
class Player(Character):
# A class for the Player character only.
# We will use a few parameters to set initial variables
def __init__(self):
super().__init__(name="", hp=10, armor=10, damage=10, strength=7,
intelligence=7, wisdom=7, dexterity=7, constitution=7,
charisma=7, inventory=[], profession="")
maxhp = 10
level = 1
exp = 0
、ときに私がコードを実行すると、私は単純に取得します:
Traceback (most recent call last):
File "main.py", line 122, in <module>
menu()
File "main.py", line 53, in menu
gameloop()
File "main.py", line 73, in gameloop
statchoice()
File "main.py", line 108, in statchoice
choosestat(strength, 6, 1)
NameError: name 'strength' is not defined
完全なコードはここにあります:
import os
import random
import time
from utilities import roll
class Character(object):
# A Class for ALL characters - player, enemy etc.
def __init__(self, name, hp, armor, damage, strength,
intelligence, wisdom, dexterity, constitution, charisma,
inventory, profession):
self.name = name
self.hp = hp
self.armor = armor
self.damage = damage
self.strength = strength
self.intelligence = intelligence
self.wisdom = wisdom
self.dexterity = dexterity
self.constitution = constitution
self.charisma = charisma
self.inventory = inventory
self.profession = profession
class Player(Character):
# A class for the Player character only.
# We will use a few parameters to set initial variables
def __init__(self):
super().__init__(name="", hp=10, armor=10, damage=10, strength=7,
intelligence=7, wisdom=7, dexterity=7, constitution=7,
charisma=7, inventory=[], profession="")
maxhp = 10
level = 1
exp = 0
class Enemy(Character):
# a class for any enemy.
def __init__(self):
super().__init__(name=enemyname, hp=10, armor=10, damage=10,
strength=7, intelligence=7, wisdom=7, dexterity=7,
constitution=7, charisma=7, inventory=[],
profession="")
playerchar = Player()
# simply call playerchar rather than calling the Class each time
def menu():
# Running the Main Menu under a single Function.
# This is the simplest method of running the menu
while True:
print("|==================|")
print("Welcome to Py RPG!")
print("Please select an option!")
print("1. Start a Game\n")
print("2. Settings\n")
print("3. Quit")
choice = input("\n>")
if choice == 1 or choice == "start":
gameloop()
elif choice == 2 or choice == "settings":
settings()
elif choice == 3 or choice == "quit":
break
else:
print("Please select an option from the menu.")
def settings():
# Settings page for all amendments
# TODO - create a 'settings' file and have this read/amend it?
print("Nothing exists here at the moment!")
menu()
def gameloop():
# the main game loop, contains all calls to relevant functions
while True:
print("This is the main game")
print("Let's begin. What is your name?")
playerchar.name = input(">")
print("Well then, " + playerchar.name + ", let us begin.")
statchoice()
def choosestat(statname, max, min):
# Takes the name of the stat
# as mentioned in the Player/Character class and
# a min/max value for the length. Allows the player
# to set their stats for the char.
print("Your stat choices are: " + str(stats))
choice = int(input("Please select a strength score ("+min+":"+max+")\n"))
if type(choice) == int and choice < (max+1) and choice > (min-1):
self.statname = stats[choice-1]
stats.pop(choice-1)
else:
print("Please select a valid option.\n")
def displaystats(entity):
# quick function to display all relevant stats in-line.
print("Are you happy with your choices?\n")
print("Strength: " + str(entity.strength))
print("Intelligence: " + str(entity.intelligence))
print("Wisdom: " + str(entity.wisdom))
print("Dexterity: " + str(entity.dexterity))
print("Constitution: " + str(entity.constitution))
print("Charisma: " + str(entity.charisma))
def statchoice():
# Using the roll function, we get 6 ability scores, append them to 'stats',
# and run the choosestat function for each to set stats.
stats = []
stats.append(roll(4, 6))
stats.append(roll(4, 6))
stats.append(roll(4, 6))
stats.append(roll(4, 6))
stats.append(roll(4, 6))
stats.append(roll(4, 6))
choosestat(strength, 6, 1)
choosestat(intelligence, 5, 1)
choosestat(wisdom, 4, 1)
choosestat(dexterity, 3, 1)
choosestat(constitution, 2, 1)
choosestat(charisma, 1, 1)
displaystats(playerchar)
reroll = input("Do you wish to re-roll?")
if reroll == "yes" or reroll == "y":
statchoice()
menu()
エラーは何が間違っているかを正確に伝えます。あなたは 'statchoice()'に 'strength'を定義しませんでした。文字列 '' strength ''を渡すことを意味しましたか? – MooingRawr
あなたが問題を抱えていると主張している機能では、エラーは起こっていません。 –