rong-client 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ require 'rong/client/window'
@@ -0,0 +1,22 @@
1
+ module Rong
2
+ module Client
3
+ class DrawableElement
4
+ attr_reader :color, :element, :window, :z_index
5
+
6
+ def initialize(window, element, color = Gosu::Color::WHITE, z_index = Window::ZIndex::FOREGROUND)
7
+ @window = window
8
+ @element = element
9
+ @color = color
10
+ @z_index = z_index
11
+ end
12
+
13
+ def draw
14
+ window.draw_quad(element.left, element.top, color,
15
+ element.left, element.bottom, color,
16
+ element.right, element.bottom, color,
17
+ element.right, element.top, color,
18
+ z_index)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,121 @@
1
+ require 'gosu'
2
+ require 'rong/elements'
3
+
4
+ require 'rong/client/drawable_element'
5
+
6
+ module Rong
7
+ module Client
8
+ class Window < Gosu::Window
9
+ include Rong::Elements::WindowConstants
10
+
11
+ module ZIndex
12
+ BACKGROUND = 0
13
+ FOREGROUND = 1
14
+ HUD = 2
15
+ end
16
+
17
+ STRIPE_LEFT = WINDOW_CENTER_X - 2
18
+ STRIPE_RIGHT = WINDOW_CENTER_X + 2
19
+
20
+ SCORE_FONT = 'Synchro LET'
21
+ SCORE_HEIGHT = 90
22
+ SCORE_X_OFFSET = 40
23
+ SCORE_Y_OFFSET = -10
24
+
25
+ attr_accessor :game, :left_score_image, :right_score_image, :winner_image,
26
+ :paddle_boxes, :ball_box, :paddle_blip, :wall_blip
27
+
28
+ def initialize
29
+ super(WINDOW_WIDTH, WINDOW_HEIGHT, false)
30
+ self.caption = "Rong: Ruby Pong"
31
+
32
+ self.game = Rong::Elements::Game.new
33
+ game.on_score {|left, right| update_scores(left, right) }
34
+ game.on_hit { paddle_blip.play(1,1) }
35
+ game.on_bounce { wall_blip.play(1,1) }
36
+ game.on_win do |winner|
37
+ self.winner_image = Gosu::Image.from_text(self, "#{winner.name} wins!", SCORE_FONT, SCORE_HEIGHT)
38
+ end
39
+
40
+ self.paddle_boxes = game.paddles.map {|p| DrawableElement.new(self, p) }
41
+ self.ball_box = DrawableElement.new(self, game.ball)
42
+
43
+ self.left_score_image = Gosu::Image.from_text(self, "00", SCORE_FONT, SCORE_HEIGHT)
44
+ self.right_score_image = Gosu::Image.from_text(self, "00", SCORE_FONT, SCORE_HEIGHT)
45
+
46
+ load_samples
47
+ end
48
+
49
+ def load_samples
50
+ self.paddle_blip = Gosu::Sample.new(self, asset_sample('pongblipG_5.wav'))
51
+ self.wall_blip = Gosu::Sample.new(self, asset_sample('pongblipF5.wav'))
52
+ end
53
+
54
+ def asset_sample(file_name)
55
+ File.join(File.dirname(__FILE__), '..', '..', '..', 'assets', 'samples', file_name)
56
+ end
57
+
58
+ def update
59
+ if button_down?(Gosu::Button::KbA)
60
+ game.paddles.first.move_up
61
+ end
62
+ if button_down?(Gosu::Button::KbZ)
63
+ game.paddles.first.move_down
64
+ end
65
+ if button_down?(Gosu::Button::KbK)
66
+ game.paddles.last.move_up
67
+ end
68
+ if button_down?(Gosu::Button::KbM)
69
+ game.paddles.last.move_down
70
+ end
71
+ game.update
72
+ end
73
+
74
+ def draw
75
+ draw_background
76
+ paddle_boxes.each {|p| p.draw }
77
+ ball_box.draw
78
+ if winner_image
79
+ winner_image.draw(WINDOW_CENTER_X - (winner_image.width / 2), WINDOW_CENTER_Y, Window::ZIndex::HUD)
80
+ end
81
+ end
82
+
83
+ def draw_background
84
+ draw_stripe
85
+ draw_scores
86
+ end
87
+
88
+ def draw_stripe
89
+ (0..WINDOW_HEIGHT).step(20) do |y|
90
+ draw_quad(STRIPE_LEFT, y + 10, Gosu::Color::WHITE,
91
+ STRIPE_LEFT, y, Gosu::Color::WHITE,
92
+ STRIPE_RIGHT, y, Gosu::Color::WHITE,
93
+ STRIPE_RIGHT, y + 10, Gosu::Color::WHITE,
94
+ Window::ZIndex::BACKGROUND)
95
+ end
96
+ end
97
+
98
+ def draw_scores
99
+ left_score_image.draw(WINDOW_CENTER_X - (SCORE_X_OFFSET + left_score_image.width), SCORE_Y_OFFSET, Window::ZIndex::BACKGROUND)
100
+ right_score_image.draw(WINDOW_CENTER_X + SCORE_X_OFFSET, SCORE_Y_OFFSET, Window::ZIndex::BACKGROUND)
101
+ end
102
+
103
+ def update_scores(left_score, right_score)
104
+ left_score_text = left_score > 9 ? left_score.to_s : "0#{left_score}"
105
+ right_score_text = right_score > 9 ? right_score.to_s : "0#{right_score}"
106
+ self.left_score_image = Gosu::Image.from_text(self, left_score_text, SCORE_FONT, SCORE_HEIGHT)
107
+ self.right_score_image = Gosu::Image.from_text(self, right_score_text, SCORE_FONT, SCORE_HEIGHT)
108
+ end
109
+
110
+ def button_down(id)
111
+ if id == Gosu::Button::KbEscape
112
+ close
113
+ elsif id == Gosu::Button::KbSpace && game.winner
114
+ self.winner_image = nil
115
+ game.reset
116
+ update_scores(0, 0)
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rong-client
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 1
9
+ version: 0.2.1
10
+ platform: ruby
11
+ authors:
12
+ - Matt Yoho
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-07 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: gosu
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 7
31
+ - 28
32
+ version: 0.7.28
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rong-elements
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ - 2
46
+ - 1
47
+ version: 0.2.1
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rspec
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 2
60
+ - 5
61
+ - 0
62
+ version: 2.5.0
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: ruby-debug19
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ type: :development
77
+ version_requirements: *id004
78
+ description: Rong-client is the catch-all Rong client package, and in the future will hopefully be replaced with platform-specifc implementations.
79
+ email: mby@mattyoho.com
80
+ executables: []
81
+
82
+ extensions: []
83
+
84
+ extra_rdoc_files: []
85
+
86
+ files:
87
+ - lib/rong/client/drawable_element.rb
88
+ - lib/rong/client/window.rb
89
+ - lib/rong/client.rb
90
+ has_rdoc: true
91
+ homepage: http://github.com/mattyoho/rong
92
+ licenses: []
93
+
94
+ post_install_message:
95
+ rdoc_options: []
96
+
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ segments:
105
+ - 1
106
+ - 8
107
+ - 7
108
+ version: 1.8.7
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ segments:
115
+ - 1
116
+ - 3
117
+ - 7
118
+ version: 1.3.7
119
+ requirements: []
120
+
121
+ rubyforge_project:
122
+ rubygems_version: 1.3.7
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Client library for a Ruby-based Pong server implementation
126
+ test_files: []
127
+