diff --git a/Paddle.gd b/Paddle.gd index 36278d7..dee8141 100644 --- a/Paddle.gd +++ b/Paddle.gd @@ -3,6 +3,8 @@ extends CharacterBody2D const SPEED: float = 350.0 @export var player_id: int +@export var score_label: Label +var score: int = 0 func _input(event: InputEvent) -> void: if event.is_action_pressed('up-%d' % player_id): @@ -15,3 +17,9 @@ func _input(event: InputEvent) -> void: func _physics_process(_delta: float) -> void: move_and_slide() + +func increment_score(): + # score one point + score += 1 + # update the UI label + score_label.text = '%02d' % score diff --git a/ball.gd b/ball.gd index 934b492..78a9d2a 100644 --- a/ball.gd +++ b/ball.gd @@ -1,6 +1,10 @@ extends CharacterBody2D -const SPEED : float = 300.0 +const SPEED : float = 310.0 + +@export var paddle_0 : Node2D +@export var paddle_1 : Node2D + func _ready() -> void: initialize() @@ -17,5 +21,11 @@ func initialize(): position = get_viewport_rect().size / 2.0 func _on_screen_exited() -> void: + #Update Score + if position.x <0.0: + paddle_1.increment_score() + else: + paddle_0.increment_score() + #Reint After 1 second await get_tree().create_timer(1.0).timeout initialize() diff --git a/game.tscn b/game.tscn index 54d9afe..3c52721 100644 --- a/game.tscn +++ b/game.tscn @@ -57,17 +57,21 @@ shape = SubResource("RectangleShape2D_mwb40") [node name="Polygon2D" type="Polygon2D" parent="BottomWall"] polygon = PackedVector2Array(-30, -10, -30, 10, 1172, 10, 1172, -10) -[node name="Paddle-0" parent="." instance=ExtResource("1_80nbo")] +[node name="Paddle-0" parent="." node_paths=PackedStringArray("score_label") instance=ExtResource("1_80nbo")] position = Vector2(40, 273) +score_label = NodePath("../HBoxContainer/Score-0") -[node name="Paddle-1" parent="." instance=ExtResource("1_80nbo")] +[node name="Paddle-1" parent="." node_paths=PackedStringArray("score_label") instance=ExtResource("1_80nbo")] position = Vector2(1078, 273) player_id = 1 +score_label = NodePath("../HBoxContainer/Score-1") -[node name="Ball" type="CharacterBody2D" parent="."] +[node name="Ball" type="CharacterBody2D" parent="." node_paths=PackedStringArray("paddle_0", "paddle_1")] position = Vector2(134, 72) motion_mode = 1 script = ExtResource("2_e2o6t") +paddle_0 = NodePath("../Paddle-0") +paddle_1 = NodePath("../Paddle-1") [node name="Polygon2D" type="Polygon2D" parent="Ball"] polygon = PackedVector2Array(100, 75, 120, 75, 120, 95, 100, 95) @@ -79,4 +83,26 @@ shape = SubResource("RectangleShape2D_feb5d") [node name="VisibleOnScreenNotifier2D" type="VisibleOnScreenNotifier2D" parent="Ball"] position = Vector2(110, 85) +[node name="HBoxContainer" type="HBoxContainer" parent="."] +layout_mode = 1 +anchors_preset = 10 +anchor_right = 1.0 +offset_top = 20.0 +offset_bottom = 43.0 +grow_horizontal = 2 + +[node name="Score-0" type="Label" parent="HBoxContainer"] +layout_mode = 2 +size_flags_horizontal = 3 +theme_override_font_sizes/font_size = 40 +text = "00" +horizontal_alignment = 1 + +[node name="Score-1" type="Label" parent="HBoxContainer"] +layout_mode = 2 +size_flags_horizontal = 3 +theme_override_font_sizes/font_size = 40 +text = "00" +horizontal_alignment = 1 + [connection signal="screen_exited" from="Ball/VisibleOnScreenNotifier2D" to="Ball" method="_on_screen_exited"]