Pong/scripts/ball.gd
Henry 49988ae674 Add menu system and scene navigation
The changes include: - Moving script files to scripts/ directory -
Creating Start menu scene with Play/Quit buttons - Adding scene
switching between menu and gameplay - Creating theme for menu buttons -
Adding ESC key to return to menu
2025-10-12 13:20:30 +01:00

31 lines
791 B
GDScript

extends CharacterBody2D
const SPEED : float = 310.0
@export var paddle_0 : Node2D
@export var paddle_1 : Node2D
func _ready() -> void:
initialize()
func _physics_process(_delta: float) -> void:
var collision = move_and_collide(velocity * _delta)
if collision:
velocity = velocity.bounce(collision.get_normal())
func initialize():
var extra_offset = 0.0 if randf() < 0.5 else PI
var angle = extra_offset + randf_range(-PI/3.0, PI/3.0)
velocity = Vector2(cos(angle), sin(angle)).normalized() * SPEED
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()