gpt4 book ai didi

Ruby:RPG 库存系统

转载 作者:数据小太阳 更新时间:2023-10-29 09:01:25 27 4
gpt4 key购买 nike

既然我终于弄清楚了类继承在 ruby​​ 中的工作原理,我正在为文本 rpg 设计一个库存系统。但是,有些怪癖我无法弄清楚。

项目类别:

class Item
attr_accessor :name, :type, :attack, :armor, :wearloc, :weight, :price
def initialize(name, type, attack, armor, wearloc, weight, price)
@name = name
@type = type
@attack = attack
@armor = armor
@wearloc = wearloc
@weight = weight
@price = price
end
end

库存类:

class Inventory
attr_accessor :items
def initialize
@@items = []
end

def add_item(item)
@@items << item
end

attr_accessor :inventory
def inventory
@@items.each do |item|
puts "#{item.name} (#{item.type})"
if item.attack != nil ; puts " %-20s %00d" % ['Attack', item.attack] ; end
if item.armor != nil ; puts " %-20s %00d" % ['Armor', item.armor] ; end
if item.wearloc != nil ; puts " %-20s %00s" % ['Wear', item.wearloc] ; end
if item.weight != nil ; puts " %-20s %00d" % ['Weight', item.weight] ; end
if item.price != nil ; puts " %-20s %00d" % ['Price', item.price] ; end
end
end
end

玩家类:

class Player < Inventory
attr_accessor :playername
def initialize(playername)
@playername = playername
end
end

实例化库存,创建一些项目,将它们添加到库存中所有工作。

inv = Inventory.new
broadsword = Item.new('a heavy broadsword', 'weapon', 5, nil, 'wield', 15, 2)
breastplate = Item.new('a mithril breastplate', 'armor', nil, 10, 'torso', 15, 5)
ring = Item.new('a gold ring', 'armor', nil, 3, 'finger', 2, 20)
inv.add_item(broadsword)
inv.add_item(breastplate)
inv.add_item(ring)

但是,当我从 Player 类调用 inventory 方法时,我得到了所需的输出,加上 3 个 #item 对象,如下所示:

a heavy broadsword (weapon)
Attack 5
Wear wield
Weight 15
Price 2
a mithril breastplate (armor)
Armor 10
Wear torso
Weight 15
Price 5
a gold ring (armor)
Armor 3
Wear finger
Weight 2
Price 20
#<Item:0x007fae1b8b11d8>
#<Item:0x007fae1b8b1138>
#<Item:0x007fae1b8b1098>

为什么最后三行在那里?我不知道它来自哪里或如何修复它。

更新:

使用实例变量而不是类变量对 Item < Inventory < Player 继承进行了重新设计。

class Player
attr_accessor :playername
def initialize(playername)
@playername = playername
end
end

class Inventory < Player
attr_accessor :items
def initialize
@items = []
end

def add_item(item)
@items << item
end

attr_accessor :inventory
def inventory
@items.each do |item|
puts "#{item.name} (#{item.type})"
if item.attack != nil ; puts " %-20s %00d" % ['Attack', item.attack] ; end
if item.armor != nil ; puts " %-20s %00d" % ['Armor', item.armor] ; end
if item.wearloc != nil ; puts " %-20s %00s" % ['Wear', item.wearloc] ; end
if item.weight != nil ; puts " %-20s %00d" % ['Weight', item.weight] ; end
if item.price != nil ; puts " %-20s %00d" % ['Price', item.price] ; end
end
nil
end
end

class Item < Inventory
attr_accessor :name, :type, :attack, :armor, :wearloc, :weight, :price
def initialize(name, type, attack, armor, wearloc, weight, price)
@name = name
@type = type
@attack = attack
@armor = armor
@wearloc = wearloc
@weight = weight
@price = price
end
end

inv = Inventory.new
broadsword = Item.new('a heavy broadsword', 'weapon', 5, nil, 'wield', 15, 2)
breastplate = Item.new('a mithril breastplate', 'armor', nil, 10, 'torso', 15, 5)
ring = Item.new('a gold ring', 'armor', nil, 3, 'finger', 2, 20)
inv.add_item(broadsword)
inv.add_item(breastplate)
inv.add_item(ring)

player = Player.new('Chris')
# puts player.inventory
puts inv.inventory

最佳答案

ruby 中的方法返回方法中最后一条语句的值。

Inventory#inventory 的情况下,最后一个语句是 @@items.each 方法。 each 返回调用它的集合,因此返回值是 @@items 数组。

如果你不想让方法返回任何东西,你可以在最后放一个nil:

def inventory
@@items.each do |item|
# code to print the item stats
end

nil
end

关于Ruby:RPG 库存系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34645578/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com