-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
149 lines (111 loc) · 3.14 KB
/
game.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
class PlayGame
def initialize
require "colorize"
create_map
clear_screen
print_map
get_action
end
def create_map
@grass = 0
@water = 1
@snow = 2
@the_map = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,2,2],
[0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,2,2,2],
[0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,2,2],
[0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,2,2,2],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,2,2,2],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2]
]
@you_are_here_row = 0
@you_are_here_col = 5
@the_map.length.times do |i|
@the_map[i].length.times do |j|
number = @the_map[i][j]
if number == 0
terrain = "░".green
elsif number == 1
terrain = "░".blue
elsif number == 2
terrain = "░".white
else
terrain = "?"
end
@the_map[i][j] = terrain
end
end
end
####################
def clear_screen
system "clear"
end
def print_and_flush(str)
print str
$stdout.flush
end
####################
def get_action
print_and_flush "\nWhat to do next? ".green
@action = gets.chomp
do_action
end
def print_map
paint = "╔".blue
(@the_map[0].length + 2).times do |i|
paint += "═".blue
end
paint += "╗\n".blue
@the_map.length.times do |i|
paint += "║ ".blue
@the_map[i].length.times do |j|
if @you_are_here_row == i && @you_are_here_col == j
tile = "☓".red
else
tile = @the_map[i][j]
end
paint += tile
end
paint += " ║\n".blue
end
paint += "╚".blue
(@the_map[0].length + 2).times do |i|
paint += "═".blue
end
paint += "╝".blue
puts paint
end
def do_action
if @action == "quit"
clear_screen
print_map
puts "\nGoodbye.\n".yellow
elsif @action == "help"
clear_screen
print_map
give_help
get_action
elsif @action == "jump"
clear_screen
@you_are_here_col += 1
if @you_are_here_col >= @the_map[0].length
@you_are_here_col = 0
end
print_map
puts "\nJump!".yellow
get_action
else
clear_screen
print_map
puts "\nAction '#{@action}' not found.".red
give_help
get_action
end
end
def give_help
puts ["\nActions:","help","jump","quit"].join("\n - ").yellow
end
end
PlayGame.new
# MVS's project = https://github.com/smellsblue/holmes