-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.rb
452 lines (399 loc) · 11.3 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#
# game.rb
#
require 'misc.rb'
require 'field.rb'
require 'hito.rb'
require 'other.rb'
require 'singleton'
#debug flags
# $DEBUG_FASTEST = true
# $DEBUG_SHOW_FIELDDATA = true
class Game
include Constants
include Singleton
FONT_GAME = "image/boxfont2.ttf"
FONT_GAME_SIZE = 20
def init(screen,highscores)
@screen = screen
@font=SDL::TTF.open(FONT_GAME, FONT_GAME_SIZE)
@font.style=SDL::TTF::STYLE_NORMAL
#init objects
@field = Field.new
@hito = Hito.new
@gauge = DamageGauge.new
@system= System.new
@effects=Effects.new
@highscores=highscores
@score=0
@state=State.new(:gameover)
@demo=false
end
attr_reader :highscores
attr_reader :effects
Keydata = Struct.new("Keydata",:left,:right)
def self.effects
self.instance.effects
end
def run_demo
@demo=true
run
@demo=false
end
def reset #restart game
@field.reset
@hito.reset
@gauge.reset
@effects.reset
@score=0
@state.reset
@demo_time = 0
SDL::Mixer.playMusic($sound.bgm, -1) if $CONF_MUSIC && !@demo
halt_waves if $CONF_SOUND
end
def run(demo=false)
@gameovertimer = Timer.new(Wait::GAMEOVER)
before=now=SDL.getTicks
key = Keydata.new
#demo
@flashtimer = Timer.new(600)
@flashing = true
@demo=demo
reset
#main loop
while true
draw
@screen.flip
before=now; now=SDL::getTicks; dt=now-before
act(key,dt)
key = check_events
break if key==nil
end
#exit game
SDL::Mixer.haltMusic if $CONF_MUSIC
halt_waves if $CONF_SOUND
return :title
end
def halt_waves
$sound.channels.times do |i|
SDL::Mixer.halt(i) if SDL::Mixer.play?(i)
end
end
private :halt_waves
#-------------------------------------------------------------------
def check_events
key = Keydata.new
key.left = key.right = false
while (event=SDL::Event2.poll)
case event
when SDL::Event2::Quit
return nil
when SDL::Event2::KeyDown
@demo_time = 0
case event.sym
when SDL::Key::ESCAPE
return nil #exit
when SDL::Key::RETURN, SDL::Key::SPACE
if @demo
@demo=false
reset #start game
end
when SDL::Key::LEFT, SDL::Key::H
key.left=true
when SDL::Key::RIGHT,SDL::Key::L
key.right=true
when SDL::Key::D
@demo=true
reset
end
end
end
SDL::Key.scan
key.left = true if SDL::Key.press?(SDL::Key::LEFT) || SDL::Key.press?(SDL::Key::H)
key.right =true if SDL::Key.press?(SDL::Key::RIGHT)|| SDL::Key.press?(SDL::Key::L)
if @demo
#automatic move
key.left = key.right = false
key = auto_decide(@field,@hito)
end
key
end
#-----------------------------------------------------------------
def act(key,dt)
@system.count_fps(dt)
@score += @hito.act(@field,key,dt)
@score += @field.act(@hito,dt)
@gauge.act(@field,@hito,dt)
@effects.act(dt)
if @demo
@flashtimer.wait(dt) do
@flashing = (@flashing ? false : true)
end
else
@demo_time+=dt
if @demo_time > Wait::DEMO_TIME
@demo=true
reset #start demo
end
end
#check gameover
unless @state.gameover?
if @gauge.value<=0 then
@state.on(:gameover)
@hito.gameover
SDL::Mixer.haltMusic if $CONF_MUSIC
$sound.channels.times{|i| SDL::Mixer.halt(i)} if $CONF_SOUND
SDL::Mixer.playChannel(-1,$sound.gameover,0) if $CONF_SOUND
end
else
# Restart game after a while
@gameovertimer.wait(dt) do
unless @demo
if @highscores.size<HIGHSCORES || @highscores[-1].score<@score
name = nameentry()
@highscores<<Score.new(@score, name, Score::VERSION)
@highscores.sort!{|a,b| b.score<=>a.score}
@highscores = @highscores[0,HIGHSCORES]
end
=begin
name = nameentry()
@highscores.size.times do |i|
if @highscores[i].score < @score || @highscores[i].version < Score::VERSION
@highscores[i,0]= Score.new(@score, name, Score::VERSION)
break
end
end
=end
end
@state.off(:gameover)
reset
end
end
end
#------------------------------------------------------------------
def draw
#draw
@screen.fillRect(0,0,640,480,0)
#DEBUG----
if $DEBUG_SHOW_FIELDDATA then
#show Field::@data
for i in 0...(Field::HEI)
for j in 0...(Field::WID)
@font.drawSolidUTF8(@screen,@field[j,i].inspect, Field::RIGHT+32+j*14, 85+i*14, 255,255,255)
end
end
end
#DEBUG----
@font.drawBlendedUTF8(@screen,"FPS:#{@system.fps}", 640-80, 0, 127,127,127)
@font.drawBlendedUTF8(@screen,"SCORE RANKING", Field::RIGHT+32, 2, 255,255,255)
@highscores[0,HIGHSCORES].each_with_index do |score,i|
color = (score.version<Score::VERSION) ? [127,127,127] : [200,255,255] # Use gray for old score
@font.drawBlendedUTF8(@screen,sprintf("%2d: %6d %s",i+1, score.score, score.name), Field::RIGHT+32, 25*(i+1), *color)
end
@font.drawBlendedUTF8(@screen,"SCORE:#{@score}", Field::RIGHT+32, 300 , 255,255,255)
@font.drawBlendedUTF8(@screen,"LIFE", Field::RIGHT+32, 330 , 255,255,255)
@font.drawBlendedUTF8(@screen,"Ver.#{Main::VERSION}", 640-106, 460 , 127,127,127)
@field.draw(@screen)
@hito.draw(@screen)
@gauge.draw(@screen)
@effects.draw(@screen)
if @demo
@font.drawBlendedUTF8(@screen,"HIT SPACE KEY", 160, 200, 230,230,230) if @flashing
end
end
#----------------------------------------------------------------
SHIFT_SYMBOLS = {
"1"=>"!", "2"=>'"', "3"=>"#", "4"=>"$", "5"=>"%", "6"=>"&", "7"=>"'", "8"=>"(", "9"=>")",
"-"=>"=", "^"=>"~", "\\"=>"|","@"=>"`", "["=>"{", ";"=>"+", ":"=>"*", "]"=>"}",
","=>"<", "."=>">", "/"=>"?"
}
def nameentry
name = ""
cursor = 0
cont = true
SDL::Key.enableKeyRepeat(500,40)
@flashtimer = Timer.new(500)
flashing = true
before=now=SDL.getTicks
# ^->' @->` |->\ ;->= (:->: *=>+)
while cont
#key
while (event=SDL::Event2.poll)
case event
when SDL::Event2::Quit
cont = nil
when SDL::Event2::KeyDown
case event.sym
when SDL::Key::RETURN, SDL::Key::ESCAPE
cont = false
when SDL::Key::BACKSPACE
name.chop!
cursor-=1 if cursor>0
when SDL::Key::A .. SDL::Key::Z
if (event.sym==SDL::Key::H) && (event.mod&SDL::Key::MOD_LCTRL!=0)
#C-h
name.chop!
cursor-=1 if cursor>0
break
elsif (event.sym==SDL::Key::U) && (event.mod&SDL::Key::MOD_CTRL!=0)
#C-u
name=""
cursor=0
break
end
if event.mod&SDL::Key::MOD_SHIFT != 0
#A-Z
name << ("A".ord + event.sym-SDL::Key::A).chr
cursor+=1
else
#a-z
name << ("a".ord + event.sym-SDL::Key::A).chr
cursor+=1
end
else
if event.mod&SDL::Key::MOD_SHIFT != 0
val = SHIFT_SYMBOLS[SDL::Key::getKeyName(event.sym)]
unless val==nil
name<<val
cursor+=1
end
else
tmp = SDL::Key::getKeyName(event.sym)
if tmp.size==1
name<<tmp
cursor+=1
end
end
end
end
end
before=now
now=SDL::getTicks
dt=now-before
#draw
@screen.fillRect(0,0,640,480,0)
@font.drawBlendedUTF8(@screen,"SCORE RANKING", Field::RIGHT+32, 2, 255,255,255)
@highscores[0,HIGHSCORES].each_with_index do |score,i|
color = (score.version<Score::VERSION) ? [127,127,127] : [200,255,255] # Use gray for old score
@font.drawBlendedUTF8(@screen,sprintf("%2d: %6d %s",i+1, score.score, score.name), Field::RIGHT+32, 25*(i+1), *color)
end
@font.drawBlendedUTF8(@screen,"SCORE:#{@score}", Field::RIGHT+32, 300 , 255,255,255)
@font.drawBlendedUTF8(@screen,"LIFE", Field::RIGHT+32, 330 , 255,255,255)
@flashtimer.wait(dt){
flashing = (flashing ? false : true)
}
if flashing
@font.drawBlendedUTF8(@screen,"_", Field::RIGHT+82+cursor*10, 360 , 255,255,255)
end
@font.drawBlendedUTF8(@screen,"NAME:#{name}", Field::RIGHT+32, 360 , 255,255,255)
@field.draw(@screen)
@screen.flip
end
SDL::Key.disableKeyRepeat
if cont==nil
return nil
else
return name
end
end
private :nameentry
#-------------------------------------------------------------------
# AI (for demo mode)
#-------------------------------------------------------------------
def auto_decide(field,hito)
key = Keydata.new
key.left = key.right = false
# Do not move when floating
if @field.can_pass?(@hito.x, @hito.y+1)
return key
end
# Go left when on the right end
x = @hito.x
until @field.can_pass?(x, @hito.y+1)
x+=1; break if x>=Field::WID
end
if x>=Field::WID then
key.left=true
return key
end
r=x
# Go right when on the left end
x = @hito.x
until @field.can_pass?(x, @hito.y+1)
x-=1; break if x<0
end
if x<0 then
key.right=true
return key
end
l=x
# Otherwise:
# Check what happens falling from the right end
y = @hito.y
until @field[r,y] != Chara::EMPTY
y+=1; break if y>=Field::HEI
end
if y==Field::HEI
rstat = Chara::EMPTY
else
rstat = @field[r,y]
end
yr=y
# Check what happens falling from the left end
y = @hito.y
until @field[l,y] != Chara::EMPTY
y+=1; break if y>=Field::HEI
end
if y==Field::HEI
lstat = Chara::EMPTY
else
lstat = @field[l,y]
end
yl=y
if @field[@hito.x, @hito.y+1]==Chara::HARI
# Go nearer end when on a needle
if (r-@hito.x) < (@hito.x-l)
key.right=true
else
key.left=true
end
else
# Go opposite side from needle
if lstat==Chara::HARI
key.right=true
elsif rstat==Chara::HARI
key.left=true
else
# Go nearer end when both sides are safe
if (r-@hito.x) < (@hito.x-l)
key.right=true
elsif (r-@hito.x) > (@hito.x-l)
key.left=true
else
if rand(2)==0
key.right=true
else
key.left=true
end
end
end
end
key
end
#----------------------------------------------------------------
end
#test
if __FILE__ == $0 then
require 'sdl'
VER="??"
#init view
SDL.init(SDL::INIT_VIDEO)
screen = SDL::setVideoMode(640,480,16,SDL::SWSURFACE)
SDL::WM.setCaption("DOWN!!v#{VER} on Ruby/SDL","DOWN!!v#{VER}")
#init font
SDL::TTF.init
font=SDL::TTF.open('font.ttf',20)
font.style=SDL::TTF::STYLE_NORMAL
#execute
Game.new(screen,font).run
end