Is there a more efficient way to control the console cursor(carriage position and so on) using Ruby?

Viewed 35

In a text based game that I'm making with Ruby, I wanted to have multiple colors on a single line of text. The paint gem would only seem to work with two colors but I wanted more, so I looked up those terminal codes to manipulate the cursor position so I could move up a line and print a string in a new color.

Is there a more efficient way to do this? Here is the code for the buy function from the shop:

      clr
      draw_stats_main
      shopbag = Game_DB.weapons_array
      bagkeys = shopbag.keys
      bagkeys.delete_if {|i| i == 0}
      shopbag.each { |id, value|
        if id != 0
          if id >= 10
            string1 = " (" + id.to_s + ")" + " " + value[0]
            str1 = string1.size
          else
            string1 = "  (" + id.to_s + ")" + " " + value[0]
            str1 = string1.size
          end
          string2 = "ATTACK: #{value[1]}"; str2 = string2.size
          string3 = "SPEED: #{value[2]}"; str3 = string3.size
          string4 = "" if !value[3]
          string4 = "TWO HANDED WEAPON" if value[3]
          str3 = string3.size
          pa "#{string1}"
          pa "\033[1A \033[#{str1}C #{string2}", :red, :bright
          pa "\033[1A \033[#{str1+str2+4}C #{string3}", :yellow, :bright if value[1] < 10
          pa "\033[1A \033[#{str1+str2+str3+9}C #{string4}", :red if value[1] < 10
          pa "\033[1A \033[#{str1+str2+3}C #{string3}", :yellow, :bright if value[1] > 10 && value[1] < 100
          pa "\033[1A \033[#{str1+str2+str3+8}C #{string4}", :red if value[1] > 10 && value[1] < 100
          pa "\033[1A \033[#{str1+str2+2}C #{string3}", :yellow, :bright if value[1] >= 100
          pa "\033[1A \033[#{str1+str2+str3+7}C #{string4}", :red if value[1] >= 100
          pa "        >> Cost: #{value[4]}", :yellow
        end
      }
      pa "#{Game_DB.tx(:other, 0)}"
      pa "#{Game_DB.tx(:common, 22)}", :green, :bright
      loop do
        key = gets.chomp.to_i
        if bagkeys.include?(key)
          if @player.gold >= shopbag[key][4]
            @player.remove_gold(shopbag[key][4])
            @player.add_item(:weapon, key)
            weap = shopbag[key][0]; weapstr = weap.delete " "
            pa "#{Game_DB.tx(:other, 0)}"
            pa " You purchased #{weapstr} for #{shopbag[key][4]} gold! Dont forget to equip it!", :green, :bright
            key = gets
            break
          else
            pa " You don't have enough gold!", :red
            key = gets
            break
          end
        elsif key == 0
          @shopmenu[0] = false
          break
        end
      end

Here is a screenshot of what the player sees with this block of code: Weapons Shop screenshot

0 Answers
Related