2017-02-02 1 views
-1

私はRubyにはとても新しく、試して学ぶためにいくつかの情報源を使ってきました。私はCodecadamyのRubyクラスを終了しました。私はHard Rubyを学ぶことで約75%です。また、私はEloquent RubyとPickaxe Ruby bookの100ページを読んだことがあります。RubyでのRPGテキストベースのゲームの作成。コントロールとシーンを保存して実行するには?

私はRubyでテキストベースのRPGゲームを実践しようとしています。私はゲームをよく知っているので、Chrono Triggerを再現することに決めました。私はキャラクターやストーリーを考えるのに時間をかけたくありません。コーディングを練習したいだけです。

私は、プレーヤーが使用するための4つの簡単なコントロールを作ろうとしましたが、シーンに組み込むことは非常に困難です。

たとえば、私はコントロール "talk"を持っており、ユーザーが入力したものに基づいてシーンを実行するために 'if/else'ステートメントを使用します。ユーザーが「話す」を何度も入力すると、無限のif/elseシナリオを辿る必要があります。

以下は私のコードです。私は初心者なので非常に乱雑ですが、誰かがそれを見て、それがうまくいくようにする方法についていくつかのアドバイスをくれたら!

---------ようこそ方法-----------

def welcome 
    puts "Welcome to Chrono Trigger: Ruby Edition!" 
    puts "Let's discuss the controls to help you become familiar with the game." 
    puts "" 
    puts "We are going to break the controls down into categories to help make it easier to understand" 
    puts "Anytime you get stuck just type 'help' to bring the controls back up." 
    puts "" 
    puts "The controls are 'talk' 'action' 'attack' 'flee'." 
    puts "" 
    puts "Simple enough? Type 'start' to begin!" 

    print "> " 
    response = $stdin.gets.chomp 
     if response == "start" then chapter_1_start end 
end 

---------クロノは、次の何をすべき? - ----

def crono_next 
    puts "What should Crono do next?" 
    @next_task = $stdin.gets.chomp 
end 


def chapter_1_start 
    puts "(Bells Ringing)" 
    puts "\"Crono....Crono...\"" 
    puts "\"Time to get up!\"" 
    puts "" 
    puts "Crono wakes up from bed." 
    puts "Crono is standing in the middle of his bedroom" 
    print "> " 
    response = $stdin.gets.chomp 
    if response == "help" 
     help 
    elsif response == "action" 
     puts "Crono closes the curtains." 
     puts "Then he leaves the room" 
     chapter_1_start2 
    elsif response == "talk" 
     puts "Crono talks to himself" 
    else 
     puts "..." 
    end  
end 

def chapter_1_start2 

    def talk 
     puts "Mom: \"Oh Crono, don't forget to stop by your friend Lucca's house before you head to the Millenial Fair.\"" 
     crono_next 
     if @next_task == "talk" 
      puts "Mom: \"I almost forgot. Here's 200 gil to buys some cat food while you're out.\"" 
      crono_next 
      if @next_task == "talk" 
       puts "Mom: \"You really should be leaving now Crono\"" 
       chapter_1_start3 
      elsif @next_task == "action" 
       puts "Crono pets his cat." 
       puts "'Meow'" 
       chapter_1_start3 
      else 
       chapter_1_start3 
      end  

     elsif @next_task == "action" 
      action 
     else 
      "Mom: \"You really should be leaving now Crono\"" 
      chapter_1_start3 
     end 
    end 

    def action 
     puts "Crono pets his cat." 
     puts "'Meow'" 
     crono_next 
     if @next_task == "talk" 
      talk 
     elsif @next_task == "action" 
      action 
     else 
      "Crono does nothing." 
     end  
    end 
    puts "Crono heads downstairs and sees his mom in the kitchen and his cat in the living room." 
    crono_next 
    if @next_task == "talk" 
     talk 
    elsif @next_task == "action" 
     action 
    else 
     puts "Let's try that again." 
     chapter_1_start2 
    end  
end 

def chapter_1_start3 
    puts "" 
    puts "Crono begins walking away from his house" 
    puts "If he travels West he will be heading in the direction of Lucca's house." 
    puts "If he travels East he will be headed to the Millenial Fair." 
    puts crono_next 

----------------ヘルプメニュー----------------

def help 
    puts "What do you need help with? 'controls', 'hints', 'quit'" 
    print "> " 
    help_subject = $stdin.gets.chomp 
    if help_subject == "controls" 
     puts "The controls are 'talk' 'action' 'attack' 'flee'." 
    elsif help_subject == "hints" 
     puts "I got nothing" 
    elsif help_subject == "quit" 
     puts "Are you sure you want to quit?(Your game will not be saved)" 
     print "> " 
     sure_quit = $stdin.gets.chomp 
     if sure_quit.include? "y" 
      exit(0) 
     else 
      help 
     end 
    else 
     puts "Guess you didn't really need help after all." 
    end 
