根本停不下來其一!通過打游戲來學習Ruby語言 -- Ruby Warrior -- 初級篇


安家博客園的第一篇隨筆.

就來一點有(keng)趣(die)的吧... :)

博文初衷:探索和討論編程的樂趣和美感


說在前面的話:

從學習編程來講,我一直認為探索式學習是最有趣,也是最有效的一種。

而最讓人有探索欲望的...恐怕就是冒險游戲了吧,一直讓大家有進一步深♂入探索的想法。( ̄︶ ̄)↗ 

Ruby Warrior很符合這一特性,讓你感到學習和打游戲毫不矛盾。

一邊玩游戲一邊寫ruby code,用code操縱勇士來冒險,難度由淺入深,根本停不下來...╮(╯▽╰)╭

現在,作為“根本停不下來”系列的第一篇-Ruby Warrior 初級篇,推薦給大家。

關於Ruby語言:可能很多人聽說過了,也可能有些同學並不了解,但這完全無所謂,本游戲沒有上手門檻,並不需要了解太多。(詳情戳我

引用一段話來說明其思想吧:

人們特別是電腦工程師們,常常從機器着想。他們認為:“這樣做,機器就能運行的更快;這樣做,機器運行效率更高;這樣做,機器就會怎樣怎樣怎樣。”

實際上,我們需要從人的角度考慮問題,人們怎樣編寫程序或者怎樣使用機器上應用程序。我們是主人,他們是仆人。

關於本文服務對象:希望從編程中獲得或者發現樂趣的“玩家”,初級篇不需要編程基礎就可以獨立解決(后面幾關需要動動腦)

關於本文目的為了樂趣  ,真的。。就這些

關於提供的代碼: 請盡量獨立完成,代碼僅供參考,時間倉促,作者是個菜鳥,現學現賣,所以並非完美,如果大家有更好的解決方案或者發現紕漏,請務必留言給我 :)


點擊這里進入Ruby Warrior

提示:點擊Venture Forth 並輸入一個昵稱!

提示:點擊Abilities 查看你的“技能”列表


 

攻略與心得

初級篇 - LEVEL 1 - 一直走吧...

 

You see before yourself a long hallway with stairs at the end. There is nothing in the way.

思路:一直走下去...

提示:使用walk!方法來前進

class Player
  def play_turn(warrior)
     warrior.walk!
  end
end
解決方案

初級篇 - LEVEL 2 - 有敵人了!

It is too dark to see anything, but you smell sludge nearby.

思路:前面有敵人要打,沒有就繼續走...

提示:使用feel.empty?來判斷前方一格的狀況

class Player
  def play_turn(warrior)
     if warrior.feel.enemy?
        warrior.attack!
     else
        warrior.walk!
     end
  end
end
解決方案

初級篇 - LEVEL 3 - 爺能回血...傻了吧

The air feels thicker than before. There must be a horde of sludge

思路:血量低於安全值的時候就原地加血吧...否則繼續殺。

提示:使用health方法加血 

 1 class Player
 2   def play_turn(warrior)
 3      if warrior.feel.enemy?
 4         warrior.attack!
 5      else
 6         if warrior.health <= 9 
 7            warrior.rest!
 8         return 0
 9         end
10      warrior.walk!
11      end
12   end
13 end
View Code

 

初級篇 - LEVEL 4 - 量力而行!

 You can hear bow strings being stretched

思路:根據血量判斷,被遠程進攻時不要加血

提示:使用@health變量保存血量並輔助判斷

 1 class Player
 2   @health = 20
 3   def play_turn(warrior)
 4      if warrior.feel.enemy?
 5         then warrior.attack!
 6      else 
 7         if warrior.health <=15 && warrior.health == @health
 8           warrior.rest!
 9           @health += 2
10           return 0
11         end
12         warrior.walk!
13      end
14   @health = warrior.health
15   end
16 end
View Code

 

初級篇 - LEVEL 5 - 是敵是友?

You hear cries for help. Captives must need rescuing.

思路:判斷是敵是友...然后采取策略,注意安全

提示:使用feel.captive?判斷是否需要解救,使用rescue!解救

方案1-多判斷

 

 1 class Player
 2   @health = 20
 3   def play_turn(warrior)
 4      if warrior.feel.enemy?
 5         warrior.attack!
 6      elsif warrior.health <=15 && warrior.health == @health
 7         warrior.rest!
 8         @health += 2
 9         return 0
