我正在编写一款游戏,允许用户单击按钮来击打随机蜂。
蜜蜂有三种类型:
女王蜂:生命值达到100。 •当女王蜂被击中时,从她的生命中扣除8点生命值。 •如果/当女王蜂的生命值耗尽时,所有剩余的活蜂会自动耗尽生命 点。 •只有1只皇后蜂
工蜂:工蜂的生命周期为75。 •当工蜂被击中时,会从其寿命中扣除10点生命值。 •有5个工蜂。
无人蜂:无人蜂的寿命为50点生命值。 •当击中无人蜂时,他的寿命中扣除12点生命值。 •有8架无人蜂。
我正在尝试编写一个函数,该函数每次拍摄蜜蜂时都会从蜜蜂的寿命中减去8分。但是,蜜蜂的寿命会相应缩短,因此,在每次后续拍摄中,“ hit()”方法将采用一个“寿命”参数,该参数比上次调用时间低8点。此刻,我的程序每次都用蜜蜂的原始点数调用“ hit”方法(100次)。有人知道如何实现此功能吗?
代码:
HTML代码:
<!DOCTYPE html>
<html>
<head>
<title>Bees Game</title>
</head>
<style>
.button {
padding: 10px 20px;
border: 1px solid #999999;
background-color: #228822;
border-radius: 20px;
}
</style>
<body>
<?php
include('queenbee.php');
if (isset($_GET['hello'])) {
echo 'Hello is set';
runMyFunction();
}
?>
<h1>Bee Game</h1>
<div class="button"><a href='index.php?hello=true'>Hit me</a></div>
</body>
</html>
PHP代码:
<?php
class Bee {
private $lifespan;
private $points;
function __construct($lifespan, $points)
{
$this->lifespan = $lifespan;
$this->points = $points;
}
public function hit($lifespan, $points) { // Function used to hit a bee
$this->lifespan = $lifespan - $points;
echo $this->lifespan;
}
}
function runMyFunction() {
$randomNumber = rand(0, 2);
$bees = ['queen', 'worker', 'drone'];
echo '<br>' . $bees[$randomNumber];
if($bees[$randomNumber] == 'queen'){
if($queencalled != 'yes')
{
$queen = new Bee(100, 8); // First time that queen bee is called
}
else
{
$queen = new Bee($queen->lifespan, 8);
}
$queencalled = 'yes';
$queen->hit(); # NULL
}
if($bees[$randomNumber] == 'worker'){
if($workercalled != 'yes')
{
$worker = new Bee(75, 10);
}
else
{
$worker = new Bee($worker->lifespan, 10);
}
$workercalled = 'yes';
$worker->hit(); # NULL
}
if($bees[$randomNumber] == 'drone'){
if($dronecalled != 'yes')
{
$drone = new Bee(50, 12);
}
else
{
$drone = new Bee($drone->lifespan, 12);
}
$dronecalled = 'yes';
$drone->hit(); # NULL
}
}
?>
答案 0 :(得分:0)
代码中有错误的概念,我将尽力解释所有这些问题,并提供一个可以解释其他概念的有效版本。
<?php
include('queenbee.php');
if (isset($_GET['hello'])) {
echo 'Hello is set';
runMyFunction();
}
?>
<h1>Bee Game</h1>
<div class="button"><a href='index.php?hello=true'>Hit me</a></div>
该按钮将为整个页面重新充电并清除所有内容。如果您希望此按钮是 interactive ,则必须使用javascript(如果要从服务器(如php)执行代码,则使用ajax)。然后也许在控制台上显示输出,或者有一些图形来完成游戏。
runMyFunction
不是一个好的函数名称,在我的示例中,我将其称为main
。 Main是函数的常用名称,用于指出完整程序执行的开始位置。
public function hit($lifespan, $points) { // Function used to hit a bee
$this->lifespan = $lifespan - $points;
echo $this->lifespan;
}
最好在被击中的蜜蜂中使用此方法,因为该蜜蜂是更改信息的所有者。我将其称为getHit
,因为它被另一只蜜蜂击中了。该蜜蜂对象将其命中点作为参数。
public function getHit($points) { // this bee get hit
$this->lifespan -= $points;
}
以下代码的类似版本重复3次,最好将其包装在以变量为参数的函数中。
if($bees[$randomNumber] == 'queen'){
if($queencalled != 'yes')
{
$queen = new Bee(100, 8); // First time that queen bee is called
}
else
{
$queen = new Bee($queen->lifespan, 8);
}
$queencalled = 'yes';
$queen->hit(); # NULL
}
我弄清楚了你想做什么,我创建了一个简单的游戏,其中有许多这种类型的蜜蜂,它们互相撞击直到只有一只活着。这非常有用,也很有趣。目前,它是一个控制台应用程序,而不是浏览器应用程序。
在控制台中运行它,并在对系统的主要部分进行编程时对一些测试进行编程。然后,当一切运行顺利时,便是时候使用浏览器并弄清整个系统架构了。
<?php
class Bee {
private $lifespan;
private $type;
private $points;
// added a parameter to know the bee type
function __construct($type,$lifespan, $points)
{
$this->type = $type;
$this->lifespan = $lifespan;
$this->points = $points;
}
public function getHit($points) { // this bee get hit
$this->lifespan -= $points;
}
// know the number of points
public function points(){
return $this->points;
}
// get a string version of the bee
public function __toString(){
if ($this->isAlive()){
return "a {$this->type} bee with ".
str_pad ( $this->lifespan,3," ", STR_PAD_LEFT).
" of life";
}
return "a {$this->type} dead bee";
}
//know whether it is alive
public function isAlive(){
return $this->lifespan > 0;
}
}
function main() {
// extracted the data from the code
$bees = [
//type => [number, lifespain, points]
'queen' => [1,100,8],
'worker' => [5,75,10],
'drone' => [8,50,12],
];
// this is the bee pool where all bees are created
$beePool = [];
// create all bees
foreach($bees as $type => $data) {
for($i=$data[0]; $i>0; $i--){
$beePool[] = new Bee($type,$data[1],$data[2]);
}
echo "created $data[0] $type bees".PHP_EOL;
}
echo PHP_EOL;
// let them fight
while (count($beePool)>1){
$beeNumber = count($beePool);
// get a bee from the beePool
$attacked = rand(0, $beeNumber-1);
$attacker = rand(0, $beeNumber-2);
// fix the number so it wont be the same bee
// and will get the full range of valid bees
if ($attacker >= $attacked){
$attacker ++;
}
echo "The bee #$attacker will atack the bee #$attacked".PHP_EOL;
$attBee = $beePool[$attacker];
$hitBee = $beePool[$attacked];
echo "ATTACKER: ".$attBee.PHP_EOL;
echo " HITS: ".$hitBee.PHP_EOL;
$hitBee->getHit($attBee->points());
// remove dead bee
if (!$hitBee->isAlive()){
echo "AND IT DIES".PHP_EOL. $beeNumber-1 ." bees alive".PHP_EOL;
array_splice($beePool, $attacked, 1);
}
echo PHP_EOL;
sleep(1);
}
echo "The remaining bee is ".$beePool[0];
}
main();
它输出:
created 1 queen bees
created 5 worker bees
created 8 drone bees
The bee #1 will atack the bee #5
ATTACKER: a worker bee with 75 of life
HITS: a worker bee with 75 of life
The bee #9 will atack the bee #10
ATTACKER: a drone bee with 50 of life
HITS: a drone bee with 50 of life
The bee #7 will atack the bee #5
ATTACKER: a drone bee with 50 of life
HITS: a worker bee with 65 of life
[...]
The bee #2 will atack the bee #1
ATTACKER: a drone bee with 30 of life
HITS: a worker bee with 11 of life
AND IT DIES
2 bees alive
The bee #0 will atack the bee #1
ATTACKER: a queen bee with 36 of life
HITS: a drone bee with 30 of life
The bee #0 will atack the bee #1
ATTACKER: a queen bee with 36 of life
HITS: a drone bee with 22 of life
The bee #0 will atack the bee #1
ATTACKER: a queen bee with 36 of life
HITS: a drone bee with 14 of life
The bee #1 will atack the bee #0
ATTACKER: a drone bee with 6 of life
HITS: a queen bee with 36 of life
The bee #0 will atack the bee #1
ATTACKER: a queen bee with 24 of life
HITS: a drone bee with 6 of life
AND IT DIES
1 bees alive
The remaining bee is a queen bee with 24 of life
答案 1 :(得分:-1)
在调用hit()函数时,您没有传递任何参数,但是您在类中使用参数定义了该函数。您不需要这些参数,因为它们已在构造函数中定义。
public function hit() { // Function used to hit a bee
$this->lifespan -= $this->points;
echo $this->lifespan;
}