我没有完全理解课程。我已经阅读了python文档和其他一些教程。我了解了它的基本要点,但不了解细微差别。例如在我的代码中:
class whiteroom(): """ Pick a door: red, blue, green, or black. """ do = raw_input("> ") if "red" in do: print "You entered the red room." elif "blue" in do: print "You entered the blue room." elif "green" in do: print "You entered the green room." elif "black" in do: print "You entered the black room." else: print "You sit patiently but slowly begin to stave. You're running out of time." return whiteroom() game = whiteroom() game
(原始键盘)
我想回到教室的白色房间。这是不可能的,或者是做不正确的。如果您能弄清楚如何返回一个类,或如何将两个类“链接”在一起,以便在白色空间上重复其他房间,并且在调用时返回其他房间(将成为类),那就太好了。
另外,我非常不稳定,__init__仍然不确定其目的是什么。每个人都不断告诉我它“初始化”了,我敢肯定,但是,这似乎并没有帮助我的大脑。
__init__
函数与类有很大的不同。好像您已使用一个函数,并将其更改def为class。我猜这在您的情况下 最 有效,但并不是应该上课的方式。
def
class
类包含函数(方法)和数据。例如,您有一个球:
class Ball(object): # __init__ is a special method called whenever you try to make # an instance of a class. As you heard, it initializes the object. # Here, we'll initialize some of the data. def __init__(self): # Let's add some data to the [instance of the] class. self.position = (100, 100) self.velocity = (0, 0) # We can also add our own functions. When our ball bounces, # its vertical velocity will be negated. (no gravity here!) def bounce(self): self.velocity = (self.velocity[0], -self.velocity[1])
现在我们有一Ball堂课。我们如何使用它?
Ball
>>> ball1 = Ball() >>> ball1 <Ball object at ...>
它看起来不是很有用。数据是有用的地方:
>>> ball1.position (100, 100) >>> ball1.velocity (0, 0) >>> ball1.position = (200, 100) >>> ball1.position (200, 100)
好吧,很酷,但是与全局变量相比有什么优势?如果您还有另一个Ball实例,它将保持独立:
>>> ball2 = Ball() >>> ball2.velocity = (5, 10) >>> ball2.position (100, 100) >>> ball2.velocity (5, 10)
并ball1保持独立:
ball1
>>> ball1.velocity (0, 0)
现在bounce我们定义的那个方法(类中的函数)呢?
bounce
>>> ball2.bounce() >>> ball2.velocity (5, -10)
该bounce方法导致它修改velocity自身的数据。再次,ball1没有被感动:
velocity
>>> ball1.velocity
球很齐整,但是大多数人并没有模仿它。您正在制作游戏。让我们考虑一下我们拥有的东西:
因此,让我们腾出一个房间。房间有名称,因此我们将有一些数据来存储:
class Room(object): # Note that we're taking an argument besides self, here. def __init__(self, name): self.name = name # Set the room's name to the name we got.
让我们做一个实例:
>>> white_room = Room("White Room") >>> white_room.name 'White Room'
臭 但是,如果您希望不同的房间具有不同的功能,那么这并不是那么有用,所以让我们创建一个 子类 。一个 子类 继承其所有功能 超 ,但你可以添加更多的功能或者覆盖超类的功能。
让我们考虑一下我们要如何处理房间:
我们想与房间互动。
我们该怎么做?
用户键入一行得到响应的文本。
它的响应方式取决于房间,因此让我们使用称为的方法来处理房间interact:
interact
class WhiteRoom(Room): # A white room is a kind of room. def __init__(self): # All white rooms have names of 'White Room'. self.name = 'White Room' def interact(self, line): if 'test' in line: print "'Test' to you, too!"
现在,让我们尝试与之交互:
>>> white_room = WhiteRoom() # WhiteRoom's __init__ doesn't take an argument (even though its superclass's __init__ does; we overrode the superclass's __init__) >>> white_room.interact('test') 'Test' to you, too!
您最初的示例是在房间之间移动。让我们使用一个名为的全局变量current_room来跟踪我们所在的房间。1让我们也创建一个红色房间。
current_room
1.除了全局变量外,这里还有更好的选择,但是为了简单起见,我将使用一个。
class RedRoom(Room): # A red room is also a kind of room. def __init__(self): self.name = 'Red Room' def interact(self, line): global current_room, white_room if 'white' in line: # We could create a new WhiteRoom, but then it # would lose its data (if it had any) after moving # out of it and into it again. current_room = white_room
现在让我们尝试一下:
>>> red_room = RedRoom() >>> current_room = red_room >>> current_room.name 'Red Room' >>> current_room.interact('go to white room') >>> current_room.name 'White Room'
读者练习: 添加代码WhiteRoom的interact,可以让你回到红厅。
WhiteRoom
现在我们可以正常工作了,让我们将它们放在一起。利用name所有房间的新数据,我们还可以在提示中显示当前房间!
name
def play_game(): global current_room while True: line = raw_input(current_room.name + '> ') current_room.interact(line)
您可能还想创建一个功能来重置游戏:
def reset_game(): global current_room, white_room, red_room white_room = WhiteRoom() red_room = RedRoom() current_room = white_room
将所有的类定义和这些函数放到一个文件中,您可以在这样的提示下播放它(假设它们在中mygame.py):
mygame.py
>>> import mygame >>> mygame.reset_game() >>> mygame.play_game() White Room> test 'Test' to you, too! White Room> go to red room Red Room> go to white room White Room>
为了仅通过运行Python脚本就可以玩游戏,您可以在底部添加以下代码:
def main(): reset_game() play_game() if __name__ == '__main__': # If we're running as a script... main()
这是对类以及如何将其应用于您的情况的基本介绍。