-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rb
244 lines (220 loc) · 6.8 KB
/
main.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
require 'gosu'
require 'uri'
require 'net/http'
require_relative 'player'
require_relative 'tower'
require_relative 'tile'
require_relative 'timer'
require_relative 'coin'
require_relative 'background'
require_relative 'bomb'
require_relative 'menu'
NAME = ARGV[0] || "Anonymous"
class Main < Gosu::Window
SCREEN_WIDTH = 1088
SCREEN_HEIGHT = 1024
attr_reader :large_font, :state
def initialize
super(SCREEN_WIDTH, SCREEN_HEIGHT, true)
self.caption = "Ruby Racer"
@tower = Tower.new(self)
@tiles = [Gosu::Image.new(self, "tiles/yellow.png", true),
Gosu::Image.new(self, "tiles/purple.png", true),
Gosu::Image.new(self, "tiles/blue.png", true),
Gosu::Image.new(self, "tiles/green.png", true),
Gosu::Image.new(self, "tiles/red.png", true)]
@coins = Gosu::Image.load_tiles(self, "tiles/coin/cointiles.png", 48, 48, false)
@bomb = Gosu::Image.new(self, "tiles/bomb.png", true)
@explosion = Gosu::Image.load_tiles(self, "tiles/explosion.png", 64, 64, false)
@song = Gosu::Song.new(self, ['music/theme1.ogg', 'music/theme2.ogg', 'music/theme3.ogg'].sample)
@gameover = Gosu::Song.new(self, 'music/gameover.ogg')
@background = Gosu::Image.new(self, "tiles/bg.png", true)
@timer = Timer.new
@player = Player.new(self, @tower.brick)
@large_font = Gosu::Font.new(self, "Arial", screen_height / 6)
@small_font = Gosu::Font.new(self, "Tahoma", screen_height / 16)
@bomb_font = Gosu::Font.new(self, "Tahoma", screen_height / 14)
@state = :menu
@music = true
@sfx = true
@menu = Menu.new(self, @music, @sfx)
@movement = 0
@score = 0
@bomb_count = 1
@coin_count = 0
@floor_num = 0
@game_end = nil
@uri = URI('http://rubyracergame.herokuapp.com/scores')
@name = NAME
end
def update
if @state != :menu
if @player.dead?
@state = :lost
if @game_end == nil
begin
post_score
rescue
end
@game_end = Timer.new
end
@game_end.update
if @game_end.seconds == 10
reset(:menu)
end
end
if @state == :running
@gameover.stop
@music == true && @timer.seconds >= 1 ? @song.play : @song.pause
elsif @state == :lost
if @music == true && @game_end.seconds <= 5
@gameover.play
end
end
if @state != :lost
if button_down?(Gosu::KbLeft)
@player.move_left(@tower.board, @tower.offset)
@movement += 1
if @movement > 35 then @movement = 1 end
elsif button_down?(Gosu::KbRight)
@player.move_right(@tower.board, @tower.offset)
@movement += 1
if @movement > 35 then @movement = 1 end
else
@movement = 0
end
if button_down?(Gosu::KbSpace)
if state == :running
@player.jump(@tower.speed)
end
end
@tower.update(@timer.seconds, @timer.frames)
@timer.update
@player.floor_contact(@tower.board, @tower.offset, SCREEN_HEIGHT)
if @player.collect_coins(@tower.coins, @tower.offset, @sfx)
@score += 2000 * (@tower.speed - 2)
@coin_count += 1
if @coin_count % 5 == 0
@bomb_count += 1
end
end
@player.bombs.reject! { |bomb| Gosu::milliseconds - bomb.time > 280 }
end
if button_down?(Gosu::KbR)
if state != :running
reset(:running)
end
end
if @state == :running
@score += (@player.y + @player.icon.height) * @tower.speed / 200
end
else
menu_action = @menu.update
if menu_action == "start"
@timer = Timer.new
@state = :running
elsif menu_action == "mtoggle"
@music == true ? @music = false : @music = true
elsif menu_action == "sfxtoggle"
@sfx == true ? @sfx = false : @sfx = true
end
@menu.menu_action = nil
end
if button_down?(Gosu::KbEscape)
close
end
end
def draw
if @state != :menu
@player.draw(@movement)
@tower.board.each do |tile|
tile.draw(@tower.offset, @tiles)
end
@tower.coins.each do |coin|
coin.draw(@tower.offset, @coins)
end
@tower.background.each do |bg|
bg.draw(@tower.offset, @background)
end
if @player.bombs.size != 0
@player.bombs.each do |bomb|
bomb.draw(@tower.speed, @explosion)
end
end
draw_rect(0, 0, 1088, 60, 0x77000000)
draw_text(15, -10, "SCORE: #{@score}", @small_font, Gosu::Color::WHITE)
floor_shift = 0
@floor_num = ((@tower.offset - @player.y) / -192).ceil
if @floor_num >= 100 then floor_shift = 28 else floor_shift = 0 end
draw_text(846 - floor_shift, -10, "FLOOR: #{@floor_num}", @small_font, Gosu::Color::WHITE)
@bomb.draw(8, 952, 6)
draw_text(65, 950, "x", @small_font, Gosu::Color::WHITE)
draw_text(97, 945, "#{@bomb_count}", @bomb_font, Gosu::Color.argb(0xFFFF7400))
if @state == :lost
draw_text_centered("Game Over", large_font)
draw_rect(0, 60, 1088, 1074, 0x77000000)
end
else
@menu.draw(@music, @sfx, @tiles, @background, @coins)
end
end
def button_down(id)
if @state == :menu
@menu.button_down(id)
else
if id == Gosu::KbS
@music == true ? @music = false : @music = true
end
if id == Gosu::KbDown && @state == :running
if @bomb_count > 0
@player.drop_bomb(@tower.board, @tower.offset, @sfx)
@bomb_count -= 1
end
end
if id == Gosu::KbM
@song.stop
reset(:menu)
end
end
end
def reset(state)
@timer = Timer.new
@tower = Tower.new(self)
@player = Player.new(self, @tower.brick)
@song = Gosu::Song.new(self, ['music/theme1.ogg', 'music/theme2.ogg', 'music/theme3.ogg'].sample)
@state = state
@movement = 0
@score = 0
@bomb_count = 1
@coin_count = 0
@game_end = nil
@menu = Menu.new(self, @music, @sfx)
end
def draw_rect(x, y, width, height, color)
draw_quad(x, y, color,
x + width, y, color,
x + width, y + height, color,
x, y + height, color, 2)
end
def draw_text(x, y, text, font, color)
font.draw(text, x, y, 3, 1, 1, color)
end
def draw_text_centered(text, font)
x = (screen_width - font.text_width(text)) / 2
y = (screen_height - font.height) / 2
color = Gosu::Color::RED
draw_text(x, y, text, font, color)
end
def screen_width
width
end
def screen_height
height
end
def post_score
Net::HTTP.post_form(@uri, 'score' => @score, 'name' => @name, 'floor' => @floor_num,
'bombs' => @player.bombs_used, 'coins' => @coin_count,
'move' => @player.movespeed)
end
end
Main.new.show