end 

----------------ヘルプメニュー-----------------

def talk(person) 
    @person = person 
end 

welcome 
+4

投稿をコードの特定の領域を指す特定の質問に限定してください –

+0

作業コードに関する質問はhttp://codereview.stackexchange.comに適していますが、いくつかのアップ。それは座っているときには少し "コードダンプ"ですが、それが小さなコードセットに集中すれば、それは良い質問かもしれません。 –

+0

ごめんなさい!私は一般的にコミュニティとプログラミングに新しいです、私はこのことについて謝ります。 – Caleb

答えて

1

テキストベースのゲームは、実際にはかなり複雑で、従来のビデオゲームと同じロジックおよびデザインパターンの多くに従います。ゲームデザインのガイドについては、googleを利用することができます。それらの多くがあり、基本はすべてのプログラミング言語に適用されます。

言い換えれば、無限のif文で失われることなくそのままゲームを続けたい場合は、クラスを使用してシーンを管理する必要があります。

すべてのゲームには、メインループがあります。このループは、画面への表示、ユーザー入力の収集、およびゲーム状態の変化の計算という3つの主なタスクを処理する無限ループです。それは何度も何度も起こる:

#main.rb 
scene = Scene.new(:chapter_1) 
while true 
    scene.display 
    print "> " 
    action = $stdin.gets.chomp 
    if command == 'talk' 
    scene = scene.talk 
    elsif command == 'action' 
    scene = scene.action 
    elsif command = 'attack' 
    scene = scene.attack 
    elsif command = 'flee' 
    scene = scene.flee 
    else 
    puts 'unknown command!' 
    end 
end 

シーンクラスは、基本的には同様の行動が他のどのシーンにどのシーンにリンクされ、各シーンのテキストを保持しています。この構造は、ステートマシンと呼ばれ、それがゲームの状態を追跡するための最も簡単な方法です:

#scene.rb 
class Scene 
    def initialize beginning_state 
    @state = beginning_state 
    @scenes = { 
     chapter_1: [ 
     "Hello and welcome to chapter 1 of my game!", 
     :ch1_talk, 
     :ch1_action, 
     :ch1_attack, 
     :chapter_2], 
     ch1_talk: [ 
     "Please stop talking while I'm talking!", 
     :ch1_talk, 
     :ch1_action, 
     :ch1_attack, 
     :chapter_2], 
     ch1_action: [ 
     "W-what are you doing?!", 
     :ch1_talk, 
     :ch1_action, 
     :ch1_attack, 
     :chapter_2], 
     ch1_attack: [ 
     "Stop, that hurts :c", 
     :ch1_talk, 
     :ch1_action, 
     :ch1_attack, 
     :chapter_2], 
     chapter_2: [ 
     "Congratulations, you have entered...\n chapter 2!", 
     :ch2_talk, 
     :ch2_action, 
     :ch2_attack, 
     :ch2_flee], 
     ch2_talk: [ 
     "I'm ignoring you", 
     :ch2_talk, 
     :ch2_action, 
     :ch2_attack, 
     :ch2_flee], 
     ch2_action: [ 
     "I don't even care", 
     :ch2_talk, 
     :ch2_action, 
     :ch2_attack, 
     :ch2_flee], 
     ch2_attack: [ 
     "You're no match for me", 
     :ch2_talk, 
     :ch2_action, 
     :ch2_attack, 
     :ch2_flee], 
     ch2_flee: [ 
     "Okay, goodbye!", 
     :ch2_talk, 
     :ch2_action, 
     :ch2_attack, 
     :ch2_flee], 
    } 
    end 

    def display 
    puts @scenes[@state][0] 
    end 

    def talk 
    @state = @scenes[@state][1] 
    end 

    def action 
    @state = @scenes[@state][2] 
    end 

    def attack 
    @state = @scenes[@state][3] 
    end 

    def flee 
    @state = @scenes[@state][4] 
    end 
end 

は申し訳ありませんが、それは少し複雑ですが、ゲームは少し複雑です。理想的には、あなたのシーンを特別にフォーマットされたテキストファイルに保存してから、私のようにソースコードで定義するのではなく、ゲームにロードするのが理想的です。

+0

これにコメントする時間をとってくれてありがとう!はい、私はこれが私の理解のためにはあまりにも複雑すぎることに同意しますが、私のプロジェクトを進める方法についての素晴らしい洞察を私に提供します。あなたの事例で使用しているシーンがシンボルとみなされていますか?メークアップは価値のないハッシュに似ています。 – Caleb

+0

喜んで助けてください!はい、私はシンボルを配列に入れて使用しています。ハッシュは一般的にシンボルをキーとして使用するので、ここで似ていると思われます。 P.S. StackOverflowへようこそプログラミングの世界c: – eiko

関連する問題