機能はクラスとは大きく異なります。あなたが機能を取り、ちょうど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
は独立したまま:
我々が定義されている
bounce
メソッド(クラス内の関数)について今、何
>>> 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
は、ファイルにクラス定義と、これらの機能のすべてを入れて、あなたは(このようプロンプトでそれを再生することができますPythonスクリプトを実行して、ただのゲームをプレイすることができるようにするに
>>> 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>
、あなたが一番下にこれを追加することができます:)彼らはmygame.py
にいると仮定し
def main():
reset_game()
play_game()
if __name__ == '__main__': # If we're running as a script...
main()
これは、クラスの基本的な紹介と、それをあなたの状況に適用する方法です。
「ボール」はクラスであってはなりません。経験則:2つのメソッドしかなく、そのうちの1つが '__init__'の場合は、関数と' functools.partial'を使って関数をいくつかのデータにバインドする必要があります。ここに紹介されている他のクラスと同じです。 PythonはJavaではありません!ここで私が見ているのは、クラスでなければならないのはゲームそのものですが、代わりにモジュール全体の状態を使用するのですか? –
ユーザの運動部では、白い部屋から赤い部屋に行くことができるようにしました。でも、私は出来ません。 "if" x "in line:current_room = red_room"を追加しました。現在のルーム変数は変更されません。しかし、もし私が次のようなことをしたら: "if" x "in line:print" this ""それは動作します。あなたの投稿をありがとう。それは非常に有用だった! –
私はそれを考え出した!あなたの助けをもう一度ありがとう:) –