Game - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Game.php

  1. <?php
  2. class Hero
  3. {
  4.         public $HP;
  5.         public $damage;
  6.         public $name;
  7.        
  8.         public function __construct($name, $HP, $damage, $crit = 5, $critMult = 2, $evasion = 5, $shieldChance = 5, $shieldBlock = 10)
  9.         {
  10.                 $this->name = $name;
  11.                 $this->HP = $HP;
  12.                 $this->damage = $damage;
  13.                 $this->crit = $crit;
  14.                 $this->critMult = $critMult;
  15.                 $this->evasion = $evasion;
  16.         }
  17.        
  18.         public function getStats()
  19.         {
  20.                 echo "Имя: {$this->name}";
  21.                 echo "\n";
  22.                 echo "HP: {$this->HP}";
  23.                 echo "\n";
  24.                 echo "Урон: {$this->damage}";
  25.         }
  26.        
  27.         public function getDamage()
  28.         {
  29.                 $isEvasion = mt_rand(1, 100) <= $this->evasion;
  30.                 if ($isEvasion) {
  31.                         return 0;
  32.                 }
  33.                
  34.                 $isCrit = mt_rand(1, 100) <= $this->crit;
  35.                 if ($isCrit) {
  36.                         return $this->damage * $this->critMult;
  37.                 }
  38.                 return $this->damage;
  39.         }
  40.        
  41.         public function hit()
  42.         {
  43.                 $damage = $this->getDamage();
  44.                 echo $this->name . ' наночит по врагу ' .$damage . ' урона';
  45.                 return $damage;
  46.         }
  47.        
  48.         public function takeHit($hit)
  49.         {
  50.                 $hp = ($this->HP - $hit);
  51.                 $this->HP = ($hp < 0) ? 0 : $hp;
  52.         }
  53.        
  54.         public function checkHP()
  55.         {
  56.                 echo "\n";
  57.                 echo $this->name . " HP: {$this->HP}";
  58.                 echo "\n";
  59.         }
  60. }
  61.  
  62. class Fight
  63. {
  64.         public $hero1;
  65.         public $hero2;
  66.        
  67.         public function __construct(Hero $hero1, Hero $hero2)
  68.         {
  69.                 $this->hero1 = $hero1;
  70.                 $this->hero2 = $hero2;
  71.         }
  72.        
  73.         public function hitHero1()
  74.         {
  75.                 $this->hero2->takeHit($this->hero1->hit());
  76.                 $this->hero2->checkHP();
  77.         }
  78.        
  79.         public function hitHero2()
  80.         {
  81.                 $this->hero1->takeHit($this->hero2->hit());
  82.                 $this->hero1->checkHP();
  83.         }
  84.        
  85.         public function heroesHits()
  86.         {
  87.                 $rand = mt_rand(0,1);
  88.                
  89.                 if ($rand) {
  90.                         $this->hitHero1();
  91.                         $this->hitHero2();
  92.                 } else {
  93.                         $this->hitHero2();
  94.                         $this->hitHero1();
  95.                 }
  96.                
  97.                 echo "\n";
  98.                 echo "----------------------";
  99.                 echo "\n";
  100.         }
  101.        
  102.         public function fight()
  103.         {
  104.                 while($this->hero2->HP >= 0 || $this->hero1->HP >= 0) {
  105.                         $this->heroesHits();
  106.                        
  107.                         if($this->hero1->HP <= 0) {
  108.                                 echo $this->hero2->name . ' win';
  109.                                 break;
  110.                                 return;
  111.                         }
  112.                        
  113.                         if($this->hero2->HP <= 0) {
  114.                                 echo $this->hero1->name . ' win';
  115.                                 break;
  116.                                 return;
  117.                         }
  118.                
  119.                 }
  120.                
  121.         }
  122.        
  123.         public function getStats()
  124.         {
  125.                 echo 'Fight: ' . $this->hero1->name . ' VS ' . $this->hero2->name;
  126.                 echo "\n\n";
  127.                 $this->hero1->getStats();
  128.                 echo "\n\n";
  129.                 $this->hero2->getStats();
  130.                 echo "\n\n";
  131.                 echo "Lets Fight";
  132.                 echo "\n\n";
  133.         }
  134. }
  135.  
  136. $hero1 = new Hero('Sashko', 100, 15, 30, 1.5, 50);
  137. $hero2 = new Hero('Ruslan', 100, 15, 17, 2.25, 5);
  138.  
  139. $fight = new Fight($hero1, $hero2);
  140. $fight->getStats();
  141. $fight->fight();
  142.  
File Description
  • Game
  • PHP Code
  • 18 Apr-2022
  • 2.59 Kb
You can Share it: