我试图用Python来解决OOP问题。通过教程我已被指示制作游戏。在本教程的前面,我使用函数制作了一个游戏,但现在我必须使用类和对象。
我有了自己的物品,但我不确定如何让物品移动。以下是它的要点:
class Scene(object):
def enter(self):
pass
class Scene1(Scene):
def enter(self):
print "Some text describing the scene bla bla"
def someFunction that requires raw_input and a correct answer
return 'Scene2'
class Scene2(Scene):
def enter(self):
print "Some text describing the scene bla bla"
def someFunction that requires raw_input and a correct answer
return 'Scene3'
class Scene3(Scene):
def enter(self):
print "Some text describing the scene bla bla"
def someFunction that requires raw_input and a correct answer
return 'finished'
游戏还有比这更多的东西,但我只是把它缩小到我遇到麻烦的部分。
教程作者提示使用另一个对象来运行游戏,一个"引擎"这将从scene1开始并贯穿每个场景直到完成'退回。他确实有一个例子,但这是一个更复杂的游戏,所以我试图忽略它。我想他希望我们使用while循环。
但while what?
我可以使用什么逻辑来运行游戏?我可以告诉你一个学习者。我是否拥有所需的所有物品?什么是最好的方式来运行"游戏?
答案 0 :(得分:1)
class Scene1:
def enter(self):
print "a scene"
raw_input("Hit Enter To Go To Next Scene")
return "Scene2"
class Scene2:
def enter(self):
print "a different scene"
raw_input("Hit Enter To Go Back:")
return "Scene1"
class Engine:
def __init__(self,sceneDict,startKey=1):
self._scenes = sceneDict
self._current = self._scenes[startKey]
def run(self):
while True:
next_scene_key = self._current.enter()
self._current = self._scenes[next_scene_key]
Engine({"Scene1":Scene1(),"Scene2":Scene2()},"Scene1").run()
是实现此目的的许多方法的一个例子
答案 1 :(得分:1)
首先,如果你在OOP项目中看到很多重复的代码,你很可能正在反对OOP而不是它。
不要为每个场景创建一个新类,而是为每个场景创建一个(场景的)对象。
这是一个如何实现“引擎”(最后4行)的一个非常简单的例子:
#! /usr/bin/python3
class Scene:
def __init__(self, name, welcome, question, answer):
self.name = name
self.welcome = welcome
self.question = question
self.answer = answer
self.connected = []
def connect(self, otherScene):
self.connected.append(otherScene)
def enter(self):
print(self.welcome)
while (input(self.question + ' ') != self.answer): pass #raw_input in py2
if not self.connected: return
print('\nWhither goest thou?')
for i, scene in enumerate(self.connected):
print('{}: {}'.format(i, scene.name))
while True:
try:
i = int(input('? '))
return self.connected[i]
except ValueError:
print('I understand thee not.')
except IndexError:
print('I understand thee not.')
#creating the scenes
house = Scene('A house',
'Thou art standing in front of a house. The landlord talketh to thee.',
'What is the top speed of an uncharged swallow?',
'42')
bridge = Scene('A bridge',
'Thou seest a bridge guarded by a knight clothèd in black.',
'What is the capital of England?',
'London')
swamp = Scene('A swamp',
'Thou enterst a foul morast and a witch eyeballth thee.',
'What is the capital of Azerbaijan?',
'Baku')
castle = Scene('A castle',
'The castle! A fair maiden greeteth thee.',
'What is my name?',
'Peach')
#connecting the scenes
house.connect(bridge)
house.connect(swamp)
bridge.connect(castle)
swamp.connect(castle)
#the game engine
nextScene = house
while nextScene:
nextScene = nextScene.enter()
print('Fin')
答案 2 :(得分:1)
创建一个包含所有场景的类。 run()
方法遍历所有场景并执行enter()
方法,该方法将同时输出print
和return
字符串。
class Engine:
def __init__(self):
self.scenes = [Scene1(),Scene2(),Scene3()]
def run(self):
for s in self.scenes:
print s.enter()
Engine().run()
输出:
Some text describing the scene bla bla
Scene2
Some text describing the scene bla bla
Scene3
Some text describing the scene bla bla
finished
更新评论:更动态地执行此操作:
class Scene1(Scene):
def enter(self):
print "Some text describing the scene bla bla"
raw = raw_input("Guess the number")
if raw != "42":
return 'Scene1'
return 'Scene2'
class Engine:
def __init__(self):
self.scenes = {'Scene1':Scene1(),'Scene2':Scene2(),'Scene3':Scene3()}
def run(self):
next = 'Scene1'
while True:
if next == 'finished':
break;
next = self.scenes.get(next).enter()
Engine().run()