-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
416 lines (316 loc) · 6.78 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
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
#!/usr/bin/env ruby
require 'optparse'
require 'sdl'
require_relative 'word'
require_relative 'sound'
class Game
TITLE = "Neko-type"
SCREEN_X = 640
SCREEN_Y = 480
FPS = 30
TIME_POS_X = SCREEN_X - 105
#--------------------------
TIMELIMIT = FPS * 60
TARGET_SCORE = 10000
#TIMELIMIT = 1
#TARGET_SCORE = 0
@@isFullscreen = false
def initialize()
@objs = []
@age = 0 # 現在のステートになってからのフレーム数
@lastTime = 0
@screen = nil
$sound = Sound.new
@score = 0
@keys = []
@newState = nil
@gameFrame = 0
@clearImageIndex = (rand() * 3).to_i
@currentWord = nil
end
def init()
parseArgs
f = open("words")
$dict = f.readlines()
f.close
puts "Loaded " + $dict.length.to_s + " words."
puts "init"
initSDL()
$image_title = SDL::Surface.loadBMP("image/title.bmp")
$sound.loadSounds
end
def initGame()
@objs = []
@score = 0
@gameFrame = 0
@scoreDrawer = ScoreDrawer.new()
@createWait = 45
@isNearTimelimit = false
@currentWord = nil
@clearImageIndex += 1
if @clearImageIndex >= 3
@clearImageIndex = 0
end
$image_clear = SDL::Surface.loadBMP(sprintf("image/clear%02d.bmp", @clearImageIndex))
end
def start()
puts "start"
@gameOver = GameOver.new()
@titleDrawer = TitleDrawer.new()
@state = :title
mainLoop()
end
def addScore(point)
@score += point
@scoreDrawer.setScore(@score, point)
end
def addGameObj(gameObj)
@objs.push(gameObj)
end
def getKeys()
return @keys
end
def setCurrentWord(word)
if @currentWord == nil
@currentWord = word
end
end
def lostCurrentWord()
puts "lostCurrentWord"
@currentWord = nil
end
def isSetCurrentWord()
return @currentWord != nil
end
def isCurrentTarget(word)
return @currentWord == word
end
def isNearTimelimit()
return @isNearTimelimit
end
private
def initSDL
puts "initSDL"
SDL.init( SDL::INIT_VIDEO | SDL::INIT_AUDIO )
if @@isFullscreen
@screen = SDL::setVideoMode(Game::SCREEN_X, Game::SCREEN_Y, 16, SDL::SWSURFACE|SDL::DOUBLEBUF | SDL::FULLSCREEN)
else
@screen = SDL::setVideoMode(Game::SCREEN_X, Game::SCREEN_Y, 16, SDL::SWSURFACE|SDL::DOUBLEBUF)
end
$screen = @screen
SDL::Mouse.hide
SDL::WM::setCaption TITLE, TITLE
SDL::Mixer.open(22010,SDL::Mixer::FORMAT_S8,1,1024) #22kHz,8bit,monoral
#init font
SDL::TTF.init
$font = SDL::TTF.open('font/boxfont2.ttf',20)
$font2 = SDL::TTF.open('font/boxfont2.ttf',40)
$font_word = SDL::TTF.open('font/tahoma.ttf',20)
#@kanji = SDL::Kanji.open("english.bdf", 14)
#if not @kanji
#puts "failed to load kanji"
#exit
#end
end
def action()
keys = getKeys()
if @newState
@state = @newState
@newState = nil
@age = 0
# ステート開始
case @state
when :game
$sound.playBGM()
initGame()
when :gameover
@gameOver.init()
$sound.haltMusic()
$sound.playSound($sound_gameover)
when :clear
$sound.haltMusic()
$sound.playSound($sound_clear)
end
end
case @state
when :title
@titleDrawer.action()
if keys.length > 0
@newState = :game
end
when :game
if @gameFrame == Game::TIMELIMIT
if @score < TARGET_SCORE
@newState = :gameover
else
@newState = :clear
end
return
end
if @gameFrame == Game::TIMELIMIT - Game::FPS * 10
$sound.playSound($sound_limit)
@isNearTimelimit = true
end
@objs.delete_if {|x| x.dead? }
@objs.each do |x|
x.action()
end
createNewWordRandom()
@scoreDrawer.action()
@gameFrame += 1
when :gameover
@gameOver.action()
@scoreDrawer.action()
if @age > FPS * 3 and keys.length > 0
@newState = :title
end
when :clear
@scoreDrawer.action()
if @age == 245
$sound.playSound($sound_clearcat)
end
if @age > FPS * 8 and keys.length > 0
@newState = :title
end
end
@age += 1
end
def draw()
time = (TIMELIMIT - @gameFrame - 1) / FPS + 1
case @state
when :title
@screen.fillRect(0, 0, SCREEN_X, SCREEN_Y,0)
@titleDrawer.draw()
@screen.flip
when :game
@screen.fillRect(0, 0, SCREEN_X, SCREEN_Y,0)
#
#puts "draw kanji"
#@kanji.put(@screen, "hoge", 100, 100, 255, 255, 0)
#return
@objs.each do |x|
x.draw()
end
@scoreDrawer.draw(true)
drawTime(time)
@screen.flip
when :gameover
@screen.fillRect(0, 0, SCREEN_X, SCREEN_Y,0)
@gameOver.draw()
drawTime(0)
@scoreDrawer.draw(true)
@screen.flip
when :clear
if @age > 250
@screen.fillRect(0, 0, SCREEN_X, SCREEN_Y,0)
$screen.put($image_clear, 190, 130)
$font.drawSolidUTF8(@screen, "Congraturations!", 230, 350, 255, 200, 150);
$font.drawSolidUTF8(@screen, "Time: " + sprintf("%3d", time), TIME_POS_X, 0, 255, 255, 255);
drawTime(0)
@scoreDrawer.draw(true)
@screen.flip
end
end
end
def mainLoop()
while true
time = SDL.getTicks
if time - @lastTime < 1000 / Game::FPS
SDL.delay(1000 / Game::FPS - (time - @lastTime))
end
@lastTime = SDL.getTicks
@buttonState = updateKeys()
if !@buttonState
exit
end
action()
draw()
end
end
def updateKeys()
@keys = []
while (event=SDL::Event2.poll)
case event
when SDL::Event2::Quit
return nil
when SDL::Event2::KeyDown
case event.sym
when SDL::Key::ESCAPE
return nil #exit
end
begin
# Ruby/SDL1ではevent.symが文字列だったが、2では変わっているようだ
key = event.sym.to_i.chr
p key
@keys.push(key)
rescue RangeError
puts "RangeError"
end
end
end
return true
end
def createNewWordRandom()
if @createWait <= 0
doCreateNewWord
intervals = [
[10, 3],
[20, 2.4],
[30, 1.9],
[40, 1.7],
[50, 1.5],
[60, 1]
]
interval = FPS
intervals.each do |x|
if (@age/FPS) < x[0]
interval = (x[1] * FPS).to_i
break
end
end
@createWait = interval
end
@createWait -= 1
end
=begin
def doCreateNewWord
puts "doCreateNewWord"
chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
str = ""
length = (rand() * 10).to_i
length.times do
str += chars[(rand() * chars.length).to_i]
end
word = Word.new(str)
@objs.push(word)
#$sound.playFoot()
end
=end
def doCreateNewWord
r = (rand() * $dict.length).to_i
str = $dict[r].chomp()
#str = "wixiyizi"
word = Word.new(str)
@objs.push(word)
end
def drawTime(time)
$font.drawSolidUTF8(@screen, "Time: " + sprintf("%3d", time), TIME_POS_X, 0, 255, 255, 255);
end
def parseArgs
opts = ARGV.getopts("fh", "help")
if opts["help"] || opts["h"]
puts TITLE
puts "usage: ruby main.rb [options]"
puts "options:"
puts " -f, --fullscreen"
puts " -h, --help"
exit
end
if opts["f"]
@@isFullscreen = true
end
end
end
$game = Game.new
$game.init
$game.start()