Finallized scoring logic and adjusted ball speed.

This commit is contained in:
Henry 2025-08-25 14:51:30 +01:00
parent 4eba01e61b
commit e5dc6dd42a
3 changed files with 48 additions and 4 deletions

View file

@ -3,6 +3,8 @@ extends CharacterBody2D
const SPEED: float = 350.0 const SPEED: float = 350.0
@export var player_id: int @export var player_id: int
@export var score_label: Label
var score: int = 0
func _input(event: InputEvent) -> void: func _input(event: InputEvent) -> void:
if event.is_action_pressed('up-%d' % player_id): if event.is_action_pressed('up-%d' % player_id):
@ -15,3 +17,9 @@ func _input(event: InputEvent) -> void:
func _physics_process(_delta: float) -> void: func _physics_process(_delta: float) -> void:
move_and_slide() move_and_slide()
func increment_score():
# score one point
score += 1
# update the UI label
score_label.text = '%02d' % score

12
ball.gd
View file

@ -1,6 +1,10 @@
extends CharacterBody2D 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: func _ready() -> void:
initialize() initialize()
@ -17,5 +21,11 @@ func initialize():
position = get_viewport_rect().size / 2.0 position = get_viewport_rect().size / 2.0
func _on_screen_exited() -> void: 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 await get_tree().create_timer(1.0).timeout
initialize() initialize()

View file

@ -57,17 +57,21 @@ shape = SubResource("RectangleShape2D_mwb40")
[node name="Polygon2D" type="Polygon2D" parent="BottomWall"] [node name="Polygon2D" type="Polygon2D" parent="BottomWall"]
polygon = PackedVector2Array(-30, -10, -30, 10, 1172, 10, 1172, -10) 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) 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) position = Vector2(1078, 273)
player_id = 1 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) position = Vector2(134, 72)
motion_mode = 1 motion_mode = 1
script = ExtResource("2_e2o6t") script = ExtResource("2_e2o6t")
paddle_0 = NodePath("../Paddle-0")
paddle_1 = NodePath("../Paddle-1")
[node name="Polygon2D" type="Polygon2D" parent="Ball"] [node name="Polygon2D" type="Polygon2D" parent="Ball"]
polygon = PackedVector2Array(100, 75, 120, 75, 120, 95, 100, 95) 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"] [node name="VisibleOnScreenNotifier2D" type="VisibleOnScreenNotifier2D" parent="Ball"]
position = Vector2(110, 85) 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"] [connection signal="screen_exited" from="Ball/VisibleOnScreenNotifier2D" to="Ball" method="_on_screen_exited"]