rong-elements 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rong/elements/ball.rb +9 -2
- data/lib/rong/elements/entity.rb +12 -18
- data/lib/rong/elements/game.rb +21 -12
- data/lib/rong/elements/paddle.rb +12 -6
- metadata +93 -5
data/lib/rong/elements/ball.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'rong/elements/entity'
|
2
|
+
|
1
3
|
module Rong
|
2
4
|
module Elements
|
3
5
|
class Ball
|
@@ -14,7 +16,8 @@ module Rong
|
|
14
16
|
super(start_x, start_y)
|
15
17
|
end
|
16
18
|
|
17
|
-
def reflect_x
|
19
|
+
def reflect_x(x_position=nil)
|
20
|
+
move_to(x_position, y) if x_position
|
18
21
|
self.x_direction *= -1
|
19
22
|
end
|
20
23
|
|
@@ -27,8 +30,12 @@ module Rong
|
|
27
30
|
self.y += SPEED * Math.sin(angle_radians) * y_direction
|
28
31
|
end
|
29
32
|
|
30
|
-
def stop
|
33
|
+
def stop(resest_x=nil, reset_y=nil)
|
31
34
|
self.x_direction = self.y_direction = 0
|
35
|
+
|
36
|
+
reset_x ||= x
|
37
|
+
reset_y ||= y
|
38
|
+
move_to(reset_x, reset_y)
|
32
39
|
end
|
33
40
|
|
34
41
|
def serve_left_from(x, y)
|
data/lib/rong/elements/entity.rb
CHANGED
@@ -33,25 +33,19 @@ module Rong
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def intersects?(other)
|
36
|
-
if
|
37
|
-
|
38
|
-
true
|
39
|
-
elsif other.top > top && other.top < bottom
|
40
|
-
true
|
41
|
-
else
|
42
|
-
false
|
43
|
-
end
|
44
|
-
elsif other.right > left && other.right < right
|
45
|
-
if other.bottom < bottom && other.bottom > top
|
46
|
-
true
|
47
|
-
elsif other.top > top && other.top < bottom
|
48
|
-
true
|
49
|
-
else
|
50
|
-
false
|
51
|
-
end
|
52
|
-
else
|
53
|
-
false
|
36
|
+
if left > other.right
|
37
|
+
return false
|
54
38
|
end
|
39
|
+
if right < other.left
|
40
|
+
return false
|
41
|
+
end
|
42
|
+
if top > other.bottom
|
43
|
+
return false
|
44
|
+
end
|
45
|
+
if bottom < other.top
|
46
|
+
return false
|
47
|
+
end
|
48
|
+
true
|
55
49
|
end
|
56
50
|
end
|
57
51
|
end
|
data/lib/rong/elements/game.rb
CHANGED
@@ -13,7 +13,7 @@ module Rong
|
|
13
13
|
:score_callbacks, :hit_callbacks,
|
14
14
|
:bounce_callbacks, :win_callbacks
|
15
15
|
|
16
|
-
def initialize
|
16
|
+
def initialize(&block)
|
17
17
|
self.left_score = self.right_score = 0
|
18
18
|
|
19
19
|
self.score_callbacks = []
|
@@ -22,8 +22,18 @@ module Rong
|
|
22
22
|
self.win_callbacks = []
|
23
23
|
|
24
24
|
self.ball = Ball.new(WINDOW_CENTER_X, WINDOW_CENTER_Y, 0)
|
25
|
-
self.paddles = [Paddle.new("Player 1", LEFT_PADDLE_X, PADDLE_Y),
|
26
|
-
|
25
|
+
self.paddles = [Paddle.new("Player 1", :left, LEFT_PADDLE_X, PADDLE_Y),
|
26
|
+
Paddle.new("Player 2", :right, RIGHT_PADDLE_X, PADDLE_Y)]
|
27
|
+
yield self if block_given?
|
28
|
+
end
|
29
|
+
|
30
|
+
[:left, :right].each do |side|
|
31
|
+
class_eval <<-MET, __FILE__, __LINE__
|
32
|
+
def #{side}_paddle
|
33
|
+
@#{side}_paddel ||= paddles.find {|p| p.side == :#{side} }
|
34
|
+
@#{side}_paddel
|
35
|
+
end
|
36
|
+
MET
|
27
37
|
end
|
28
38
|
|
29
39
|
def update
|
@@ -76,13 +86,12 @@ module Rong
|
|
76
86
|
end
|
77
87
|
|
78
88
|
def reset_paddles
|
79
|
-
|
80
|
-
|
89
|
+
left_paddle.move_to(LEFT_PADDLE_X, PADDLE_Y)
|
90
|
+
right_paddle.move_to(RIGHT_PADDLE_X, PADDLE_Y)
|
81
91
|
end
|
82
92
|
|
83
93
|
def declare_winner(who)
|
84
|
-
ball.
|
85
|
-
ball.stop
|
94
|
+
ball.stop(WINDOW_CENTER_X, WINDOW_CENTER_Y)
|
86
95
|
self.winner = who
|
87
96
|
win_callbacks.each {|cb| cb.call(who) }
|
88
97
|
end
|
@@ -104,11 +113,11 @@ module Rong
|
|
104
113
|
|
105
114
|
if ball.left < 0 || ball.right > WINDOW_WIDTH
|
106
115
|
if ball.left < 0
|
107
|
-
self.left_score += 1
|
108
|
-
ball.serve_right_from(SERVE_FROM_X, SERVE_FROM_Y)
|
109
|
-
else
|
110
116
|
self.right_score += 1
|
111
117
|
ball.serve_left_from(SERVE_FROM_X, SERVE_FROM_Y)
|
118
|
+
else
|
119
|
+
self.left_score += 1
|
120
|
+
ball.serve_right_from(SERVE_FROM_X, SERVE_FROM_Y)
|
112
121
|
end
|
113
122
|
score
|
114
123
|
reset_paddles
|
@@ -117,9 +126,9 @@ module Rong
|
|
117
126
|
|
118
127
|
def check_win_condition
|
119
128
|
if left_score >= WINNING_SCORE
|
120
|
-
declare_winner(
|
129
|
+
declare_winner(left_paddle)
|
121
130
|
elsif right_score >= WINNING_SCORE
|
122
|
-
declare_winner(
|
131
|
+
declare_winner(right_paddle)
|
123
132
|
end
|
124
133
|
end
|
125
134
|
end
|
data/lib/rong/elements/paddle.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'rong/elements/entity'
|
2
|
+
|
1
3
|
module Rong
|
2
4
|
module Elements
|
3
5
|
class Paddle
|
@@ -6,10 +8,11 @@ module Rong
|
|
6
8
|
SPEED = 10
|
7
9
|
include Entity
|
8
10
|
|
9
|
-
attr_accessor :name
|
11
|
+
attr_accessor :name, :side
|
10
12
|
|
11
|
-
def initialize(name, start_x, start_y)
|
13
|
+
def initialize(name, side, start_x, start_y)
|
12
14
|
self.name = name
|
15
|
+
self.side = side
|
13
16
|
super(start_x, start_y)
|
14
17
|
end
|
15
18
|
|
@@ -21,11 +24,14 @@ module Rong
|
|
21
24
|
self.y += SPEED
|
22
25
|
end
|
23
26
|
|
27
|
+
def left?
|
28
|
+
side == :left
|
29
|
+
end
|
30
|
+
|
24
31
|
def hit(ball)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
32
|
+
ball_x = left? ? right : left
|
33
|
+
ball.reflect_x(ball_x)
|
34
|
+
ball.reflect_y if ball.y < y
|
29
35
|
|
30
36
|
strength = (y - ball.y).abs / (height / 2)
|
31
37
|
ball.angle = 45 * strength
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 2
|
9
|
+
version: 0.2.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Matt Yoho
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-08-20 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -27,11 +27,99 @@ dependencies:
|
|
27
27
|
- !ruby/object:Gem::Version
|
28
28
|
segments:
|
29
29
|
- 2
|
30
|
-
-
|
30
|
+
- 6
|
31
31
|
- 0
|
32
|
-
version: 2.
|
32
|
+
version: 2.6.0
|
33
33
|
type: :development
|
34
34
|
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: guard
|
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
|
+
- 6
|
46
|
+
- 1
|
47
|
+
version: 0.6.1
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: guard-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
|
+
- 0
|
60
|
+
- 4
|
61
|
+
- 2
|
62
|
+
version: 0.4.2
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: rb-fsevent
|
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
|
+
- 4
|
76
|
+
- 3
|
77
|
+
version: 0.4.3
|
78
|
+
type: :development
|
79
|
+
version_requirements: *id004
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: growl
|
82
|
+
prerelease: false
|
83
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ~>
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
segments:
|
89
|
+
- 1
|
90
|
+
- 0
|
91
|
+
- 3
|
92
|
+
version: 1.0.3
|
93
|
+
type: :development
|
94
|
+
version_requirements: *id005
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: growl_notify
|
97
|
+
prerelease: false
|
98
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ~>
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
- 0
|
106
|
+
- 1
|
107
|
+
version: 0.0.1
|
108
|
+
type: :development
|
109
|
+
version_requirements: *id006
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: ruby-debug19
|
112
|
+
prerelease: false
|
113
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
version: "0"
|
121
|
+
type: :development
|
122
|
+
version_requirements: *id007
|
35
123
|
description: Rong-elements contains shared game models and utilities for Rong, a Ruby implementation of Pong that consists of a server module for managing games, a client module for for interfacing with the server, and hopefully a few client implementations for playing some rad Pong, brah.
|
36
124
|
email: mby@mattyoho.com
|
37
125
|
executables: []
|