Class: RubyGame::Game

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/ruby_game/game.rb

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



5
6
7
8
9
10
# File 'lib/ruby_game/game.rb', line 5

def initialize
  super(640, 480, false)
  self.caption = "Ruby Game"
  @background_image = Gosu::Image.new(self, File.join(IMAGES_PATH, 'background.png'), true)
  @font = Gosu::Font.new(self, Gosu::default_font_name, 60)
end

Instance Method Details

#button_down(id) ⇒ Object



35
36
37
38
# File 'lib/ruby_game/game.rb', line 35

def button_down(id)
  self.close if id == Gosu::Button::KbEscape
  self.restart! if id == Gosu::Button::KbR
end

#drawObject



28
29
30
31
32
33
# File 'lib/ruby_game/game.rb', line 28

def draw
  @background_image.draw(0, 0, 0)
  @font.draw("You won!", 200, 240, 2, 1.0, 1.0, 0xffffff00) if self.won?
  @font.draw("Game Over", 175, 240, 2, 1.0, 1.0, 0xffffff00) if self.gameover?
  ([@ruby, @player] + @monsters).each {|object| object.draw}
end

#gameover!Object



65
66
67
# File 'lib/ruby_game/game.rb', line 65

def gameover!
  @state = :gameover
end

#monsters(monsters) ⇒ Object



40
41
42
# File 'lib/ruby_game/game.rb', line 40

def monsters(monsters)
  @monsters += monsters
end

#start!(&block) ⇒ Object Also known as: restart!



50
51
52
53
54
55
56
57
58
# File 'lib/ruby_game/game.rb', line 50

def start!(&block)
  @monsters = []
  @init = block if block_given?
  self.instance_eval(&@init)
  ([@ruby, @player] + @monsters).each {|object| object.init_image(self)}
  ([@player] + @monsters).each {|object| object.init_limits(width, height, 15, 40)}
  @state = :run
  self.show if block_given?
end

#updateObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_game/game.rb', line 12

def update
  if self.run?
    @player.move_left if button_down? Gosu::Button::KbLeft
    @player.move_right if button_down? Gosu::Button::KbRight
    @player.move_up if button_down? Gosu::Button::KbUp
    @player.move_down if button_down? Gosu::Button::KbDown

    @monsters.each do |monster|
      monster.execute(@player)
      self.gameover! if monster.touch?(@player)
    end

    self.won! if @player.touch?(@ruby)
  end
end

#won!Object



61
62
63
# File 'lib/ruby_game/game.rb', line 61

def won!
  @state = :won
end