Heim > Artikel > Backend-Entwicklung > 25个不错PHP的游戏编程脚本代码分享(1)_PHP教程
简单的掷骰器
许多游戏和游戏系统都需要骰子。让我们先从简单的部分入手:掷一个六面骰子。实际上,滚动一个六面骰子就是从 1 到 6 之间选择一个随机数字。在 PHP 中,这十分简单:echo rand(1,6);。
在许多情况下,这基本上很简单。但是在处理机率游戏时,我们需要一些更好的实现。PHP 提供了更好的随机数字生成器:mt_rand()。在不深入研究两者差别的情况下,可以认为 mt_rand 是一个更快、更好的随机数字生成器:echo mt_rand(1,6);。如果把该随机数字生成器放入函数中,则效果会更好。
清单 1. 使用 mt_rand() 随机数字生成器函数
<ol class="dp-c"> <li class="alt"><span><span class="keyword">function</span><span> roll () { </span></span></li> <li> <span class="keyword">return</span><span> mt_rand(1,6); </span> </li> <li class="alt"><span>} </span></li> <li> <span class="func">echo</span><span> roll(); </span> </li> </ol>
然后可以把需要滚动的骰子类型作为参数传递给函数。
清单 2. 将骰子类型作为参数传递
<ol class="dp-c"> <li class="alt"><span><span class="keyword">function</span><span> roll (</span><span class="vars">$sides</span><span>) { </span></span></li> <li> <span class="keyword">return</span><span> mt_rand(1,</span><span class="vars">$sides</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li> <span class="func">echo</span><span> roll(6); </span><span class="comment">// roll a six-sided die </span><span> </span> </li> <li class="alt"> <span class="func">echo</span><span> roll(10); </span><span class="comment">// roll a ten-sided die </span><span> </span> </li> <li> <span class="func">echo</span><span> roll(20); </span><span class="comment">// roll a twenty-sided die</span><span> </span> </li> </ol>
从这里开始,我们可以继续根据需要一次滚动多个骰子,返回结果数组;也可以一次性滚动多个不同类型的骰子。但是大多数任务都可以使用这个简单的脚本。
随机名称生成器
如果正在运行游戏、编写故事或者一次性创建大批字符,有时会疲于应付不断出现的新名字。让我们看一看可用于解决此问题的一个简单随机名称生成器。首先,让我们创建两个简单数组 — 一个用于名字,一个用于姓氏。
清单 3. 名字和姓氏的两个简单数组
<ol class="dp-c"> <li class="alt"><span><span class="vars">$male</span><span> = </span><span class="keyword">array</span><span>( </span></span></li> <li> <span class="string">"William"</span><span>, </span> </li> <li class="alt"> <span class="string">"Henry"</span><span>, </span> </li> <li> <span class="string">"Filbert"</span><span>, </span> </li> <li class="alt"> <span class="string">"John"</span><span>, </span> </li> <li> <span class="string">"Pat"</span><span>, </span> </li> <li class="alt"><span>); </span></li> <li> <span class="vars">$last</span><span> = </span><span class="keyword">array</span><span>( </span> </li> <li class="alt"> <span class="string">"Smith"</span><span>, </span> </li> <li> <span class="string">"Jones"</span><span>, </span> </li> <li class="alt"> <span class="string">"Winkler"</span><span>, </span> </li> <li> <span class="string">"Cooper"</span><span>, </span> </li> <li class="alt"> <span class="string">"Cline"</span><span>, </span> </li> <li><span>); </span></li> </ol>
然后就可以从每个数组中选择一个随机元素:echo $male[array_rand($male)] . ' ' . $last[array_rand($last)];。要一次性提取多个名称,只需混合数组并根据需要提取。
清单 4. 混合名称数组
<ol class="dp-c"> <li class="alt"><span><span>shuffle(</span><span class="vars">$male</span><span>); </span></span></li> <li> <span>shuffle(</span><span class="vars">$last</span><span>); </span> </li> <li class="alt"> <span class="keyword">for</span><span> (</span><span class="vars">$i</span><span> = 0; </span><span class="vars">$i</span><span> <span class="vars">$i</span><span>++) { </span></span> </li> <li> <span class="func">echo</span><span> </span><span class="vars">$male</span><span>[</span><span class="vars">$i</span><span>] . </span><span class="string">' '</span><span> . </span><span class="vars">$last</span><span>[</span><span class="vars">$i</span><span>]; </span> </li> <li class="alt"><span>} </span></li> </ol>
基于此基本概念,我们可以创建保存名字和姓氏的文本文件。如果在文本文件的每一行中存放一个名字,则可以轻松地用换行符分隔文件内容以构建源代码数组。
清单 5. 创建名称的文本文件
<ol class="dp-c"> <li class="alt"><span><span class="vars">$male</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">'n'</span><span>, </span><span class="func">file_get_contents</span><span>(</span><span class="string">'names.female.txt'</span><span>)); </span></span></li> <li> <span class="vars">$last</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">'n'</span><span>, </span><span class="func">file_get_contents</span><span>(</span><span class="string">'names.last.txt'</span><span>)); </span> </li> </ol>
构建或查找一些好的名字文件(代码归档 中附带了一些文件),此后我们绝不再需要为名字烦恼。
场景生成器
利用构建名字生成器使用的相同基本原理,我们可以构建场景生成器。此生成器不但在角色扮演游戏中十分有用,而且在需要用到伪随机环境集合(可用于角色扮演、即兴创作、写作等情况)的情况下也十分有用。我最喜欢的游戏之一,Paranoia 在其 GM Pack 中包括了 “任务混合器(mission blender)”。任务混合器可用于在快速滚动骰子时整合完整任务。让我们整合自己的场景生成器。
考虑以下场景:您醒来后发现自己迷失于丛林中。您知道自己必须赶去纽约,但是不知道原因。您可以听到附近的狗叫声及清晰的敌方搜寻者的声音。您浑身发冷、不住颤抖,而且没有武器。该场景中的每一句话都介绍场景的特定方面:
“您醒来后发现自己迷失于丛林中” — 这句话将建立设置。
“您知道自己必须赶去纽约” — 这句话将描述目标。
“您可以听到狗叫声” — 这句话将介绍敌人。
“您浑身发冷、不住颤抖,而且没有武器” — 这句话将添加复杂度。
就像创建名字和姓氏的文本文件一样,首先分别创建设置、目标、敌人和复杂度的文本文件。代码归档中附带了样例文件。在拥有这些文件后,生成场景的代码与生成名称的代码基本相同。
清单 6. 生成场景
<ol class="dp-c"> <li class="alt"><span><span class="vars">$settings</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">"n"</span><span>, </span><span class="func">file_get_contents</span><span>(</span><span class="string">'scenario.settings.txt'</span><span>)); </span></span></li> <li> <span class="vars">$objectives</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">"n"</span><span>, </span><span class="func">file_get_contents</span><span>(</span><span class="string">'scenario.objectives.txt'</span><span>)); </span> </li> <li class="alt"> <span class="vars">$antagonists</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">"n"</span><span>, </span><span class="func">file_get_contents</span><span>(</span><span class="string">'scenario.antagonists.txt'</span><span>)); </span> </li> <li> <span class="vars">$complicati</span><span>**** = </span><span class="func">explode</span><span>(</span><span class="string">"n"</span><span>, </span><span class="func">file_get_contents</span><span>(</span><span class="string">'scenario.complicati****.txt'</span><span>)); </span> </li> <li class="alt"> <span>shuffle(</span><span class="vars">$settings</span><span>); </span> </li> <li> <span>shuffle(</span><span class="vars">$objectives</span><span>); </span> </li> <li class="alt"> <span>shuffle(</span><span class="vars">$antagonists</span><span>); </span> </li> <li> <span>shuffle(</span><span class="vars">$complicati</span><span>****); </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="vars">$settings</span><span>[0] . </span><span class="string">' '</span><span> . </span><span class="vars">$objectives</span><span>[0] . </span><span class="string">' '</span><span> . </span><span class="vars">$antagonists</span><span>[0] . </span><span class="string">' '</span><span> </span> </li> <li> <span>. </span><span class="vars">$complicati</span><span>****[0] . </span><span class="string">"<br>n"</span><span>; </span> </li> </ol>
我们可以通过添加新文本文件向场景中添加元素,也可能希望添加多重复杂度。添加到基本文本文件中的内容越多,场景随时间的变化就越多。
牌组创建器(Deck builder)和装备(shuffler)
如果您要玩纸牌并且要处理与纸牌相关的脚本,我们需要用装备中的工具整合一副牌组构建器。首先,让我们构建一副标准纸牌。需要构建两个数组 — 一个用于保存同花色的组牌,而另一个用于保存牌面。如果稍后需要添加新组牌或牌类型,则这样做将获得很好的灵活性。
清单 7. 构建一副标准扑克牌
<ol class="dp-c"> <li class="alt"><span><span class="vars">$suits</span><span> = </span><span class="keyword">array</span><span> ( </span></span></li> <li> <span class="string">"Spades"</span><span>, </span><span class="string">"Hearts"</span><span>, </span><span class="string">"Clubs"</span><span>, </span><span class="string">"Diamonds"</span><span> </span> </li> <li class="alt"><span>); </span></li> <li> <span class="vars">$faces</span><span> = </span><span class="keyword">array</span><span> ( </span> </li> <li class="alt"> <span class="string">"Two"</span><span>, </span><span class="string">"Three"</span><span>, </span><span class="string">"Four"</span><span>, </span><span class="string">"Five"</span><span>, </span><span class="string">"Six"</span><span>, </span><span class="string">"Seven"</span><span>, </span><span class="string">"Eight"</span><span>, </span> </li> <li> <span class="string">"Nine"</span><span>, </span><span class="string">"Ten"</span><span>, </span><span class="string">"Jack"</span><span>, </span><span class="string">"Queen"</span><span>, </span><span class="string">"King"</span><span>, </span><span class="string">"Ace"</span><span> </span> </li> <li class="alt"><span>); </span></li> </ol>
然后构建一副牌数组来保存所有纸牌值。只需使用一对 foreach 循环即可完成此操作。
清单 8. 构建一副牌数组
<ol class="dp-c"> <li class="alt"><span><span class="vars">$deck</span><span> = </span><span class="keyword">array</span><span>(); </span></span></li> <li> <span class="keyword">foreach</span><span> (</span><span class="vars">$suits</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$suit</span><span>) { </span> </li> <li class="alt"> <span class="keyword">foreach</span><span> (</span><span class="vars">$faces</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$face</span><span>) { </span> </li> <li> <span class="vars">$deck</span><span>[] = </span><span class="keyword">array</span><span> (</span><span class="string">"face"</span><span>=></span><span class="vars">$face</span><span>, </span><span class="string">"suit"</span><span>=></span><span class="vars">$suit</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li><span>} </span></li> </ol>
在构建了一副扑克牌数组后,我们可以轻松地洗牌并随机抽出一张牌。
清单 9. 洗牌并随机抽出一张牌
<ol class="dp-c"> <li class="alt"><span><span>shuffle(</span><span class="vars">$deck</span><span>); </span></span></li> <li> <span class="vars">$card</span><span> = </span><span class="func">array_shift</span><span>(</span><span class="vars">$deck</span><span>); </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="vars">$card</span><span>[</span><span class="string">'face'</span><span>] . </span><span class="string">' of '</span><span> . </span><span class="vars">$card</span><span>[</span><span class="string">'suit'</span><span>]; </span> </li> </ol>
现在,我们就获得了抽取多副牌或构建多层牌盒(multideck shoe)的捷径。
胜率计算器:发牌
由于构建扑克牌时会分别跟踪每张牌的牌面和花色,因此可以通过编程方式利用这副牌来计算得到特定牌的几率。首先每只手分别抽出五张牌。
清单 10. 每只手抽出五张牌
<ol class="dp-c"> <li class="alt"><span><span class="vars">$hands</span><span> = </span><span class="keyword">array</span><span>(1 => </span><span class="keyword">array</span><span>(), 2=></span><span class="keyword">array</span><span>()); </span></span></li> <li> <span class="keyword">for</span><span> (</span><span class="vars">$i</span><span> = 0; </span><span class="vars">$i</span><span> <span class="vars">$i</span><span>++) { </span></span> </li> <li class="alt"> <span class="vars">$hands</span><span>[1][] = implode(</span><span class="string">" of "</span><span>, </span><span class="func">array_shift</span><span>(</span><span class="vars">$deck</span><span>)); </span> </li> <li> <span class="vars">$hands</span><span>[2][] = implode(</span><span class="string">" of "</span><span>, </span><span class="func">array_shift</span><span>(</span><span class="vars">$deck</span><span>)); </span> </li> <li class="alt"><span>} </span></li> </ol>
然后可以查看这副牌,看看剩余多少张牌以及抽到特定牌的机率是多少。查看剩余的牌数十分简单。只需要计算 $deck 数组中包含的元素数。要获得抽到特定牌的机率,我们需要一个函数来遍历整副牌并估算其余牌以查看是否匹配。
清单 11. 计算抽到特定牌的几率
<ol class="dp-c"> <li class="alt"><span><span class="keyword">function</span><span> calculate_odds(</span><span class="vars">$draw</span><span>, </span><span class="vars">$deck</span><span>) { </span></span></li> <li> <span class="vars">$remaining</span><span> = </span><span class="func">count</span><span>(</span><span class="vars">$deck</span><span>); </span> </li> <li class="alt"> <span class="vars">$odds</span><span> = 0; </span> </li> <li> <span class="keyword">foreach</span><span> (</span><span class="vars">$deck</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$card</span><span>) { </span> </li> <li class="alt"> <span class="keyword">if</span><span> ( (</span><span class="vars">$draw</span><span>[</span><span class="string">'face'</span><span>] == </span><span class="vars">$card</span><span>[</span><span class="string">'face'</span><span>] && </span><span class="vars">$draw</span><span>[</span><span class="string">'suit'</span><span>] == </span> </li> <li> <span class="vars">$card</span><span>[</span><span class="string">'suit'</span><span>] ) || </span> </li> <li class="alt"> <span>(</span><span class="vars">$draw</span><span>[</span><span class="string">'face'</span><span>] == </span><span class="string">''</span><span> && </span><span class="vars">$draw</span><span>[</span><span class="string">'suit'</span><span>] == </span><span class="vars">$card</span><span>[</span><span class="string">'suit'</span><span>] ) || </span> </li> <li> <span>(</span><span class="vars">$draw</span><span>[</span><span class="string">'face'</span><span>] == </span><span class="vars">$card</span><span>[</span><span class="string">'face'</span><span>] && </span><span class="vars">$draw</span><span>[</span><span class="string">'suit'</span><span>] == </span><span class="string">''</span><span> ) ) { </span> </li> <li class="alt"> <span class="vars">$odds</span><span>++; </span> </li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li> <span class="keyword">return</span><span> </span><span class="vars">$odds</span><span> . </span><span class="string">' in '</span><span> </span><span class="vars">$remaining</span><span>; </span> </li> <li class="alt"><span>} </span></li> </ol>
现在可以选出尝试抽出的牌。为了简单起见,传入看上去类似某张牌的数组。我们可以查找特定的一张牌。
清单 12. 查找指定的一张牌
<ol class="dp-c"> <li class="alt"><span><span class="vars">$draw</span><span> = </span><span class="keyword">array</span><span>(</span><span class="string">'face'</span><span> => </span><span class="string">'Ace'</span><span>, </span><span class="string">'suit'</span><span> => </span><span class="string">'Spades'</span><span>); </span></span></li> <li> <span class="func">echo</span><span> implode(</span><span class="string">" of "</span><span>, </span><span class="vars">$draw</span><span>) . </span><span class="string">' : '</span><span> . calculate_odds(</span><span class="vars">$draw</span><span>, </span><span class="vars">$deck</span><span>); </span> </li> </ol>
或者可以查找指定牌面或花色的牌。
清单 13. 查找指定牌面或花色的牌
<ol class="dp-c"> <li class="alt"><span><span class="vars">$draw</span><span> = </span><span class="keyword">array</span><span>(</span><span class="string">'face'</span><span> => </span><span class="string">''</span><span>, </span><span class="string">'suit'</span><span> => </span><span class="string">'Spades'</span><span>); </span></span></li> <li> <span class="vars">$draw</span><span> = </span><span class="keyword">array</span><span>(</span><span class="string">'face'</span><span> => </span><span class="string">'Ace'</span><span>, </span><span class="string">'suit'</span><span> => </span><span class="string">''</span><span>); </span> </li> </ol>
1