Game Developement Problems

Ok, so my problem is with this code. I want the if statements with the
variable race to set other variables. Which works as far as I can tell.
But after the if statements the program ends. I want it to continue but
I am stumped about how to go about it. Any help would be appreciated,
thanks.
Also, I need to finish the whole game by the 8th or the 9th of December.

race = “”
health = 0
magic = 0
fatigue = 0

def paws
sleep(0.05)
end

def pause
sleep(0.10)
end

print “W”
pause
print “e”
pause
print “l”
pause
print “c”
pause
print “o”
pause
print “m”
pause
print “e”
pause
print " "
print “T”
pause
print “o”
pause
print " "
pause
print “M”
pause
print “y”
pause
print " "
pause
print “R”
pause
print “P”
pause
print “G”
pause
print “!”
input_needed = gets.chomp
print “W”
paws
print “h”
paws
print “a”
paws
print “t”
paws
print " "
paws
print “r”
paws
print “a”
paws
print “c”
paws
print “e”
paws
print " "
paws
print “a”
paws
print “r”
paws
print “e”
paws
print " "
paws
print “y”
paws
print “o”
paws
print “u”
paws
print “?”
input_needed = gets.chomp
puts “Aelv - 80 HP - 25 MP - 80 - FP”
puts “Elf - 50 HP - 75 MP - 60 FP”
race = gets.chomp

if @race =~ /aelv/ or /Aelv/
health + 70
magic + 25
fatigue + 100
elsif @race =~ /Elf/ or /elf/
health + 50
magic + 75
fatigue + 80

puts “What is your combat specialization?”
sleep(2)
puts “Melee”
puts “Magic”
combat = gets.chomp

if combat $=~/^magic/ or $=~/^Magic/
magic + 25
fatigue + 20

if combat $=~/^Melee/ or $=~/^melee/
health + 15
fatigue + 10

puts “An #{user_race.capitalize} that specializes in #{combat} combat?”
sleep(2)
puts “With stats like those, you won’t have any trouble breaking out of
your cell, ehh…”
sleep(3)
puts “What’s your name again?”
sleep(1)
name = gets.chomp

puts “Well, #{name.capitalize}, your journey begins here.”
sleep(5)
puts “OH! I almost forgot! Your skills. Seeing as you specialize in
#{combat}, maybe you should chose skills”
puts “fall under that category.”
sleep(1)

if combat $=~/^melee/ or $=~/^Melee/
puts “First, you’re weapon skill.”
sleep(1)
puts “Blades”
puts “Axes”
puts “Maces”
weapon_skill = gets.chomp
end

blades = 10
axes = 10
maces = 10

if weapon_skill $=~/^Blades/ or $=~/^blades/
blades + 15

if weapon_skill $=~/^Axes/ or $=~/^axes/
axes + 15

if weapon_skill $=~/^Maces/ or $=~/^maces/
maces + 15

puts “#{weapon_skill}, huh? I figured you a mace user.”
sleep(2)

if combat $=~/^magic/ or $=~/^Magic/
puts “Seeing as you specialize in magic, chose what category.”
sleep (2)
puts “Destruction”
puts “Illusion (used to charm others and perform invisibility spells)”
puts “Alteration (used to frenzy enemies or knock them out)”
magic_skill = gets.chomp

if magic_skill $=~/^destruction/ or $=~/^Destruction/
destruiction + 15

if magic_skill $=~/^illusion/ or $=~/^Illusion/
illusion + 15

if magic_skill $=~/^alteration/ or $=~/^Alteration/
alteration + 15
end
end
end

destruction = 10
illusion = 10
alteration = 10

puts “And now your journey begins. You shall no longer need my, the
anonymous voice inside your code.”
sleep(2)
puts “Good luck! And remember! Your decisions will play a huge part in
the upcoming war.”

end
end
end
end
end
end
end
$end
$end

I have a suggestion:

def print_with_pause( phrase, pause_length = 0.05 )
phrase.each_char { |c| print c; sleep 0.1 }
end

print_with_pause ‘Welcome To My RPG!’, 0.1

print_with_pause ‘What Are You?’

Also, I would recommend looking at a Class to keep your values in,
rather than having them float around the main environment; and look into
loops so you can validate input and give the user another chance if they
input something invalid.

I think the problem with your “if” statements is that you have nested
them. Using indentation will help you track where the statements are
nested.

For example:

if combat $=~/^magic/ or $=~/^Magic/
magic + 25
fatigue + 20

This only gets executed if combat contains magic!

if combat $=~/^Melee/ or $=~/^melee/
health + 15
fatigue + 10

end # if combat is melee

end # if combat is magic

I don’t quiet understand your print with pause idea, sorry, I’m new to
Ruby and coding in general.But I fixed the if statements. Well… I
fixed one of the problems. Now some of my if statements wont get
executed! And what do you mean by class? I solved my first if problem by
doing this instead :

puts “Aelv - 80 HP - 25 MP - 80 - FP”
puts “Elf - 65 HP - 85 MP - 50 FP”

race = gets.chomp.capitalize

puts " "

if @race =~ /Elf/
health + 65
magic + 85
fatigue + 50
puts “An Elf?”

elsif @race =~ /Aelv/
health + 80
magic + 25
fatigue + 80
puts “An Aelv?”

else

Also, thanks for the quick replies!

Try experimenting with the method print_with_pause to find out what it
does:

print_with_pause ‘Hi!’

is the same thing as this:

print ‘H’
pause
print ‘i’
pause
print ‘!’
pause

Using methods like this makes your code much simpler to write, and to
read.

It might be a bit early for you to learn classes, but if you’re building
something like a game they will really help you to handle complex
objects.

I recommend you look up “Object Oriented Programming”, which tends to
use classes and makes complex programs much simpler to develop. What
you’re writing at the moment is Procedural, which tends to be a program
which runs sequentially down the page.

Here’s a simple example of something you could do:


class Hero

attr_accessor :health, :magic, :fatigue

def initialize( race )

case race

when /^Aelv/i

  self.health = 70
  self.magic = 25
  self.fatigue = 100

when /^Elf/i

  self.health = 50
  self.magic = 75
  self.fatigue = 80

else

  fail ArgumentError, 'race: ' + race.to_s + ' not recognised!'

end

def to_s
  "Health: #{ health }\nMagic: #{ magic }\nFatigue: #{ fatigue }"
end

end

end

race = gets

#> Returns “Elf\n”

hero = Hero.new( race )

#> Returns #<Hero:0x00000002b8a298 @health=50, @magic=75, @fatigue=80>

puts hero

“Health: 50
Magic: 75
Fatigue: 80”

case is often shorter and easier to read than an if statement, plus you
can feed it things like Regexp and Range and will handle them naturally.

A “case” statement clearly says “I am making a decision based on the
race”, whereas an “if” statement might end up including any number of
other components in the decision-making.

I dislike having to repeat myself in code. Imagine changing the variable
“race” to something else. If you use an “if/elsif” statement, you’ll
need to change it on every line!

Sounds good! I will look into it. I already started looping all the
input places incase someone puts in something invalid. Thanks! If I find
anymore problems… no. When I find anymore problems I will come back
here! Thanks again!

Joel P. wrote in post #1179697:

Try experimenting with the method print_with_pause to find out what it
does:

print_with_pause ‘Hi!’

is the same thing as this:

print ‘H’
pause
print ‘i’
pause
print ‘!’
pause

I understand what the print with pause does now, thanks for telling me
about it :smiley: It really shortened the amount of code! But I can’t
understand what case does? And if I can use case in place of an if
statement, why not just use an if statement? Thanks again!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs