thetrap.py

来自「大型多人在线游戏开发 这本书的光盘内容。 包括一些脚本代码和图片」· Python 代码 · 共 51 行

PY
51
字号
# thetrap.py
#
# A SafeSandbox that represents a region that contains 
# a deadly trap, where one wrong step can kill you.
#
# ... if there were any code here!
#
# Instead, we demonstrate some of the things you can't
# do in a safesandbox.
#

class TheTrap:
  """A very dangerous place, where one wrong step can kill you."""

  def Init(self):
    print 'TheTrap.Init()'

  def OnPlayerEnter(self, key):
    print 'OnPlayerEnter( %s )' % (key,)    
    self.ClientMessage(key.subj, 'This is a test of the SafeSandbox system!')
    player = self.GetGameObject(key.subj)

    # try to access the deferred module
    try:
      import deferred
      self.ClientMessage(key.subj, 'You succeeded in importing the deferred module - this is bad.')
    except:
      self.ClientMessage(key.subj, 'You failed to import the deferred module - this is good.')

    # try to access the health attribute of a player
    try:
      print 'player.health = ', player.health
      self.ClientMessage(key.subj, 'You succeeded in accessing the health attribute - this is bad.')
    except:
      self.ClientMessage(key.subj, 'You failed to access the health attribute - this is good.')

    # try to access a protected method on a player
    try:
      player._GetWeapon()
      self.ClientMessage(key.subj, 'You succeeded in calling the _GetWeapon() method - this is bad.')
    except:
      self.ClientMessage(key.subj, 'You failed to call the _GetWeapon() method - this is good.')

    # try to open a file for writing 
    try:
      f = open('junk.txt', 'w')
      self.ClientMessage(key.subj, 'You succeeded opening a file - this is bad.')
    except:
      self.ClientMessage(key.subj, 'You failed to open a file - this is good.')

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?