实例:新创建一个对象 2018-04-22

Tutorial: Building a Giant Mech

增加命令

# in a new file mygame/commands/mechcommands.py

from evennia import Command

class CmdShoot(Command):
    """
    Firing the mech’s gun
    
    Usage:
      shoot [target]

    This will fire your mech’s main gun. If no
    target is given, you will shoot in the air.
    """
    key = "shoot"
    aliases = ["fire", "fire!"]

    def func(self):
        "This actually does the shooting"

        caller = self.caller
        location = caller.location

        if not self.args:
            # no argument given to command - shoot in the air
            message = “BOOM! The mech fires its gun in the air!”
            location.msg_contents(message)
            return

        # we have an argument, search for target
        target = caller.search(self.args)
        if target:
            message = "BOOM! The mech fires its gun at %s" % target.key
            location.msg_contents(message)

增加命令集

# in the same file mygame/commands/mechcommands.py

from evennia import CmdSet
from evennia import default_cmds

class MechCmdSet(CmdSet):
    """
    This allows mechs to do do mech stuff.
    """
    key = "mechcmdset"

    def at_cmdset_creation(self):
        "Called once, when cmdset is first created"        
        self.add(CmdShoot())
        self.add(CmdLaunch())

临时赋予对象命令集

@py self.search("mech").cmdset.add("commands.mechcommands.MechCmdSet")

创建类

# in the new file mygame/typeclasses/mech.py

from objects import Object
from commands.mechcommands import MechCmdSet
from evennia import default_cmds

class Mech(Object):
    """
    This typeclass describes an armed Mech.
    """
    def at_object_creation(self):
        "This is called only when object is first created"
        self.cmdset.add_default(default_cmds.CharacterCmdSet)
        self.cmdset.add(MechCmdSet, permanent=True)
        self.locks.add("puppet:all();call:false()")
        self.db.desc = "This is a huge mech. It has missiles and stuff."

你可能感兴趣的:(实例:新创建一个对象 2018-04-22)