创建游戏:
hero.class.php
<?php
class Hero
{
public $blood;
public $gongji;
public $jingyan;
public $level;
public $name;
public $money;
public $jineng = array();
//构造函数,对成员进行初始化
function __construct($n)
{
$this->blood = 100;
$this->gongji = 10;
$this->jingyan = 0;
$this->level = 1;
$this=>money =100;
$this->name = $n;
}
//打怪函数
function DaGuai()
{
//随机
$sj = floor(rand(0,100));
if($sj>30)
{
$jy = floor(rand(0,40));//获取经验
$this->jingyan = $this->jingyan+$jy;//将该英雄的经验增加
if($this->jingyan>=50)//判断是否要升级
{
$this->level +=1;
$this->jingyan = 0;
$this->blood += 20;
$this->gongji +=5;
}
echo $this->name."杀死一个怪物,获得了{$jy}点经验";
}
else
{
if($this->level==1)
{
}
else
{
$this->level -=1;
}
echo "你被怪物打死了";
}
}
function Show()//查看英雄信息
{
echo "英雄名称:{$this->name}<br>";
echo "英雄血量:{$this->blood}<br>";
echo "英雄攻击:{$this->gongji}<br>";
echo "英雄经验:{$this->jingyan}<br>";
echo "英雄等级:{$this->level}<br>";
echo "技能为:";
foreach($this->jineng as $v)
{
echo $v.",";
}
}
function XueXi()//学习技能
{
$hf = floor(rand(0,20));//花钱
$n = floor(rand(0,5));
switch($n)
{
case 0:
array_push($this->jineng,"冲锋");
break;
case 1:
array_push($this->jineng,"嘲讽");
break;
case 2:
array_push($this->jineng,"致死打击");
break;
case 3:
array_push($this->jineng,"盾墙");
break;
case 4:
array_push($this->jineng,"沉默");
break;
}
}
}
play.php
<?php
include("Hero.class.php");
$hero = new Hero("张三");//创建英雄
$hero->DaGuai();
$hero->Show();
$hero->DaGuai();
$hero->Show();
$hero->DaGuai();
$hero->Show();
$hero->DaGuai();
$hero->Show();
$hero->DaGuai();
$hero->Show();
$hero->XueXi();
$hero->Show();
$hero->XueXi();
$hero->Show();
?>