Skip to content

Instantly share code, notes, and snippets.

@Seralto
Last active March 14, 2023 21:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Seralto/c7cd248d0fade417c7f010717e44e359 to your computer and use it in GitHub Desktop.
Save Seralto/c7cd248d0fade417c7f010717e44e359 to your computer and use it in GitHub Desktop.
Mini jogo de RPG em Ruby
class Monstro
attr_accessor :energia, :ataque, :vivo
def initialize
self.energia = Random.rand(10) + 6
self.vivo = true
end
def bater(alvo)
if alvo.esta_vivo?
self.ataque = Random.rand(5)
puts "O dano do monstro foi #{self.ataque}"
alvo.energia -= self.ataque
else
puts 'Voce está morto!'
end
end
def esta_vivo?
true if self.energia > 0
end
end
class Heroi
attr_accessor :energia, :ataque, :vivo, :numero_de_mortos
def initialize
self.energia = 30
self.vivo = true
self.numero_de_mortos = 0
end
def bater(alvo)
if alvo.esta_vivo?
self.ataque = Random.rand(5) + 3
puts "Você acertou o monstro, seu dano foi #{self.ataque}"
alvo.energia -= self.ataque
else
puts "O monstro está morto!"
end
unless alvo.esta_vivo?
puts "O monstro está morto!\n\n"
self.numero_de_mortos += 1
end
end
def esta_vivo?
true if self.energia > 0
end
end
odim = Heroi.new
puts odim.inspect
while odim.esta_vivo?
fishman = Monstro.new
puts fishman.inspect
while fishman.esta_vivo? && odim.esta_vivo?
odim.bater(fishman)
puts "A energia do Fishman é #{fishman.energia}" if fishman.esta_vivo?
if fishman.esta_vivo?
fishman.bater(odim)
print "Sua energia é #{odim.energia}"
puts ''
end
end
end
puts 'Odim está morto.'
puts "Você matou #{odim.numero_de_mortos} monstros"
@ricardo-benicio
Copy link

$ ruby RPG.rb
#<Heroi:0x000001eacec12dd8 @energia=30, @vivo=true, @numero_de_mortos=0>
RPG.rb:54:in <main>': undefined method esta_vivo?' for #<Heroi:0x000001eacec12dd8 @energia=30, @vivo=true, @numero_de_mortos=0> (NoMethodError)

while odin.esta_vivo?\r
^^^^^^^^^^^

o que pode está acontecendo nesse erro Professor? meu editor é vscode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment