-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameExample.rb
173 lines (157 loc) · 4.91 KB
/
GameExample.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
class Player
attr_reader :name, :level
attr_accessor :gold, :weapon, :armor, :hitpoints
def initialize(name, hitpoints, weapon, armor, level, gold)
@name = name
@hitpoints = hitpoints
@weapon = weapon
@armor = armor
@level = level
@gold = gold
end
def attack(target)
target.defend(rand((@weapon + 1)*6), self)
end
def defend(damage, attacker)
if @armor != 0
block = rand(@armor * 4)
else
block = 0
end
total = damage - block
if total <= 0
total = 0
end
if block > 0
puts "The #{attacker.name} attacks #{self.name} for #{damage} with a #{$weapons[attacker.weapon]}, but #{self.name} blocks for #{block}, resulting in #{total} damage."
else
puts "The #{attacker.name} attacks #{self.name} for #{damage} with a #{$weapons[attacker.weapon]}, resulting in #{total} damage."
end
@hitpoints = @hitpoints - total
if @hitpoints <= 0
die
end
end
def die
puts "The #{@name} falls dramatically to its death on the hard forest floor."
end
end
$weapons = ["nothing", "rusty dagger", "dagger", "sharp dagger", "rusty sword", "sword", "sharp sword", "rusty axe", "axe", "sharp axe"]
$armors = ["nothing", "hannah montannah t-shirt", "leather coat", "chain mail vest"]
$monsters = [Player.new("Rat", 5, 0, 0, 1, 10), Player.new("Wolf", 10, 0, 0, 1, 15), Player.new("Mummy", 15, 0, 0, 1, 20), Player.new("Skeleton Warrior", 20, 1, 1, 2, 25), Player.new("Forest Troll", 30, 2, 3, 2, 40)]
def getname
puts "Greetings traveler. What is your name?"
name = gets.chomp
puts "#{name}, eh? That's a strange name, are you sure?(Y/n)"
choice = gets.chomp
if choice == "n"
getname
end
@player = Player.new(name, 20, 0, 0, 1, 20)
end
def menu
puts "You have #{@player.gold} gold."
puts "1) Go into the forest to fight."
puts "2) Go to the weapon shop"
puts "3) Go to the armor shop"
puts "4) Go to the INN"
puts "5) Quit the game"
choice = gets.chomp.to_i
case choice
when 1
forest
when 2
weapons
when 3
armor
when 4
inn
when 5
exit
else
menu
end
end
def forest
stop = false
monster = $monsters[rand($monsters.length)].clone
until stop
puts "A #{monster.name} lurches toward you."
puts "You have #{@player.hitpoints} hitpoints."
puts "What do you do?"
puts "1) Attack #{monster.name}"
puts "2) Run back to town"
choice = gets.chomp.to_i
case choice
when 1
@player.attack(monster)
if monster.hitpoints > 0
monster.attack(@player)
if @player.hitpoints <= 0
puts "Game over."
exit
end
else
puts "You stand over #{monster.name} in triumph, and pick up #{monster.gold} gold pieces from the body!"
@player.gold += monster.gold
stop = true
end
when 2
stop = true
end
end
menu
end
def weapons
puts "You step into the weapon shop, carrying your #{$weapons[@player.weapon]} high."
$weapons.each_with_index do |weapon_name, weapon_number|
puts "#{weapon_number}) #{weapon_name}: #{weapon_number * 10} gold"
end
puts "You have #{@player.gold} gold, and I will give you #{@player.weapon * 5} gold for your crummy old #{$weapons[@player.weapon]}."
puts "What would you like to buy, #{@player.name}?"
choice = gets.chomp.to_i
if choice * 10 <= @player.gold + @player.weapon * 5
@player.gold += @player.weapon * 5
@player.weapon = choice
@player.gold = @player.gold - choice * 10
puts "You are now using a #{$weapons[choice]} as your weapon. Scary stuff!"
else
puts "I am sorry, you don't have enough gold. The #{$weapons[choice]} costs #{choice * 10} gold, and you are #{choice * 10 - @player.gold} gold short."
end
menu
end
def armor
puts "You step into the armor shop, wearing your #{$armors[@player.armor]} proudly."
$armors.each_with_index do |armor_name, armor_number|
puts "#{armor_number}) #{armor_name}: #{armor_number * 10} gold"
end
puts "You have #{@player.gold} gold."
puts "What would you like to buy, #{@player.name}?"
choice = gets.chomp.to_i
if choice * 10 <= @player.gold
@player.armor = choice
@player.gold = @player.gold - choice * 10
puts "You are now protected by #{$armors[choice]}. Don't you feel invincible?"
else
puts "I am sorry, you don't have enough gold. The #{$armors[choice]} costs #{choice * 10} gold, and you are #{choice * 10 - @player.gold} gold short."
end
menu
end
def inn
puts "You are in a warm and cozy inn."
missing_hitpoints = @player.level * 20 - @player.hitpoints
puts "It will be #{missing_hitpoints} gold to stay. Are you interested?(y/N)"
choice = gets.chomp
if choice == "y"
if @player.gold > missing_hitpoints
@player.hitpoints = @player.level * 20
puts "You leave the Inn, feeling refreshed with a full #{@player.hitpoints} hitpoints, but a wallet #{missing_hitpoints} gold lighter!"
@player.gold -= missing_hitpoints
else
"I'm sorry, you are #{missing_hitpoints - @player.gold} gold short."
end
end
menu
end
getname
menu