Should I use multiple classes for game?

前端 未结 5 1393
悲&欢浪女
悲&欢浪女 2020-12-14 23:24

I\'m considering making a text-based RPG-type program in PHP as both a holiday project and a chance to learn more about PHP and OOP. (Maybe not the best choice in language,

5条回答
  •  醉梦人生
    2020-12-14 23:53

    What you should do is get some real organization into the game.

    I've never built a PHP Game before but i have a pretty good idea how the structure should be.

    A Entity / Monster should be built up of several class defining its characteristics

    Here's an small example of the top of my head:

    abstract class NonHuman implements Strengh,Weapons,Vehicles
    {
         var $strength;
    }
    abstract class Vermin implements Strengh,Chemicals
    {
        var $strength = 20;
        var $poisonous = true;
    }
    abstract class Humanoid implements Strengh,Weapons,Vehicles,Arms,Legs
    {
    }
    

    Basic layout for the abstract classes are like so:

    abstract class  implements < Characteristics , Weapons , Etc>
    {
        // Depending on < Characteristics , Weapons , Etc> you should
        // Build the methods here so that theres less work in the long run.
    }
    

    Then once you have your base being types you can do things like

    class Rat extends Vermin
    {
        public function __construct($name,$strength = 50)
        {
             $this->strength = $strength;
        }
    
        //Any new methods here would be specific to this Being / Rat.
    }
    
    $Robert = new Rat('Robert',80);
    $Andrew = new Rat('Andrew',22);
    
    if($Robert->strength > 50)
    {
        $Robert->Kick($Andrew,'left',20); //20 mph lol
        if($Andrew->IsAlive())
        {
            if($Robert->TakeWeapon($Andrew,20)) //Uses 20% force
            {
                 $Robert->FireWeaponAt($Andrew,-1); //Use all bullets on andrew!
            }
        }
        if(!$Andrew->IsAlive())
        {
            $Robert->UpdateScoreFromPLayer($Andrew,100); //Max of 100 points if andrew has them.
        }
    }
    

    By doing this it would not be hard to generate characteristics for entities.

    You can also set up parent destructors to save the user-names data in a database for next time, and use the __construct to update the classes data. Hope this gives you a good idea :)


    There's more :)

    If you make classes for SpecialMoves lets say you can always do

    $Robert->AddSpecialMove('Roundhouse',new SpecialMove_Roundhouse(12));
    $Robert->UserSpecialMove('Roundhouse',2);/ x2
    if($Robert->_SpecialMoves->Roundhouse->Left() < 12)
    {
        $Robert->UserSpecialMove('Roundhouse',-1);/ Use all kicks.
    }
    

    Within SpecialMove_Roundhouse it would have parameters such as damage, lenght of time taken to complete, how much energy it uses, how many times you can use it.

    and the very first class that in the scope should always me a calculator for things such as heartrate, blood level, energy, inventory etc so you always have the essentials!


    Implements Example

    Implements make sure that the higher class contains certain functions and variables

    interface Weapons
    {
        public function Fire($target,$bullets);
    }
    
    class Colt45 implements Weapons
    {
       var $damage = 2;
       var $max_bullets = 80;
       var $clip = 80;
    
       //THIS CLASS MUST HAVE FIRE
       public function fire($target,$bullets)
       {
          $ammo = $bullets > $clip ? $clip : $ammo;
          for($shot=0;$shot<=$ammo;$shot++)
          {
              $target->ReduceHealth($damage);
              if(!$target->IsAlive())
              {
                  break;
              }
              $clip--; //Reduce ammo in clip.
          }
       }
    }
    

    Example here taken from php.net | http://www.php.net/manual/en/language.oop5.interfaces.php#96368

    " . __FUNCTION__);
        }
    }
    
    class T806 extends T805 implements Auxiliary_Platform
    {
        public function Weapon()
        {
            var_dump(__CLASS__);
        }
        public function Shields()
        {
            var_dump(__CLASS__ . "->" . __FUNCTION__);
        }
    }
    
    $T805 = new T805();
    $T805->Weapon();
    $T805->Health();
    $T805->Shields();
    
    echo "
    "; $T806 = new T806(); $T806->Weapon(); $T806->Health(); $T806->Shields(); /* Output: string(4) "T805" string(12) "T805::Health" string(13) "T805->Shields"
    string(4) "T806" string(12) "T805::Health" string(13) "T806->Shields" */ ?>

提交回复
热议问题