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.
Result of php executing
Full code of Game.php
- <?php
- class Hero
- {
- public $HP;
- public $damage;
- public $name;
- public function __construct($name, $HP, $damage, $crit = 5, $critMult = 2, $evasion = 5, $shieldChance = 5, $shieldBlock = 10)
- {
- $this->name = $name;
- $this->HP = $HP;
- $this->damage = $damage;
- $this->crit = $crit;
- $this->critMult = $critMult;
- $this->evasion = $evasion;
- }
- public function getStats()
- {
- echo "Имя: {$this->name}";
- echo "\n";
- echo "HP: {$this->HP}";
- echo "\n";
- echo "Урон: {$this->damage}";
- }
- public function getDamage()
- {
- $isEvasion = mt_rand(1, 100) <= $this->evasion;
- if ($isEvasion) {
- return 0;
- }
- $isCrit = mt_rand(1, 100) <= $this->crit;
- if ($isCrit) {
- return $this->damage * $this->critMult;
- }
- return $this->damage;
- }
- public function hit()
- {
- $damage = $this->getDamage();
- echo $this->name . ' наночит по врагу ' .$damage . ' урона';
- return $damage;
- }
- public function takeHit($hit)
- {
- $hp = ($this->HP - $hit);
- $this->HP = ($hp < 0) ? 0 : $hp;
- }
- public function checkHP()
- {
- echo "\n";
- echo $this->name . " HP: {$this->HP}";
- echo "\n";
- }
- }
- class Fight
- {
- public $hero1;
- public $hero2;
- public function __construct(Hero $hero1, Hero $hero2)
- {
- $this->hero1 = $hero1;
- $this->hero2 = $hero2;
- }
- public function hitHero1()
- {
- $this->hero2->takeHit($this->hero1->hit());
- $this->hero2->checkHP();
- }
- public function hitHero2()
- {
- $this->hero1->takeHit($this->hero2->hit());
- $this->hero1->checkHP();
- }
- public function heroesHits()
- {
- $rand = mt_rand(0,1);
- if ($rand) {
- $this->hitHero1();
- $this->hitHero2();
- } else {
- $this->hitHero2();
- $this->hitHero1();
- }
- echo "\n";
- echo "----------------------";
- echo "\n";
- }
- public function fight()
- {
- while($this->hero2->HP >= 0 || $this->hero1->HP >= 0) {
- $this->heroesHits();
- if($this->hero1->HP <= 0) {
- echo $this->hero2->name . ' win';
- break;
- return;
- }
- if($this->hero2->HP <= 0) {
- echo $this->hero1->name . ' win';
- break;
- return;
- }
- }
- }
- public function getStats()
- {
- echo 'Fight: ' . $this->hero1->name . ' VS ' . $this->hero2->name;
- echo "\n\n";
- $this->hero1->getStats();
- echo "\n\n";
- $this->hero2->getStats();
- echo "\n\n";
- echo "Lets Fight";
- echo "\n\n";
- }
- }
- $hero1 = new Hero('Sashko', 100, 15, 30, 1.5, 50);
- $hero2 = new Hero('Ruslan', 100, 15, 17, 2.25, 5);
- $fight = new Fight($hero1, $hero2);
- $fight->getStats();
- $fight->fight();
File Description
- Game
- PHP Code
- 18 Apr-2022
- 2.59 Kb
You can Share it:
Latest PHP Pastes