昊天SEO

练习52: 创造你的网页游戏–笨方法学ruby在线

这本书马上就要结束了。本章的练习对你是一个真正的挑战。当你完成以后,你就可以算是一个能力不错的 Ruby 初学者了。为了进一步学习,你还需要多读一些书,多写一些程序,不过你已经具备进一步学习的技能了。接下来的学习就只是时间、动力、以及资源的问题了。

在本章练习中,我们不会去创建一个完整的游戏,取而代之的是我们会为《练习42》中的游戏建立一个“引擎(engine)”,让这个游戏能够在浏览 器中运行起来。这会涉及到将《练习42》中的游戏「重构(refactor)」,将《练习47》中的架构混合进来,添加自动测试代码,最后建立一个可以 运行游戏的web 引擎。
这是一节很「庞大」的练习。我预测你要花一周到一个月才能完成它。最好的方法是一点一点来,每晚上完成一点,在进行下一步之前确认上一步有正确完成。

重构《练习42》的游戏

你已经在两个练习中修改了 gothonweb 项目,这节练习中你会再修改一次。这种修改的技术叫做「重构(refactoring)」,或者用我喜欢的讲法来说,叫「修修补补(fixing stuff)」。重构是一个程序术语,它指的是清理旧代码或者为旧代码添加新功能的过程。你其实已经做过这样的事情了,只不过不知道这个术语而已。这 是写软体过程的第二个自然属性。

你在本节中要做的,是将《练习47》中的可以测试的房间地图,以及《练习42》中的游戏这两样东西归并到一起,创建一个新的游戏架构。游戏的内容不会发生变化,只不过我们会通过“重构”让它有一个更好的架构而已。

第一步是将 ex47.rb 的内容复制到 gothonweb/lib/map.rb 中,然后将ex47_tests.rb的内容复制到gothonweb/test/test_map.rb中,然后再次运行测试,确认他们还能正常运作。

Note: 从现在开始我不会再向你展示运行测试的输出了,我就假设你回去运行这些测试,而且知道怎样的输出是正确的。

将《练习47》的代码拷贝完毕后,你就该开始重构它,让它包含《练习42》中的地图。我一开始会把基本架构为你准备好,然后你需要去完成map.rb和map_tests.rb 里边的内容。

首先要做的是使用 Room 类来构建基本的地图架构:

class Room

  attr_accessor :name, :description, :paths

  def initialize(name, description)
    @name = name
    @description = description
    @paths = {}
  end

  def go(direction)
    @paths[direction]
  end

  def add_paths(paths)
    @paths.update(paths)
  end

end

central_corridor = Room.new("Central Corridor",
%q{
The Gothons of Planet Percal #25 have invaded your ship and destroyed
your entire crew.  You are the last surviving member and your last
mission is to get the neutron destruct bomb from the Weapons Armory,
put it in the bridge, and blow the ship up after getting into an 
escape pod.

You're running down the central corridor to the Weapons Armory when
a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown costume
flowing around his hate filled body.  He's blocking the door to the
Armory and about to pull a weapon to blast you.
})


laser_weapon_armory = Room.new("Laser Weapon Armory",
%q{
Lucky for you they made you learn Gothon insults in the academy.
You tell the one Gothon joke you know:
Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr.
The Gothon stops, tries not to laugh, then busts out laughing and can't move.
While he's laughing you run up and shoot him square in the head
putting him down, then jump through the Weapon Armory door.

You do a dive roll into the Weapon Armory, crouch and scan the room
for more Gothons that might be hiding.  It's dead quiet, too quiet.
You stand up and run to the far side of the room and find the
neutron bomb in its container.  There's a keypad lock on the box
and you need the code to get the bomb out.  If you get the code
wrong 10 times then the lock closes forever and you can't
get the bomb.  The code is 3 digits.
})


the_bridge = Room.new("The Bridge",
%q{
The container clicks open and the seal breaks, letting gas out.
You grab the neutron bomb and run as fast as you can to the
bridge where you must place it in the right spot.

You burst onto the Bridge with the netron destruct bomb
under your arm and surprise 5 Gothons who are trying to
take control of the ship.  Each of them has an even uglier
clown costume than the last.  They haven't pulled their
weapons out yet, as they see the active bomb under your
arm and don't want to set it off.
})


escape_pod = Room.new("Escape Pod",
%q{
You point your blaster at the bomb under your arm
and the Gothons put their hands up and start to sweat.
You inch backward to the door, open it, and then carefully
place the bomb on the floor, pointing your blaster at it.
You then jump back through the door, punch the close button
and blast the lock so the Gothons can't get out.
Now that the bomb is placed you run to the escape pod to
get off this tin can.

You rush through the ship desperately trying to make it to
the escape pod before the whole ship explodes.  It seems like
hardly any Gothons are on the ship, so your run is clear of
interference.  You get to the chamber with the escape pods, and
now need to pick one to take.  Some of them could be damaged
but you don't have time to look.  There's 5 pods, which one
do you take?
})


the_end_winner = Room.new("The End",
%q{
You jump into pod 2 and hit the eject button.
The pod easily slides out into space heading to
the planet below.  As it flies to the planet, you look
back and see your ship implode then explode like a
bright star, taking out the Gothon ship at the same
time.  You won!
})


the_end_loser = Room.new("The End",
%q{
You jump into a random pod and hit the eject button.
The pod escapes out into the void of space, then
implodes as the hull ruptures, crushing your body
into jam jelly.
})

escape_pod.add_paths({
    '2' => the_end_winner,
    '*' => the_end_loser
})

generic_death = Room.new("death", "You died.")

the_bridge.add_paths({
    'throw the bomb' => generic_death,
    'slowly place the bomb' => escape_pod
})

laser_weapon_armory.add_paths({
    '0132' => the_bridge,
    '*' => generic_death
})

central_corridor.add_paths({
    'shoot!' => generic_death,
    'dodge!'=> generic_death,
    'tell a joke' => laser_weapon_armory
})

START = central_corridor

本文地址官网    新乡网站建设,seo

分享到:更多 ()

相关推荐

a