Class: Rong::Client::Window

Inherits:
Gosu::Window
  • Object
show all
Includes:
Elements::WindowConstants
Defined in:
lib/rong/client/window.rb

Defined Under Namespace

Modules: ZIndex

Constant Summary collapse

STRIPE_LEFT =
WINDOW_CENTER_X - 2
STRIPE_RIGHT =
WINDOW_CENTER_X + 2
SCORE_FONT =
'Synchro LET'
SCORE_HEIGHT =
90
SCORE_X_OFFSET =
40
SCORE_Y_OFFSET =
-10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWindow

Returns a new instance of Window.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rong/client/window.rb', line 28

def initialize
  super(WINDOW_WIDTH, WINDOW_HEIGHT, false)
  self.caption = "Rong: Ruby Pong"

  self.game = Rong::Elements::Game.new do |g|
    g.on_score  {|left, right| update_scores(left, right) }
    g.on_hit    { paddle_blip.play }
    g.on_bounce { wall_blip.play }
    g.on_win    {|winner| create_winner_banner(winner) }
  end

  self.paddle_boxes = game.paddles.map {|p| DrawableElement.new(self, p) }
  self.ball_box     = DrawableElement.new(self, game.ball)

  self.left_score_image  = Gosu::Image.from_text(self, "00", SCORE_FONT, SCORE_HEIGHT)
  self.right_score_image = Gosu::Image.from_text(self, "00", SCORE_FONT, SCORE_HEIGHT)

  load_samples
end

Instance Attribute Details

#ball_boxObject

Returns the value of attribute ball_box.



25
26
27
# File 'lib/rong/client/window.rb', line 25

def ball_box
  @ball_box
end

#gameObject

Returns the value of attribute game.



25
26
27
# File 'lib/rong/client/window.rb', line 25

def game
  @game
end

#left_score_imageObject

Returns the value of attribute left_score_image.



25
26
27
# File 'lib/rong/client/window.rb', line 25

def left_score_image
  @left_score_image
end

#paddle_blipObject

Returns the value of attribute paddle_blip.



25
26
27
# File 'lib/rong/client/window.rb', line 25

def paddle_blip
  @paddle_blip
end

#paddle_boxesObject

Returns the value of attribute paddle_boxes.



25
26
27
# File 'lib/rong/client/window.rb', line 25

def paddle_boxes
  @paddle_boxes
end

#right_score_imageObject

Returns the value of attribute right_score_image.



25
26
27
# File 'lib/rong/client/window.rb', line 25

def right_score_image
  @right_score_image
end

#wall_blipObject

Returns the value of attribute wall_blip.



25
26
27
# File 'lib/rong/client/window.rb', line 25

def wall_blip
  @wall_blip
end

#winner_imageObject

Returns the value of attribute winner_image.



25
26
27
# File 'lib/rong/client/window.rb', line 25

def winner_image
  @winner_image
end

Instance Method Details

#asset_sample(file_name) ⇒ Object



53
54
55
# File 'lib/rong/client/window.rb', line 53

def asset_sample(file_name)
  File.join(File.dirname(__FILE__), '..', '..', '..', 'assets', 'samples', file_name)
end

#button_down(id) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/rong/client/window.rb', line 119

def button_down(id)
  if id == Gosu::Button::KbEscape
    close
  elsif id == Gosu::Button::KbSpace && game.winner
    self.winner_image = nil
    game.reset
    update_scores(0, 0)
  end
end

#create_winner_banner(winner) ⇒ Object



100
101
102
# File 'lib/rong/client/window.rb', line 100

def create_winner_banner(winner)
  self.winner_image = Gosu::Image.from_text(self, "#{winner.name} wins!", SCORE_FONT, SCORE_HEIGHT)
end

#drawObject



73
74
75
76
77
78
# File 'lib/rong/client/window.rb', line 73

def draw
  draw_background
  paddle_boxes.each {|p| p.draw }
  ball_box.draw
  draw_winner_banner if winner?
end

#draw_backgroundObject



80
81
82
83
# File 'lib/rong/client/window.rb', line 80

def draw_background
  draw_stripe
  draw_scores
end

#draw_scoresObject



95
96
97
98
# File 'lib/rong/client/window.rb', line 95

def draw_scores
  left_score_image.draw(WINDOW_CENTER_X - (SCORE_X_OFFSET + left_score_image.width), SCORE_Y_OFFSET, Window::ZIndex::BACKGROUND)
  right_score_image.draw(WINDOW_CENTER_X + SCORE_X_OFFSET, SCORE_Y_OFFSET, Window::ZIndex::BACKGROUND)
end

#draw_stripeObject



85
86
87
88
89
90
91
92
93
# File 'lib/rong/client/window.rb', line 85

def draw_stripe
  (0..WINDOW_HEIGHT).step(20) do |y|
    draw_quad(STRIPE_LEFT,  y + 10, Gosu::Color::WHITE,
              STRIPE_LEFT,  y, Gosu::Color::WHITE,
              STRIPE_RIGHT, y, Gosu::Color::WHITE,
              STRIPE_RIGHT, y + 10, Gosu::Color::WHITE,
              Window::ZIndex::BACKGROUND)
  end
end

#draw_winner_bannerObject



104
105
106
# File 'lib/rong/client/window.rb', line 104

def draw_winner_banner
  winner_image.draw(WINDOW_CENTER_X - (winner_image.width / 2), WINDOW_CENTER_Y, Window::ZIndex::HUD)
end

#load_samplesObject



48
49
50
51
# File 'lib/rong/client/window.rb', line 48

def load_samples
  self.paddle_blip = Gosu::Sample.new(self, asset_sample('pongblipG_5.wav'))
  self.wall_blip   = Gosu::Sample.new(self, asset_sample('pongblipF5.wav'))
end

#updateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rong/client/window.rb', line 57

def update
  if button_down?(Gosu::Button::KbA)
    game.paddles.first.move_up
  end
  if button_down?(Gosu::Button::KbZ)
    game.paddles.first.move_down
  end
  if button_down?(Gosu::Button::KbK)
    game.paddles.last.move_up
  end
  if button_down?(Gosu::Button::KbM)
    game.paddles.last.move_down
  end
  game.update
end

#update_scores(left_score, right_score) ⇒ Object



112
113
114
115
116
117
# File 'lib/rong/client/window.rb', line 112

def update_scores(left_score, right_score)
  left_score_text  = left_score  > 9 ? left_score.to_s  : "0#{left_score}"
  right_score_text = right_score > 9 ? right_score.to_s : "0#{right_score}"
  self.left_score_image  = Gosu::Image.from_text(self, left_score_text,  SCORE_FONT, SCORE_HEIGHT)
  self.right_score_image = Gosu::Image.from_text(self, right_score_text, SCORE_FONT, SCORE_HEIGHT)
end

#winner?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/rong/client/window.rb', line 108

def winner?
  winner_image
end