Skip to content

Commit bce049e

Browse files
committed
simulate p1/p2 score
1 parent f056667 commit bce049e

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

TddAndPong/Episode4/scenes/Game.tscn

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,19 @@ position = Vector2( 285, 289.5 )
3232
[node name="P2KillBox" parent="." instance=ExtResource( 5 )]
3333
position = Vector2( 667.5, 289.5 )
3434

35+
[node name="P1Score" type="Label" parent="."]
36+
margin_left = 373.0
37+
margin_top = 12.6667
38+
margin_right = 432.0
39+
margin_bottom = 26.6667
40+
text = "p1 score"
41+
42+
[node name="P2Score" type="Label" parent="."]
43+
margin_left = 497.833
44+
margin_top = 13.0
45+
margin_right = 556.833
46+
margin_bottom = 27.0
47+
text = "p2 score"
48+
3549
[connection signal="kill_ball" from="P1KillBox" to="." method="_on_P1KillBox_kill_ball"]
3650
[connection signal="kill_ball" from="P2KillBox" to="." method="_on_P2KillBox_kill_ball"]

TddAndPong/Episode4/scripts/game.gd

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
extends Node2D
22

33
var _ball_start_pos = null
4+
var _p1_score = 0
5+
var _p2_score = 0
46

57
func _ready():
68
$Ball.set_speed(300)
@@ -10,6 +12,8 @@ func _ready():
1012
$P1Paddle.set_speed(300)
1113
$P2Paddle.set_speed(300)
1214

15+
_update_display()
16+
1317
func _process(delta):
1418
if Input.is_action_pressed("p1_up"):
1519
$P1Paddle.move_up(delta)
@@ -23,10 +27,31 @@ func _process(delta):
2327

2428
func _on_P2KillBox_kill_ball():
2529
$Ball.set_position(_ball_start_pos)
30+
_p1_score += 1
31+
_update_display()
2632

2733

2834
func _on_P1KillBox_kill_ball():
2935
$Ball.set_position(_ball_start_pos)
36+
_p2_score += 1
37+
_update_display()
38+
39+
func _update_display():
40+
$P1Score.set_text(str(_p1_score))
41+
$P2Score.set_text(str(_p2_score))
3042

3143
func get_ball():
32-
return $Ball
44+
return $Ball
45+
46+
func get_p1_score():
47+
return _p1_score
48+
49+
func set_p1_score(p1_score):
50+
_p1_score = p1_score
51+
52+
func get_p2_score():
53+
return _p2_score
54+
55+
func set_p2_score(p2_score):
56+
_p2_score = p2_score
57+

TddAndPong/Episode4/test/unit/test_game.gd

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,21 @@ func before_each():
1111
func after_each():
1212
remove_child(_game)
1313

14+
func _simulate_p1_score(game):
15+
game.get_node('P2KillBox').emit_signal('kill_ball')
16+
17+
func _simulate_p2_score(game):
18+
game.get_node('P1KillBox').emit_signal('kill_ball')
19+
1420
func test_can_make_game():
1521
assert_not_null(Game.instance())
1622

23+
func test_get_set_p1_score():
24+
assert_accessors(_game, 'p1_score', 0, 10)
25+
26+
func test_get_set_p2_score():
27+
assert_accessors(_game, 'p2_score', 0, 10)
28+
1729
func test_when_p1_killbox_emits_kill_ball_then_ball_is_recentered():
1830
var ball = _game.get_ball()
1931
var orig_pos = ball.get_position()
@@ -31,3 +43,30 @@ func test_when_p2_killbox_emits_kill_ball_then_ball_is_recentered():
3143
ball.set_position(Vector2(1, 1))
3244
kb.emit_signal('kill_ball')
3345
assert_eq(ball.get_position(), orig_pos)
46+
47+
func test_when_p1_kill_box_kills_ball_p2_score_increases():
48+
_simulate_p2_score(_game)
49+
assert_eq(_game.get_p2_score(), 1)
50+
51+
func test_when_p2_kill_box_kills_ball_p1_score_increases():
52+
_simulate_p1_score(_game)
53+
assert_eq(_game.get_p1_score(), 1)
54+
55+
func test_score_labels_show_score_on_start():
56+
assert_eq(_game.get_node("P1Score").get_text(), '0', 'p1 initial score')
57+
assert_eq(_game.get_node("P2Score").get_text(), '0', 'p2 initial score')
58+
59+
func test_when_p1_scores_then_score_is_update():
60+
_simulate_p1_score(_game)
61+
assert_eq(_game.get_node("P1Score").get_text(), '1')
62+
63+
func test_when_p2_scores_then_score_is_update():
64+
_simulate_p2_score(_game)
65+
assert_eq(_game.get_node("P2Score").get_text(), '1')
66+
67+
68+
69+
70+
71+
72+

0 commit comments

Comments
 (0)