私は、テキストベースのゲームを作成することで構成されるLearning Python the Hard Way の 52 の例です。したがって、私は 2 つの重要なモジュールmap.py
と、現在取り組んでいるモジュールをapp.py
1 つ持っています。show_room.html
map.py
ユーザーが4桁のコードを推測する必要がある部屋「レーザー兵器の武器庫」が含まれています。
コードを明らかにするチート コードを導入しようとしていapp.py
ます。
map.py :
code=("%s%s%s%s"%(randint(0,9),randint(0,9),randint(0,9),randint(0,9)))
class Room(object):
def __init__(self, name, description):
self.name = name
self.description = description
self.paths = {}
def some_other_functions:
....
laser_weapon_armory = Room("Laser Weapon Armory",description)
app.py (重要な部分を読んでください):
import web
from gothonweb import map
urls = ('/game', 'GameEngine',
'/', 'Index',)
some_code
class GameEngine(object):
def GET(self):
if session.room:
return render.show_room(room=session.room)
def POST(self):
form = web.input(action="")
#Here is the important part:
if session.room == map.laser_weapon_armory:
if form.action == "you tell me":
map.laser_weapon_armory.description+=map.code
elif form.action != map.code:
form.action ='*'
if session.room and session.room.go(form.action):
session.room = session.room.go(form.action)
web.seeother("/game")
重要な部分の if 句が検証されることはありません。主な理由は、「session.room」が「laser_weapon_armory」と等しくなると、別のオブジェクトとして表示されるためです。
すなわち:
print(laser_weapon_armory) ##<gothonweb.map.Room object at 0x01FDE550>
一方
print(session.room) ##gives <gothonweb.map.Room object at 0x0205E230>
他のインポートが同じオブジェクトを与えるのに、なぜこれが起こるのですか?