我一直在Learn Python the Hard Way工作,目前我在你需要编写自己的基于文本的游戏的部分。我从作者提供的游戏中获取代码并“抽象”它,直到我得到一种模板,我可以导入到我自己的游戏文件中。我想使用模板模块中的类和方法,只是提供特定于我的游戏的细节。这样,我就不必重写模板中的所有内容。我正在使用的模板如下所示:
""" This module provides some classes for constructing an interactive,
text-based game. You should ideally subclass what's provided here, and
override methods or provide new methods as you see fit.
"""
from sys import exit
from random import randint
# TODO: keep Scene class and Map class here, move everything else outside and just import (?)
# TODO: move dialogs to text files (?)
# TODO: remove 'random' import, 'sys' probably needs to stay
class Scene(object):
""" A basic class describing a scene in your game. You should subclass this and implement your own version of enter(). """
def enter(self):
""" Play out the scene and return a string key referring to the next scene."""
print "This scene is not yet configured. Subclass it and implement enter()."
exit(1)
class Engine(object):
""" The main engine that runs the actual game. This is where scenes are
stepped through. You could add a system to change the starting point of
your game depending on a save state, etc.
"""
# initialize the engine with a map of scenes
def __init__(self, scene_map):
""" Initialize the engine with a map of all scenes in your game. """
self.scene_map = scene_map
def play(self):
""" Start the game with the map set at the opening scene.
Also grab the final scene of your game. Continue through the game
as long as you aren't at the final scene. Then, make sure you
actually play the final scene.
"""
# current_scene is the results of scene_map's opening_scene()
current_scene = self.scene_map.opening_scene()
# last_scene is grabbing the next scene, with param of 'finished'
last_scene = self.scene_map.next_scene('scene')
# while you're not on the final scene/room
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
# be sure to print out the last scene
current_scene.enter()
# TODO: abstract this class
class Map(object):
""" Essentially holds all the scenes for your game and provides methods for grabbing the opening scene and next scene.
Subclass this and provide your own scenes.
"""
#scenes = {"scene": Scene()}
def __init__(self, start_scene, scenes={"scene": Scene()}):
# set the dictionary of scenes
self.scenes = scenes
""" Initialize the map with whatever your starting scene is. """
self.start_scene = start_scene
def next_scene(self, scene_name):
""" Get the value of scene_name and return it. """
val = self.scenes.get(scene_name)
return val
def opening_scene(self):
""" Return the scene your game will start with. """
return self.next_scene(self.start_scene)
a_map = Map('scene')
a_game = Engine(a_map)
a_game.play()
我自己的游戏文件如下:
from sys import exit
from random import randint
from game_template import *
scenes = {
"RudeWakeUp": RudeWakeUp(),
"Fridge": Fridge(),
"PlugFix": PlugFix(),
"PlumbingFix": PlumbingFix(),
"FridgeRelocation": FridgeRelocation(),
"Final": Final(),
"Fail": Fail()
}
class Fail(Scene):
def enter(self):
print "Well, it looks like the fridge is staying right where it is."
exit(1)
class RudeWakeUp(Scene):
pass
class Fridge(Scene):
pass
class PlugFix(Scene):
pass
class PlumbingFix(Scene):
pass
class FridgeRelocation(Scene):
pass
class Final(Scene):
pass
# TEST AREA
game_map = Map('Fail', scenes)
game = Engine(game_map)
game.play()
我目前只测试Fail()
。问题是当我执行我的游戏文件时,我从模板中的Scene()基类而不是Fail()
获得输出。这是因为我导入错误,继承不正确,还是其他什么?我以前在python中做过编程,但是已经有一段时间了,因为我已经做了很多重要的事情,我可能已经忘记了关于类如何工作的一些事情。任何帮助将不胜感激。
答案 0 :(得分:0)
很久以前,您可能已经了解到python是一种声明性语言,并且代码是逐行执行的。
class Fail(Scene):
def enter(self):
print "Well, it looks like the fridge is staying right where it is."
exit(1)
scenes = {
"RudeWakeUp": RudeWakeUp(),
"Fridge": Fridge(),
"PlugFix": PlugFix(),
"PlumbingFix": PlumbingFix(),
"FridgeRelocation": FridgeRelocation(),
"Final": Final(),
"Fail": Fail()
}
在分配该类之前,只需更改顺序以便声明Fail
即可解决问题。实际上,您也可以在声明所有其他类之后设置scenes
。