这是调用一切的主要内容
import GameClass
def main ():
game = GameClass
game.Run()
#GameClass.Run()
main()
当运行调用它出现的所有内容的main
函数时出现错误:"
Traceback (most recent call last):
File "C:\Python34\D & D game\D&D game.py", line 13, in <module>
main()
File "C:\Python34\D & D game\D&D game.py", line 10, in main
game.Run()
AttributeError: 'module' object has no attribute 'Run'
不完全确定该怎么做。
GameClass
正在调用
import random
import EnemyBaseClass
import PlayerClass
import OgreClass
import TrollClass
import DragonClass
import GoblinClass
#Game Class
class GameClass():
def __init__(self):
self.PlayerCharacter = PlayerClass.Player()
self.Location = ['You have entered the frozen slopes of the ice everyglades',
'You have entered the enchanted forest of the elves',
'You decided to enter the dragons mountains',
'you saild across the frozen seas of everyfrost',
'Your home has become covered ina thick fog',
'You come across a small village']
self.Score = 0
self.Enemies = [TrollClass.Troll(), OgreClass.Ogre(), DragonClass.Dragon(), GoblinClass.Goblin()]
self.EnemySelected = self.Enemies[0]
def PlayerAttack(self):
self.PlayerCharacter.DetermineDamage()
self.Enemies[self.EnemySelected].Life -= self.PlayerCharacter.Damage
if self.PlayerCharacter.Damage == 0:
print("The player Goes for a strike but misses.")
else:
print("The player attacks and Does " +str(self.PlayerCharacter.Damage) + "damage.")
self.Score +=10
print("The " + self.Enemies[self.EnemiesSelected].Name + " now has " + str(self.Enemies[self.EnemySelected].Life))
def EnemyAttack(self):
self.Enemies[self.EnemySelected].DetermineDamage()
self.PlayerCharacter.Life -= self.Enemies[self.EnemySelected].Damage
if self.Enemies[self.EnemiesSelected].Damage == 0:
print("The " + self.Enemies[self.EnemySelected].Name + " goes for a strike but misses.")
else:
print("The " + self.Enemies[self.EnemySelected].Name + " attacks and does " + str(self.Enemies[self.EnemySelected].Damage) + "damage.")
print("The player now has " + str(self.PlayerCharacter.Life) + " life left.")
def Battle (self):
print ("A Great battle begins!")
self.EnemySelected = random.randint ( 0, len(self.Location) - 1 )
self.Enemies[self.EnemySelected].DetermineLife()
while self.PlayerCharacter.life > 0 and self.Enemies[self.EnemySeleced].Life > 0:
self.PlayerAttack()
self.EnemyAttack()
def TravelToLocation(self):
print (self.Location[random.randint( 0, len(self.Location) - 1 )])
def Login(self):
print("You must login to play the game")
def ReadFile(self):
print ("Readig File")
def VerifyLogin(self):
print("Saving your game")
def WriteFile(self):
print("Saving You Game")
def Over(self):
print ("Game Over Your Score was " + Str(self.Score_ + "Points"))
def Run(self):
while self.PlayerCharacter.GetLife() > 0:
self.PlayerCharacter.Upkeep()
self.TravelToLocation()
self.Battle()
self.WriteFile()
self.Over()
答案 0 :(得分:1)
重新阅读追溯:
AttributeError: 'module' object has no attribute 'Run'
您import
模块 GameClass
,其中是类 GameClass
。您想要实例化并调用后者上的方法。之一:
import GameClass # import the module
GameClass.GameClass().Run() # access class via module
或:
from GameClass import GameClass # import class
GameClass().Run() # access class directly
或者,如果你遵循Python的style guide:
from game import Game
Game().run()