函数与类有很大的不同。好像您已使用一个函数,并将其更改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
堂课。我们如何使用它?
>>> 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.veLocity
(0, 0)
>>> ball2.bounce()
>>> ball2.veLocity
(5, -10)
该bounce
方法导致它修改veLocity
自身的数据。再次,ball1
没有被感动:
>>> 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
:
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让我们也创建一个红色房间。
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
,可以让你回到红厅。
现在我们可以正常工作了,让我们将它们放在一起。利用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
):
>>> 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()
这是对类以及如何将其应用于您的情况的基本介绍。