-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
230 lines (204 loc) · 6 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
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
require 'gosu'
def dist(loc1, loc2)
dist_x = (loc1[0]-loc2[0]) * (loc1[0]-loc2[0])
dist_y = (loc1[1]-loc2[1]) * (loc1[1]-loc2[1])
return Integer.sqrt(dist_x+dist_y)
end
module DIRECTION
RIGHT,LEFT=0,180
end
#=====================================
# Common data for the game, and the
# update code
#======================================
class Game < Gosu::Window
def initialize
super 640, 480, borderless:true, fullscreen:true
self.caption = "Plumbers to the rescue"
@background = Gosu::Image.new("images/wall.png")
@font = Gosu::Font.new(20)
@plumber = ActiveCharacter.new(name="plumber",
health=100,
img=["images/plumber2-1.png","images/plumber2-2.png"],
weapon={name:"vorpal sword", hit:2, range:30},
items=[])
@dragon = ArmedCharacter.new(name="dragon",
health=200,
img=["images/dragon2-1.png","images/dragon2-2.png"],
weapon={name:"fireball", hit:20})
@princess = Character.new(name="princess",
img=["images/princess2.png"])
@plumber.warp(40,40)
@dragon.warp(200,100)
@dragon.volte_face
@princess.warp(300,100)
@ruby = Gosu::Image.new("images/ruby.png")
# @A = Room.new "A", [2,1,0,0]
# @B = Room.new "B", [-1,-1,0,0]
# @C = Room.new "C", [2,3,2,0]
# @D = Room.new "D", [4,4,2,2]
# @E = Room.new "E", [4,5,4,3]
# @F = Room.new "F", [5,5,5,5]
# @rooms = [@A,@B,@C,@D,@E,@F]
@end_game = false
end
def update
if Gosu.button_down? Gosu::KB_Q
self.close!
end
if Gosu.button_down? Gosu::KB_LEFT
@plumber.update_more(-1,0) # turn right
@plumber.update
end
if Gosu.button_down? Gosu::KB_RIGHT
@plumber.update_more(1,0)
@plumber.update
end
if Gosu.button_down? Gosu::KB_DOWN
@plumber.update_more(1,-1) # should preserve the horiz direction
end
if Gosu.button_down? Gosu::KB_UP
@plumber.update_more(1,1) # should preserve the horiz direction
end
@plumber.update
# @dragon.update
# @princess.update
self.collision_detect
end
def collision_detect
ploc = @plumber.get_loc
dloc = @dragon.get_loc
rloc = @princess.get_loc
if Gosu::distance(ploc[0], ploc[1], dloc[0], dloc[1]) < @plumber.attack_range
@plumber.attack(@dragon)
elsif Gosu::distance(ploc[0], ploc[1], rloc[0], rloc[1]) < 30
@end_game=true
elsif Gosu::distance(ploc[0], ploc[1], 320, 200) < 30
@plumber.add "ruby"
@picked_ruby = true
end
end
def draw
if @end_game == false
@background.draw(0,0,0)
@ruby.draw(320,240,0)
if @picked_ruby == true
@font.draw_text("You picked up a ruby!", 10, 240, 2, 1.0, 1.0, Gosu::Color::YELLOW)
@picked_ruby = false
end
@plumber.draw
@princess.draw
@dragon.draw
else
if @plumber.has?("ruby") and @dragon.dead?
mangalam=<<-END
I would have much preferred Smalltalk.
But Ruby is fine.
And they lived happily ever after.
END
@font.draw_text(mangalam, 10, 100, 2, 1.0, 1.0, Gosu::Color::YELLOW)
else
@font.draw_text("Got something for me?", 10, 100, 2, 1.0, 1.0, Gosu::Color::YELLOW)
@end_game = false
end
end
end
end
#=======================================
# common class for all characters in the game
#======================================r
class Character
def initialize(name,img)
@name=name
@x=0
@y=0
@image = Array.new(img.length)
(img.length).times do |i|
@image[i] = Gosu::Image.new(img[i])
end
end
def warp(x,y)
@x,@y=x,y
end
def draw
@image[0].draw(@x,@y,1,2,2)
end
def get_loc
return @x,@y
end
end
#=======================================
# Class for Characters having the ability to wield weapon
#======================================r
class ArmedCharacter < Character
def initialize(name,health,img,weapon)
super name,img
@health=health
@weapon=weapon
@direction=1
end
def volte_face
@direction *= -1
end
def attack(target)
target.reduce_health_by(@weapon[:hit])
end
def dead?
return @health <= 0
end
def reduce_health_by(num)
@health = [@health-num,0].max
end
def draw
if @direction==1
@image[0].draw(@x,@y,1,2,2)
else # flipped image
@image[1].draw(@x,@y,1,2,2)
end unless self.dead? #check if the character is dead
end
end
#=======================================
# Class for Characters having the ability to attack and move around
#======================================r
class ActiveCharacter < ArmedCharacter
def initialize(name,health,img,weapon,items)
super name,health,img,weapon
@items = items
@velocity=0
end
def add item
@items.append item
end
def has? item
return @items.include? item
end
def attack_range
return @weapon[:range]
end
def update
@x = @x + (@direction*@velocity)
end
def update_more(horizontal_direction,vertical_direction)
@velocity = 1
@direction = horizontal_direction
if vertical_direction == 1
then @y = @y - 2 # jumps up
elsif vertical_direction == -1 then @y = @y + 2 # jump down
end
end
end
#=============================
# The rooms are the levels in the game
# Each room has neighbors you can go to
# depending on the key pressed, and the location
# of the character
#
# all other combinations produce no change
#==================================
class Room
def initialize (name, neighbors)
@name = name
@neighbors = neighbours # has up to 8 neighbors, including itself
end
end
Game.new.show