php游戏开发过程中,经常需要用到的一些函数,需要的朋友可以参考下。
清单 1.简单的掷骰器
复制代码 代码如下:
function roll () {
return mt_rand(1,6);
}
echo roll();
复制代码 代码如下:
function roll ($sides) {
return mt_rand(1,$sides);
}
echo roll(6); // roll a six-sided die
echo roll(10); // roll a ten-sided die
echo roll(20); // roll a twenty-sided die
复制代码 代码如下:
$male = array(
"William",
"Henry",
"Filbert",
"John",
"Pat",
);
$last = array(
"Smith",
"Jones",
"Winkler",
"Cooper",
"Cline",
);
复制代码 代码如下:
shuffle($male);
shuffle($last);
for ($i = 0; $i echo $male[$i] . ' ' . $last[$i];
}
复制代码 代码如下:
$male = explode('\n', file_get_contents('names.female.txt'));
$last = explode('\n', file_get_contents('names.last.txt'));
复制代码 代码如下:
$settings = explode("\n", file_get_contents('scenario.settings.txt'));
$objectives = explode("\n", file_get_contents('scenario.objectives.txt'));
$antagonists = explode("\n", file_get_contents('scenario.antagonists.txt'));
$complicati**** = explode("\n", file_get_contents('scenario.complicati****.txt'));
shuffle($settings);
shuffle($objectives);
shuffle($antagonists);
shuffle($complicati****);
echo $settings[0] . ' ' . $objectives[0] . ' ' . $antagonists[0] . ' '
. $complicati****[0] . "
\n";
复制代码 代码如下:
$suits = array (
"Spades", "Hearts", "Clubs", "Diamonds"
);
$faces = array (
"Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King", "Ace"
);
复制代码 代码如下:
$deck = array();
foreach ($suits as $suit) {
foreach ($faces as $face) {
$deck[] = array ("face"=>$face, "suit"=>$suit);
}
}
复制代码 代码如下: