每次增加10%

时间:2014-11-19 18:59:25

标签: php

我有一个游戏,在那个游戏中你有一个" House"你升级了。对我和你来说,那个房子只不过是数据库中的一个数字。现在,要升级房屋,你需要资源(数据库中的数字更多),我希望每次房屋增加时成本都会增加。

所以例如

  • 我的房子是1级,升级到2级需要100美元
  • 我花了100来升级我的房子,但价格上涨了10%
  • 所以房子是2级,升级到3级需要110.

等等。这看起来非常简单,我不知道我是如何挣扎于此的,这是我迄今为止的表现。

$goldcostperhouse = 100;
$goldcostperhouse2 = 100 + $goldcostperhouse / 10;
$woodcostperhouse = 100;
$woodcostperhouse2 = 100 + $woodcostperhouse / 10;
$upgradecostgold = $house * $goldcostperhouse;
$upgradecostwood = $house * $woodcostperhouse2;

费用每次都保持在100 :(

这里是完整的代码

<?php
include ("header.php");
include ("connect.php");

$stats_get = mysqli_query($con,"SELECT stats.gold, stats.wood, stats.id FROM stats WHERE stats.id =     $id4");
$stats_got = mysqli_fetch_array($stats_get);
$gold10 = $stats_got['gold'];
$wood10 = $stats_got['wood'];

$buildings_get = mysqli_query($con,"SELECT buildings.house, buildings.id FROM buildings WHERE     buildings.id = $id4");
$gotbuildings = mysqli_fetch_array($buildings_get);
$house = $gotbuildings['house'];
$nextlevel = $house + 1;
$goldcostperhouse = 200;
$goldcostperhouse2 = 200 + $goldcostperhouse / 10;
$woodcostperhouse = 200;
$woodcostperhouse2 = 200 + $woodcostperhouse / 10;
$upgradecostgold = $house * $goldcostperhouse;
$upgradecostwood = $house * $woodcostperhouse2;



IF ($house <= 0){
echo "You need to buy a house, it costs 76,000 Wood and 24,000 Gold!<br>";
    IF(isset($_POST['buy'])){
    echo "You have bought the house!";
    IF($gold10 < 24000){
        echo "You don't have enough Gold!";
    }elseif($wood10 < 76000){
        echo "You don't have enough Wood!";
    }else{
    $removegold = mysqli_query($con,"UPDATE stats SET stats.gold = stats.gold - 24000 WHERE stats.id = $id4");
    $removewood = mysqli_query($con,"UPDATE stats SET stats.wood = stats.wood - 76000 WHERE stats.id = $id4");
    $givehouse = mysqli_query($con,"UPDATE buildings SET buildings.house = 1 WHERE buildings.id = $id4");
    }
}

?>
<html>
<body>

<form action="house.php" method="post">
<input type="Submit" name="buy" value="Buy"/>
</form>

<?php

}else{

echo "House level: ",number_format($house),"<br>";
echo "It will cost ",number_format($upgradecostgold)," Gold and ",number_format($upgradecostwood), " Wood to upgrade your house to level ",number_format($nextlevel),"<br>";

IF(isset($_POST['upgrade'])){
IF($gold10 < $upgradecostgold){
    echo "You do not have enough Gold!";
    }elseif($wood10 < $upgradecostwood){
    echo "You do not have enough Wood!";
    }else{
    echo "You have upgraded your house!<br>";
    $removegold = mysqli_query($con,"UPDATE stats SET stats.gold = stats.gold - $upgradecostgold WHERE stats.id = $id4");
    $removewood = mysqli_query($con,"UPDATE stats SET stats.wood = stats.wood - $upgradecostwood WHERE stats.id = $id4");
    $givehouse = mysqli_query($con,"UPDATE buildings SET buildings.house = buildings.house + 1     WHERE buildings.id = $id4");
    }

}

?>

<html>
<body>

<form action="house.php" method="post">
<input type="Submit" name="upgrade" value="Upgrade"/>
</form>

<?php

}

include("footer.php")
?>

1 个答案:

答案 0 :(得分:3)

看看你如何计算房屋成本:

Level 1 = 100
Level 2 = 100 * 1.1
Level 3 = 100 * 1.1 * 1.1
Level 4 = 100 * 1.1 * 1.1 * 1.1
etc.

这可以用公式表示(其中^是“权力”):

cost = 100 * (1.1 ^ (level - 1))

然后,您可以使用pow来计算数学能力,将PHP中的升级成本表示为房屋级别的函数:

function upgradeCost($houseLevel) {
    return intval(100 * pow(1.1, $houseLevel - 1));
}

或者您可以在代码中内联执行:

$upgradeCost = intval(100 * pow(1.1, $houseLevel - 1));

这将为您提供序列100,110,121,133,146,161,177,194,214,235 ...

请注意,我已使用intval确保费用为整数。如果您可以使用非整数费用,则可以删除intval