<?php header("Content-type:text/html; charset=gbk"); class Renwu{ public $name; //姓名 public $xueliang; //血量 public $gongjili; //攻击力 //放技能的方式 public function jineng(){ $a = array( array(50,"打狗棍法"), array(60,"天降正义"), array(50,"一阳指"), array(80,"致命一击") ); $jn = count($a); $pg = rand(0,$jn-1); echo "{$this->name}使用了{$a[$pg][1]} "; return $a[$pg][0]; } //构造函数 public function __construct($name,$xueliang,$gongjili1){ $this->name = $name; $this->xueliang = $xueliang; $this->gongjili = $gongjili1 ; } } //造第一个人 $h1 = new Renwu("李逍遥",1000,50); //造第二个人 $h2 = new Renwu("独孤求败",1200,52); while(true){ //随机一下攻击,是普攻还是技能 $sj1 = rand(0,1); $gj1 = 0; //本次h1能打掉多少血 echo "{$h1->name}打{$h2->name} "; if($sj1){ $gj1 = $h1->jineng(); }else{ $gj1 = $h1->gongjili+rand(0,30); } $h2->xueliang = $h2->xueliang-$gj1; echo "{$h1->name}打掉了{$h2->name}的{$gj1}血 "; echo "************************** "; $sj2 = rand(0,1); $gj2 = 0; //本次h2能打掉多少血 echo "{$h2->name}打{$h1->name} "; if($sj2){ $gj2 = $h2->jineng(); }else{ $gj2 = $h2->gongjili+rand(0,30); } $h1->xueliang = $h1->xueliang-$gj; echo "{$h2->name}打掉了{$h1->name}的{$gj2}血 "; echo "************************** "; if($h2->xueliang<=0){ echo "{$h1->name}赢了"; break; }else if($h1->xueliang<=0){ echo "{$h2->name}赢了"; break; } //休眠 sleep(3); }