-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
74 lines (65 loc) · 1.47 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
require 'rubygems'
require 'rubygame'
require './lib/shared.rb'
require './lib/pause.rb'
require './lib/about.rb'
require './lib/title.rb'
require './lib/ingame.rb'
Rubygame::TTF.setup
class Game
attr_accessor :screen, :queue, :clcok, :state
def initialize
@screen = Rubygame::Screen.new [640, 480], 0, [Rubygame::HWSURFACE, Rubygame::DOUBLEBUF]
@screen.title = "Pong"
@queue = Rubygame::EventQueue.new
@clock = Rubygame::Clock.new
@clock.target_framerate = 60
# The state has to be changed by the developer
# using the game class, or else the game won't
# start successfully
@state = nil
@state_buffer = nil
end
def run!
loop do
update
draw
@clock.tick
end
end
def update
@queue.peek_each do |ev|
case ev
when Rubygame::QuitEvent
Rubygame.quit
exit
when Rubygame::KeyDownEvent
# This is somewhat of a hack since EventQueue#push seems to
# add on events while your still looping over the queu, meaning
# that if we handle this in the Title state, the QuitEvent will
# be processed before the Game class can see it
if ev.key == Rubygame::K_ESCAPE and @state.class == Title
@queue.push Rubygame::QuitEvent.new
end
end
end
@state.update
if @state_buffer != nil
@state = @state_buffer
@state_buffer = nil
end
end
def draw
@state.draw
end
def switch_state state
if @state != nil
@state_buffer = state
else
@state = state
end
end
end
g = Game.new
g.switch_state Title.new(g)
g.run!