create labels and optimize player script

master
Sascha 2025-01-16 12:58:57 +07:00
parent f29437aeec
commit 81926ff5f1
2 changed files with 61 additions and 16 deletions

@ -1,12 +1,60 @@
[gd_scene load_steps=2 format=3 uid="uid://cjxteipqurqy5"]
[gd_scene load_steps=4 format=3 uid="uid://cjxteipqurqy5"]
[ext_resource type="PackedScene" uid="uid://cm7lrhjnutqxx" path="res://player.tscn" id="1_80nbo"]
[sub_resource type="SystemFont" id="SystemFont_80nbo"]
[sub_resource type="LabelSettings" id="LabelSettings_e2o6t"]
font = SubResource("SystemFont_80nbo")
font_size = 32
[node name="Game" type="Node2D"]
[node name="Player1" parent="." instance=ExtResource("1_80nbo")]
position = Vector2(20, 68)
position = Vector2(20, 70)
[node name="Player2" parent="." instance=ExtResource("1_80nbo")]
position = Vector2(1124, 578)
player_number = 2
[node name="Control" type="Control" parent="."]
layout_mode = 3
anchors_preset = 0
offset_left = 599.0
offset_top = 247.0
offset_right = 599.0
offset_bottom = 247.0
size_flags_horizontal = 4
size_flags_vertical = 4
metadata/_edit_use_anchors_ = true
[node name="VBoxContainer" type="VBoxContainer" parent="Control"]
layout_mode = 1
grow_horizontal = 2
grow_vertical = 2
metadata/_edit_use_anchors_ = true
[node name="Player1Score" type="Label" parent="Control/VBoxContainer"]
layout_mode = 2
text = "Welcome to Pong!"
label_settings = SubResource("LabelSettings_e2o6t")
[node name="HBoxContainer" type="HBoxContainer" parent="Control/VBoxContainer"]
layout_mode = 2
size_flags_horizontal = 6
size_flags_vertical = 4
[node name="Player1Score" type="Label" parent="Control/VBoxContainer/HBoxContainer"]
layout_mode = 2
text = "0"
label_settings = SubResource("LabelSettings_e2o6t")
[node name="Label" type="Label" parent="Control/VBoxContainer/HBoxContainer"]
layout_mode = 2
text = ":"
label_settings = SubResource("LabelSettings_e2o6t")
[node name="Player2Score" type="Label" parent="Control/VBoxContainer/HBoxContainer"]
layout_mode = 2
text = "0"
label_settings = SubResource("LabelSettings_e2o6t")

@ -1,21 +1,18 @@
extends CharacterBody2D
@export var player_number: int = 1
const SPEED := 300
const SPEED := 500
var direction: int
func _physics_process(delta: float) -> void:
position.y += direction * SPEED * delta
func _input(event: InputEvent) -> void:
var up_action = "player%dup" % player_number
var down_action = "player%ddown" % player_number
direction = 0
if player_number == 1:
if event.is_action_pressed("player1up"):
direction = -1
elif event.is_action_pressed("player1down"):
direction = +1
elif player_number == 2:
if event.is_action_pressed("player2up"):
direction = -1
elif event.is_action_pressed("player2down"):
direction = +1
if Input.is_action_pressed(up_action):
direction = -1
elif Input.is_action_pressed(down_action):
direction = 1
position.y += direction * SPEED * delta
position.y = clamp(position.y, 50, 600)