Jump to content

Recommended Posts

Posted

Olá,  estou fazendo um web-game, e não estou conseguindo pensar em como fazer o codigo php

para o sistema de batalhar.php

esse codigo é basicamente, deverá pegar os status, força e defesa, do player e do monstro,

e fazer comparação e retirar a vida de cada um, e devera repetir esse codigo até um dos dois chegar a ZERO de vida,

mas estou com problemas para usar o for, e while.

quem puder me ajudar com um exemplo de codigo basico, eu agradeço desde já.

Att Ivo Gabriel

Posted

Exempo:


class Bicho {

    private $ataque;
    private $defesa;
    private $vida;

    public function __construct($ataque, $defesa, $vida) {
         $this->ataque = $ataque;
         $this->defesa = $defesa;
         $this->vida = $vida;
    }

    public function getVida() {
         return $this->vida;
    }

    public function getAtaque() {
         return $this->ataque;
    }

    public function getDefesa() {
         return $this->defesa;
    }



    public function defender(Bicho $bicho) {
         return $this->vida = $this->vida - (rand(1, $bicho->getAtaque()) - $this->getDefesa());
    }


    public function atacar(Bicho $bicho) {
         $bicho->defender($this);
    }
}

Para o combate:


$bicho1 = new Bicho(10, 5, 100);
$bicho2 = new Bicho(8, 6, 140);

do {
    echo "<br />Bicho 1: Vida:".$bicho1->getVida();
    echo "<br />Bicho 2: Vida:".$bicho2->getVida();
    $bicho1->atacar($bicho2);
    $bicho2->atacar($bicho1);
} while ($bicho1->getVida() > 0 && $bicho2->getVida() > 0);

Não testei...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.