-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgammm.rb
269 lines (236 loc) · 6.16 KB
/
gammm.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# wha to do next time: figure out how to remove sword from inventory when its used against a monster
require 'io/console'
class Tile
# if something is there or not
attr_accessor :solid # true/false based on whether the character can stand there or not
attr_accessor :score # does this cell have any kind of value?
attr_accessor :damage # does this cell cause damage, and how much
attr_accessor :display # what does this look like on the map?
attr_accessor :message # what it should display when on this tile
attr_accessor :sword
def initialize
@solid = false
@score = 0
@damage = 0
@display = ' • '
# @message = "Fwffffwwww"
end
def step player
# maybe add to score if there is any
# subtract from health if there is any damage
player.score += @score
@score = 0
player.health -= @damage
end
end
class Monster < Tile
def initialize
super
@score = -10
@display = '\|/'
@message = "MONSTER!!!!#{7.chr}"
@times_i_got_stepped_on = 0
end
def step player
super
@display = '\|/'
if @times_i_got_stepped_on >= 1
@message = "There's nothing but a pile of bones here."
elsif player.sword >= 1
@times_i_got_stepped_on += 1
@message = "You used your sword to kill the monster!\nYour sword broke."
player.score + 10
player.sword -= 1
player.inventory.delete([0])
else
player.health -= 1
end
end
end
class Wall < Tile
def initialize
super
@solid = true
@display = ' ∆ '
@message = "Oh boy, this is bad"
end
end
class Sword < Tile
def initialize
super
@sword = '†'
@display = '\|/'
@message = "YOU FOUND A SWORD!"
@times_i_got_stepped_on = 0
end
def step player
super
@display = '\|/'
if @times_i_got_stepped_on >= 1
@message = "It is now empty"
else
player.inventory << @sword
player.sword += 1
@times_i_got_stepped_on += 1
end
end
end
class Grass < Tile
def initialize
super
@display = '\|/'
end
# def step player
# super
# player.x = @new_x
# player.y = @new_y
# end
end
class Treasure < Tile
def initialize
super
@score = 10
@display = ' § '
@message = "Oh yay, trea$ure!#{7.chr}"
@times_i_got_stepped_on = 0
end
def step player
super
@times_i_got_stepped_on += 1
@display = ' • '
if @times_i_got_stepped_on > 1
@message = "It is now empty"
end
end
end
class Potions < Tile
def initialize
super
@display = '\|/'
@message = "glug glug glug...+1 health!"
@times_i_got_stepped_on = 0
end
def step player
super
@times_i_got_stepped_on += 1
display = '\|/'
if @times_i_got_stepped_on > 1
@message = "There is nothing here."
else
player.health += 1
end
end
end
class Player
attr_accessor :score
attr_accessor :health
attr_accessor :x
attr_accessor :y
attr_accessor :display
attr_accessor :sword
attr_accessor :inventory
def initialize
@score = 0
@health = 3
@x = 1
@y = 1
@display = " @ "
@sword = 0
@inventory = []
end
end
grid = Array.new(10) {Array.new(10) {Tile.new}}
# grid = [
# [0,0,0,0,0,0,3,0,0,0],
# [0,0,0,0,0,2,3,0,0,0],
# [0,0,0,0,0,0,3,0,0,0],
# [0,0,0,0,1,1,1,0,0,0],
# [0,0,0,0,1,0,0,0,2,0],
# [0,0,0,0,1,0,0,0,0,0],
# [0,0,0,0,0,0,0,0,0,0],
# [0,0,0,0,0,0,0,0,0,0],
# [0,0,0,0,0,0,0,0,0,0],
# [0,0,0,0,0,0,0,0,0,0],
# ]
grid_height = grid.length
grid_width = grid[0].length
grid[2][3] = Wall.new
grid[1][3] = Wall.new
grid[2][4] = Grass.new
grid[2][5] = Grass.new
grid[3][4] = Grass.new
grid[3][5] = Sword.new
grid[5][8] = Treasure.new
grid[6][8] = Monster.new
grid[4][5] = Potions.new
# grid[9][2] = Portal.new 3,2
wall = 1
hidden_wall = 3
treasure = 2
floor = 0
# player_x = 4
# player_y = 6
# player_symbol = "@"
# player_score = 0
player = Player.new
player.x = 4
player.y = 6
keepPlaying = true
while(player.health > 0 && keepPlaying == true)
system "cls" #windows
system "clear" #osx
puts "Print with grid.each and row.each"
grid.each_with_index do |row, row_index|
row.each_with_index do |cell, cell_index|
if row_index == player.y && cell_index == player.x # the row we're on == player_y and the column we're on == player_x
print player.display
else
print cell.display
end
end
puts
end
puts "|=========|"
puts " Score: #{player.score}"
puts " Health: #{player.health}"
puts grid[player.y][player.x].message
puts "|=========|"
puts " INVENTORY"
puts player.inventory
#{player.sword}
# Before I move
input = STDIN.getch
if input == "a"
if player.x > 0 && grid[player.y][player.x - 1].solid == false
player.x = player.x - 1
end
end
if input == "s"
if player.y < grid_height - 1 && grid[player.y + 1][player.x].solid == false
player.y = player.y + 1
end
end
if input == "d"
if player.x < grid_width - 1 && grid[player.y][player.x + 1].solid == false
player.x = player.x + 1
end
end
if input == "w"
if player.y > 0 && grid[player.y - 1][player.x].solid == false
player.y = player.y - 1
end
end
if input == "q"
keepPlaying = false
end
#After I have moved
#Step on whatever tile I'm on
grid[player.y][player.x].step player
# if grid[player.y][player.x] == treasure
# # turn treasure back into floor (aka pick up treasure)
# # add to my non-existant score?
# grid[player.y][player.x] = floor
# player.score += 10
# end
end
puts "GAME OVER!!"