Skip to content

Commit fb4c359

Browse files
committed
game ends
1 parent bce049e commit fb4c359

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

TddAndPong/Episode4/scripts/game.gd

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ extends Node2D
33
var _ball_start_pos = null
44
var _p1_score = 0
55
var _p2_score = 0
6+
var _max_score = 10
7+
8+
signal game_over
69

710
func _ready():
811
$Ball.set_speed(300)
@@ -24,18 +27,25 @@ func _process(delta):
2427
if Input.is_action_pressed("p2_down"):
2528
$P2Paddle.move_down(delta)
2629

30+
func _game_over():
31+
emit_signal('game_over')
32+
$Ball.set_speed(0)
2733

2834
func _on_P2KillBox_kill_ball():
2935
$Ball.set_position(_ball_start_pos)
3036
_p1_score += 1
3137
_update_display()
38+
if(_p1_score == _max_score):
39+
_game_over()
3240

3341

3442
func _on_P1KillBox_kill_ball():
3543
$Ball.set_position(_ball_start_pos)
3644
_p2_score += 1
3745
_update_display()
38-
46+
if(_p2_score == _max_score):
47+
_game_over()
48+
3949
func _update_display():
4050
$P1Score.set_text(str(_p1_score))
4151
$P2Score.set_text(str(_p2_score))
@@ -54,4 +64,10 @@ func get_p2_score():
5464

5565
func set_p2_score(p2_score):
5666
_p2_score = p2_score
67+
68+
func get_max_score():
69+
return _max_score
70+
71+
func set_max_score(max_score):
72+
_max_score = max_score
5773

TddAndPong/Episode4/test/unit/test_game.gd

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func test_get_set_p1_score():
2626
func test_get_set_p2_score():
2727
assert_accessors(_game, 'p2_score', 0, 10)
2828

29+
func test_get_set_max_score():
30+
assert_accessors(_game, 'max_score', 10, 5)
31+
2932
func test_when_p1_killbox_emits_kill_ball_then_ball_is_recentered():
3033
var ball = _game.get_ball()
3134
var orig_pos = ball.get_position()
@@ -65,8 +68,27 @@ func test_when_p2_scores_then_score_is_update():
6568
assert_eq(_game.get_node("P2Score").get_text(), '1')
6669

6770

71+
func test_when_p1_reaches_max_score_game_over_emitted():
72+
watch_signals(_game)
73+
_game.set_max_score(2)
74+
_simulate_p1_score(_game)
75+
_simulate_p1_score(_game)
76+
assert_signal_emitted(_game, 'game_over')
6877

69-
78+
func test_when_p2_reaches_max_score_game_over_emitted():
79+
watch_signals(_game)
80+
_game.set_max_score(3)
81+
_simulate_p2_score(_game)
82+
_simulate_p2_score(_game)
83+
_simulate_p2_score(_game)
84+
assert_signal_emitted(_game, 'game_over')
85+
86+
func test_when_game_ends_ball_stops_moving():
87+
_game.set_max_score(3)
88+
_simulate_p2_score(_game)
89+
_simulate_p2_score(_game)
90+
_simulate_p2_score(_game)
91+
assert_eq(_game.get_ball().get_speed(), 0)
7092

7193

7294

0 commit comments

Comments
 (0)