paddle.gd 659 B

123456789101112131415161718192021222324252627282930
  1. extends Area2D
  2. const MOVE_SPEED = 100
  3. var _ball_dir
  4. var _up
  5. var _down
  6. @onready var _screen_size_y = get_viewport_rect().size.y
  7. func _ready():
  8. var n = String(name).to_lower()
  9. _up = n + "_move_up"
  10. _down = n + "_move_down"
  11. if n == "left":
  12. _ball_dir = 1
  13. else:
  14. _ball_dir = -1
  15. func _process(delta):
  16. # Move up and down based on input.
  17. var input = Input.get_action_strength(_down) - Input.get_action_strength(_up)
  18. position.y = clamp(position.y + input * MOVE_SPEED * delta, 16, _screen_size_y - 16)
  19. func _on_area_entered(area):
  20. if area.name == "Ball":
  21. # Assign new direction.
  22. area.direction = Vector2(_ball_dir, randf() * 2 - 1).normalized()