<?php //设置当前页面编码格式 header("Content-type:text/html; charset=gbk"); class hero{ public $name; //名字 public $blood; //血量 public $attack; //攻击力 //放技能的方法 public function jineng(){ $arr = array( array(50,"隔山打牛"), array(60,"降龙十八掌"), array(45,"九阳神功"), array(20,"葵花宝典"), array(70,"六脉神剑") ); $gs = count($arr); $sy = rand(0,$gs-1); echo "{$this->name}使用了{$arr[$sy][1]} "; return $arr[$sy][0]; } //构造函数 public function __construct($name,$blood,$gjl){ $this->name = $name; $this->blood = $blood; $this->attack = $gjl; } } //造第一个人 $h1 = new hero("狗蛋",800,15); //造第二个人 $h2 = new hero("大壮",700,16); while(true){ //随机一下,是普攻还是放技能 $sj1 = rand(0,1); $gj = 0; //本次h1能打掉多少血 echo "{$h1->name}打{$h2->name} "; if($sj1){ $gj = $h1->jineng(); }else{ $gj = $h1->attack+rand(0,20); } $h2->blood = $h2->blood-$gj; echo "{$h1->name}打掉了{$h2->name}的{$gj}血 "; echo "****************************** "; $sj2 = rand(0,1); $gj2 = 0; //本次h2能打掉多少血 echo "{$h2->name}打{$h1->name} "; if($sj2){ $gj2 = $h2->jineng(); }else{ $gj2 = $h2->attack+rand(0,20); } $h1->blood = $h1->blood-$gj2; echo "{$h2->name}打掉了{$h1->name}的{$gj2}血 "; echo "****************************** "; if($h2->blood<=0){ echo "{$h1->name}赢了"; break; }else if($h1->blood<=0){ echo "{$h2->name}赢了"; break; } echo "---- "; //休眠 sleep(3); }
如果页面内只有php代码,只需要写揩油<?php即可,不需要写结尾;
sleep()代表休眠时间。