Skip to content

Commit 706b743

Browse files
committed
paddle bounce complete
1 parent a7d3e38 commit 706b743

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
func bounce(ball):
2-
ball.set_direction(ball.get_direction() * Vector2(-1, 1))
2+
var new_y = rand_range(-.5, .5)
3+
var new_x = ball.get_direction().x * -1
4+
ball.set_direction(Vector2(new_x, new_y))

TddAndPong/Episode3/test/unit/test_paddle.gd

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,45 @@ func test_bounce_inverts_x():
1212

1313
stub(ball, 'get_direction').to_return(Vector2(1, 1))
1414
paddle.bounce(ball)
15-
assert_called(ball, 'set_direction', [Vector2(-1, 1)])
15+
var new_x = get_call_parameters(ball, 'set_direction')[0].x
16+
assert_eq(new_x, -1.0)
17+
1618

1719
func test_bounce_inverts_x_other_direction():
1820
var ball = double(Ball).new()
1921
var paddle = Paddle.new()
2022

2123
stub(ball, 'get_direction').to_return(Vector2(-1, 1))
2224
paddle.bounce(ball)
23-
assert_called(ball, 'set_direction', [Vector2(1, 1)])
25+
var new_x = get_call_parameters(ball, 'set_direction')[0].x
26+
assert_eq(new_x, 1.0)
2427

2528
func test_bounce_changes_y_randomly():
2629
var paddle = Paddle.new()
2730
var ball = double(Ball).new()
2831
stub(ball, 'get_direction').to_return(Vector2(1, 1))
29-
32+
stub(ball, 'set_direction').to_do_nothing()
33+
34+
for i in range(1000):
35+
paddle.bounce(ball)
36+
37+
var last_y = get_call_parameters(ball, 'set_direction', 0)[0].y
38+
var current_y = 0
39+
var num_equal = 0
40+
var min_y = 3
41+
var max_y = -3
42+
for i in range(1, 1000):
43+
current_y = get_call_parameters(ball, 'set_direction', i)[0].y
44+
if(current_y == last_y):
45+
num_equal += 1
46+
last_y = current_y
47+
48+
max_y = max(max_y, current_y)
49+
min_y = min(min_y, current_y)
50+
51+
assert_eq(num_equal, 0)
52+
53+
assert_between(min_y, -.5, -.3)
54+
assert_between(max_y, .3, .5)
55+
56+

0 commit comments

Comments
 (0)