面向对象练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<?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();
?>
</body>
</html>
<?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;
		}
	}
	
	
	
}
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号