-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.rb
519 lines (472 loc) · 10.9 KB
/
snake.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
require 'yaml'
require 'curses'
include Curses
#DIRECTIONS = RIGHT, DOWN, LEFT, UP
class Snake
attr_accessor :eating
def initialize
@eating = 0
@portal_exist = false
end
def reset(pos_x, pos_y)
@length = 1
@x = pos_x
@y = pos_y
@direction = :right
@portal_exist = false
@body = [ { :x => pos_x, :y => pos_y, :dir => @direction} ]
end
def change_direction(key)
case key
when Key::RIGHT then @direction = :right unless @direction == :left
when Key::DOWN then @direction = :down unless @direction == :up
when Key::LEFT then @direction = :left unless @direction == :right
when Key::UP then @direction = :up unless @direction == :down
end
end
def move
case @direction
when :right then @x += 1
when :down then @y += 1
when :left then @x -= 1
when :up then @y -= 1
end
if collision('#')
setpos(1,5)
message('OUCH!')
if $game.lives > 1 then
collide
else
$game.over
end
elsif collision('*')
message('IT\'S PAINFUL!')
if $game.lives > 1 then
collide
else
$game.over
end
elsif collision('@')
$game.spawn_food
@eating += 1
if @eating % $game.bonus_rate[$game.level] == 0 and not $game.bonus_available
$game.spawn_food("$")
$game.duration = $game.bonus_duration[$game.level]
$game.bonus_available = true
end
if @portal_exist == false and @eating >= $game.to_next_level[$game.level]
$game.spawn_food(232.chr)
@portal_exist = true
end
message('YUMMY!')
$game.score += 1
show
elsif collision('+')
warp
delete_tail
show
elsif collision('$')
message('DELICIOUS!')
$game.score += $game.duration
setpos(11,54)
addstr(' '*(($game.duration/2).to_i + 1))
$game.bonus_available = false
show
elsif collision(232.chr)
if $game.level == 9 then
$game.win
else
$game.level += 1
reset($game.start_pos[$game.level][:x],$game.start_pos[$game.level][:y])
$game.bonus_available = false
$game.set_level
$key = :right
@eating = 0
end
else
delete_tail
show
end
end
def message(string)
setpos(1,5)
addstr(' '*80)
setpos(1,5)
addstr(string)
end
def collision(char)
setpos(@y, @x)
obstacle = inch.chr
if obstacle == char
return true
else
return false
end
end
def collide
$game.lives -= 1
reset($game.start_pos[$game.level][:x],$game.start_pos[$game.level][:y])
$game.bonus_available = false
setpos(11,54)
addstr(' '*(($game.duration/2).to_i + 1))
$game.set_level
$key = :right
end
def warp
@y = 21 if @y == 3
@y = 4 if @y == 22
@x = 51 if @x == 5
@x = 6 if @x == 52
end
def delete_tail
tail = @body[0]
@body.delete_at(0)
setpos(tail[:y],tail[:x])
delch
insch(' ')
end
def show
setpos(@y, @x);
hash = { :x => @x, :y => @y, :dir => @direction}
@body << hash
addstr('*')
refresh
end
end
class Game
attr_accessor :map, :lives, :score, :level, :speed, :duration, :bonus_available, :start_pos
attr_accessor :to_next_level, :bonus_rate, :bonus_duration
def initialize
@lives = 5
@score = 0
@level = 0
@speed = 0.12
@duration = -1
@lose = false
@winning = false
@start_pos = [
{:x => 11, :y => 13},
{:x => 11, :y => 12},
{:x => 8, :y => 12},
{:x => 7, :y => 11},
{:x => 21, :y => 13},
{:x => 21, :y => 12},
{:x => 17, :y => 12},
{:x => 21, :y => 12},
{:x => 11, :y => 12},
{:x => 7, :y => 13},
]
@to_next_level = [30,30,20,30,30,30,30,30,30,20]
@bonus_rate = [4,5,6,7,8,9,10,11,12,13]
@bonus_duration = [15,20,25,30,35,40,45,50,55,60]
@bonus_available = false
end
def main_menu
cls
title = <<heredoc
** ** **
** ** **
** ** **
** ** **
** ** ** *** *****
** ** ** ****** ******
** ** ** ** ** ** *
** ** ** ****** **
** ** ** ***** **
*** *** ** ** * **
******** ** ******* **
**** ** *** **
heredoc
title = title.split("\n")
for i in (0..11)
setpos(3+i,27)
addstr(title[i])
end
setpos(19,36)
addstr("Adventure")
setpos(20,36)
addstr("High Scores")
setpos(21,36)
addstr("Settings")
setpos(22,36)
addstr("Help")
setpos(23,36)
addstr("Exit")
pos = 19
setpos(pos, 34)
addstr('*')
load_highscore
while true
$key = getch
if $key == "Q" then
exit
end
case $key
when 10 then
case pos
when 19 then
$mode = :arcade
new_arcade
break
when 20 then
show_highscore
when 21 then
settings
when 22 then
help
when 23 then
exit
end
when Key::DOWN then
setpos(pos, 34)
addstr(' ')
pos += 1 if pos < 23
when Key::UP then
setpos(pos, 34)
addstr(' ')
pos -= 1 if pos > 19
end
setpos(pos, 34)
addstr('*')
end
end
def settings
cls
setpos(17,36)
addstr('Speed Setting')
setpos(19,36)
addstr('1. Snaily')
setpos(20,36)
addstr('2. Wormy')
setpos(21,36)
addstr('3. Eely')
setpos(22,36)
addstr('4. Snakey')
setpos(23,36)
addstr('5. Lightning Stinger')
pos = 19
setpos(19,34)
addstr('*')
while true
$key = getch
case $key
when 10 then
case pos
when 19 then @speed = 0.33
when 20 then @speed = 0.26
when 21 then @speed = 0.19
when 22 then @speed = 0.12
when 23 then @speed = 0.07
end
main_menu
when Key::UP then
setpos(pos, 34)
addstr(' ')
pos -= 1 if pos > 19
when Key::DOWN then
setpos(pos, 34)
addstr(' ')
pos += 1 if pos < 23
end
setpos(pos, 34)
addstr('*')
end
end
def new_arcade
if $mode == :arcade then
cls
load_map
set_level
player = Snake.new
player.reset(@start_pos[@level][:x],@start_pos[@level][:y])
thread = repeat_every(@speed) do
if @lose == true then
Thread.kill(thread)
exit
end
statistics
@duration -= 1 if @bonus_available
if @duration == 0
setpos(@bonus_y, @bonus_x)
ch = inch.chr
addstr(' ') if ch == '$'
@duration = -1
@bonus_available = false
end
player.change_direction($key)
player.move
end
while true
$key = getch
if $key == "Q" then
exit
end
end
thread.join
end
end
def load_highscore
f = File.open('highscore.yml')
@highscore = YAML.load(f)
f.close
end
def show_highscore
cls
title = <<heredoc
***
* * * * * * *****
* * * * * *
* * *** * * *** * * *** ******** **
* * * * * * * * *** **** * * * * * * *
****** *** * * * * * * *** * * * * **
* * * * * * * * * * * * * * * ***
* * ** * * * * * * * ** * * * * * *
* * *** ** ** *** * * *** * * * ***
heredoc
title = title.split("\n")
for i in (0..8)
setpos(1+i,9)
addstr(title[i])
end
setpos(12,28)
addstr("Score")
setpos(12,34)
addstr("Level")
setpos(12,40)
addstr("Winner")
setpos(12,48)
addstr("Name")
for i in (0..9)
setpos(14+i,29)
addstr(@highscore[i][0].to_s)
setpos(14+i,36)
addstr(@highscore[i][1].to_s)
setpos(14+i,41)
addstr(@highscore[i][2].to_s)
setpos(14+i,48)
addstr(@highscore[i][3])
end
getch
main_menu if not @lose
exit
end
def help
cls
setpos(12,30)
addstr("Created by : irfan3")
setpos(14,26)
addstr("www.github.com/irfan3studio")
getch
main_menu
end
def load_map
f = File.open('map.yml')
@map = YAML.load(f)
f.close
end
def set_level
for i in 0...20
setpos(3+i,5)
addstr(@map[@level][1][i])
end
spawn_food
end
def spawn_food(char = '@')
x = 0; y = 0
success_spawn = false
while not success_spawn
x = Random.rand(6..51)
y = Random.rand(4..21)
setpos(y, x)
ch = inch.chr
success_spawn = true if ch == ' '
end
setpos(y, x)
addstr(char)
if char == '$'
@bonus_x = x
@bonus_y = y
end
end
def statistics
setpos(3, 56)
addstr("Level : "+(@level+1).to_s)
setpos(4, 56)
addstr(' '*20)
setpos(4, 56)
addstr(@map[@level][0])
setpos(8, 56)
addstr("Live : "+@lives.to_s)
setpos(9, 56)
addstr("Score : "+@score.to_s)
if @bonus_available
setpos(11,54)
addstr(' '*((@duration/2).to_i + 1))
setpos(11,54)
addstr(219.chr*(@duration/2).to_i)
end
end
def over
cls
setpos(12,35)
addstr('Game Over')
getch
@lose = true
@winning = false
save_menu if @score >= @highscore[9][0]
show_highscore
end
def save_menu
close_screen
system('cls')
puts "\n"*3
puts " "*25 + "What\'s your name?"
print " "*30
name = gets.chomp
init_screen
noecho
curs_set(0)
doupdate
@highscore.pop
@highscore << [@score, @level+1, @winning, name]
save
end
def save
@highscore.sort!
@highscore.reverse!
f = File.new('highscore.yml', 'w')
f.write(@highscore.to_yaml)
f.close
show_highscore
end
def win
cls
setpos(12,35)
addstr("Congratulations!")
setpos(13,25)
addstr("You have won this game with a score of #{$game.score}")
@lose = true
@winning = true
save_menu if @score >= @highscore[9][0]
end
def cls
clear
end
end
def repeat_every(interval)
Thread.new do
loop do
start_time = Time.now
yield
elapsed = Time.now - start_time
sleep([interval - elapsed, 0].max)
end
end
end
init_screen
noecho
stdscr.keypad(true)
curs_set(0)
$mode = :menu
$game = Game.new
$game.main_menu