forked from Metalab/elle-and-the-spooky-arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
92 lines (81 loc) · 1.73 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
require_relative 'screen'
require_relative 'device'
require_relative 'arrow'
require_relative 'joystick'
require_relative 'audio'
require_relative 'guy'
require_relative 'explosion'
require_relative 'collision'
require_relative 'score'
class Game
def init
@fps = 10
@screen = Screen.new
@device = Device.new(@screen)
@arrow = Arrow.new(@screen)
@guy = Guy.new(@screen)
@audio.init_entities(@arrow, @guy)
@collision = Collision.new(@arrow, @guy)
@explosion = Explosion.new(@screen)
@score = Score.new(@screen)
@frame_count = 0
# 0 - running
# 1 - explosion
# 2 - restart
# 3 - end
# 4 - init
@game_state = 0
end
def init_once
@j1 = Joystick.new
@audio = Audio.new('data/noise.pd')
end
def run
loop do
case @game_state
# running
when 0
if @collision.collide?
@guy.die!
@arrow.die!
@audio.arrow_off
@audio.guy_off
@audio.explosion
@game_state = 1
end
@arrow.update(@frame_count)
@arrow.draw
@audio.update_arrow
@guy.update(@j1.state)
@guy.draw
@audio.update_guy
@audio.update_j1(@j1.state)
@score.increase @frame_count/50
# explosion
when 1
@game_state = 2 if @explosion.finished?
@explosion.draw
# end
when 2
@score.draw
@game_state = 4 if @j1.state == :start
when 3
break
when 4
init
end
@game_state = 3 if @j1.state == :select
@device.flush
@j1.reset
@frame_count += 1
sleep(1/@fps.to_f)
end
end
def self.run
_game = new
_game.init_once
_game.init
_game.run
end
end
Game.run