根本停不下来其一!通过打游戏来学习Ruby语言 -- Ruby Warrior -- 初级篇
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了根本停不下来其一!通过打游戏来学习Ruby语言 -- Ruby Warrior -- 初级篇,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6947字,纯文字阅读大概需要10分钟。
内容图文
原文链接:http://www.cnblogs.com/fancylear/p/3672222.html安家博客园的第一篇随笔.
就来一点有(keng)趣(die)的吧... :)
博文初衷:探索和讨论编程的乐趣和美感
说在前面的话:
从学习编程来讲,我一直认为探索式学习是最有趣,也是最有效的一种。
而最让人有探索欲望的...恐怕就是冒险游戏了吧,一直让大家有进一步深♂入探索的想法。( ̄︶ ̄)↗
Ruby Warrior很符合这一特性,让你感到学习和打游戏毫不矛盾。
一边玩游戏一边写ruby code,用code操纵勇士来冒险,难度由浅入深,根本停不下来...╮(╯▽╰)╭
现在,作为“根本停不下来”系列的第一篇-Ruby Warrior 初级篇,推荐给大家。
关于Ruby语言:可能很多人听说过了,也可能有些同学并不了解,但这完全无所谓,本游戏没有上手门槛,并不需要了解太多。(详情戳我)
引用一段话来说明其思想吧:
人们特别是电脑工程师们,常常从机器着想。他们认为:“这样做,机器就能运行的更快;这样做,机器运行效率更高;这样做,机器就会怎样怎样怎样。”
实际上,我们需要从人的角度考虑问题,人们怎样编写程序或者怎样使用机器上应用程序。我们是主人,他们是仆人。
关于本文服务对象:希望从编程中获得或者发现乐趣的“玩家”,初级篇不需要编程基础就可以独立解决(后面几关需要动动脑)
关于本文目的:为了乐趣 ,真的。。就这些
关于提供的代码: 请尽量独立完成,代码仅供参考,时间仓促,作者是个菜鸟,现学现卖,所以并非完美,如果大家有更好的解决方案或者发现纰漏,请务必留言给我 :)
提示:点击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 endView 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 endView 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 endView 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 endView 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 endView 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维的了,
当然要素也大大丰富,敬请期待... :)
转载于:https://www.cnblogs.com/fancylear/p/3672222.html
内容总结
以上是互联网集市为您收集整理的根本停不下来其一!通过打游戏来学习Ruby语言 -- Ruby Warrior -- 初级篇全部内容,希望文章能够帮你解决根本停不下来其一!通过打游戏来学习Ruby语言 -- Ruby Warrior -- 初级篇所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。