10      elsif warrior.feel.captive?
11         warrior.rescue!
12         return 0
13      else
14         warrior.walk!
15      end
16   @health = warrior.health
17   end
18 end
方案2-多條件

 

初級篇 - LEVEL 6 - 需要一個策略...

The wall behind you feels a bit further away in this room. And you hear more cries for help.

思路:...一個一個解決,隨時判斷血量不足就退回去補充

提示:使用開關

class Player
  def play_turn(warrior)
    if @ok
      if warrior.feel.enemy?
        warrior.attacked!
        @attacked=true 
      else
        if @attacked and !@isfullHP and !(warrior.feel:backward).wall?
          warrior.walk!:backward 
        elsif (warrior.feel:backward).wall? and warrior.health < 20
          warrior.rest!
          @isfullHP = true
        else
          warrior.walk!
        end
      end
    else
      if (warrior.feel:backward).captive?
        warrior.rescue!:backward
        @ok = true
      else
        warrior.walk!:backward
      end 
    end
  end
end
View Code

 

 

初級篇 - LEVEL 7 - 前面是牆!

You feel a wall right in front of you and an opening behind you.

思路:同上,只是你需要先轉向...

提示: 注意方向

 1 
 2 class Player
 3   def play_turn(warrior)
 4     if warrior.feel.wall?
 5       warrior.pivot!
 6     else
 7       if warrior.feel.enemy?
 8         warrior.attack!
 9         @attack=true # 殺敵
10       else
11         if @attack and !@fullHP and !(warrior.feel:backward).wall?
12           warrior.walk!:backward # 回血
13         elsif (warrior.feel:backward).wall? and warrior.health < 20
14           warrior.rest!
15           @fullHP = true
16         else
17           warrior.walk!
18         end
19       end
20     end
21   end
22 end
View Code

 

 

初級篇 - LEVEL 8 - 隊長別開槍!...

You hear the mumbling of wizards. Beware of their deadly wands! Good thing you found a bow.

思路:使用開關判斷要不要開槍

提示:你可以判斷多格了...

 1 class Player
 2   def play_turn(warrior)
 3     thereisThreat = false
 4     thereisFriend = false
 5     if warrior.look[0].enemy? || warrior.look[1].enemy? || warrior.look[2].enemy?
 6       thereisThreat = true
 7     end
 8     if warrior.look[0].captive? || warrior.look[1].captive? || warrior.look[2].captive?
 9       thereisFriend = true
10     end
11     if thereisThreat == true && thereisFriend == false
12       warrior.shoot!
13       return 0
14     elsif thereisFriend == true
15       if warrior.feel.empty?
16         warrior.walk!
17         return 0
18       elsif warrior.feel.captive?
19         warrior.rescue!
20         thereisFriend = false
21         return 0
22       end
23     elsif thereisThreat == false
24       warrior.walk!
25       return 0
26     end
27   end
28 end
View Code

 

初級篇 - LEVEL ⑨ - 腹背受敵 ----

Time to hone your skills and apply all of the abilities that you have learned.

考驗你的時候到了! 這一關請自行解決...

提示:先解決困難的一面比較好

 

參考代碼:

 1 class Player
 2   def play_turn(warrior)
 3     case @attackedcount.to_i
 4     when 0
 5       warrior.pivot!
 6       @attackedcount = 1
 7     when 1
 8       if warrior.look[0].enemy? or warrior.look[1].enemy? or warrior.look[2].enemy?
 9         warrior.shoot!
10       else
11         @attackedcount = 2
12       end
13     when 2
14       warrior.pivot!
15       @attackedcount = 3
16     when 3
17       if warrior.look[0].enemy? or warrior.look[1].enemy? or warrior.look[2].enemy?
18         warrior.shoot!
19       elsif warrior.feel.captive?
20         warrior.rescue!
21         @attackedcount = 4
22       else
23         warrior.walk!
24       end
25     when 4
26       warrior.pivot!
27       @attackedcount = 5
28     when 5
29       if warrior.feel.captive?
30         warrior.rescue!
31       else
32         warrior.walk!
33       end
34     end
35   end
36 end

 

中級篇(intermediate),地圖變2維的了,

當然要素也大大豐富,敬請期待... :)

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM