Guest User

Untitled

a guest
Aug 20th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. extends Node2D
  2.  
  3. var screen_size
  4. var pad_size
  5. var direction = Vector2(1.0 , 0.0)
  6. var game_started = false
  7.  
  8. const INITIAL_BALL_SPEED = 120
  9. var ball_speed = INITIAL_BALL_SPEED
  10. const PAD_SPEED = 180
  11.  
  12. func _ready():
  13. screen_size = get_viewport_rect().size
  14. pad_size = get_node('LPallete').get_texture().get_size()
  15.  
  16. func _process(delta):
  17.  
  18. #starting the game
  19. if not game_started:
  20. if Input.is_action_just_pressed("ui_accept"):
  21. game_started = true
  22. direction = Vector2(randf() * 2 - 1, randf() * 2 - 1).normalized()
  23. return
  24.  
  25. #ball movement
  26. var ball_pos = get_node("Ball").position
  27. var left_rect = Rect2( get_node("LPallete").position - pad_size*0.5, pad_size )
  28. var right_rect = Rect2( get_node("RPallete").position - pad_size*0.5, pad_size )
  29. ball_pos += direction * ball_speed * delta
  30. if ((ball_pos.y < 0 and direction.y < 0) or (ball_pos.y > screen_size.y and direction.y > 0)): direction.y = -direction.y
  31.  
  32. #pad collision
  33. if ((left_rect.has_point(ball_pos) and direction.x < 0) or (right_rect.has_point(ball_pos) and direction.x > 0)):
  34. direction.x = -direction.x
  35. direction.y = randf()*2.0 - 1
  36. direction = direction.normalized()
  37. ball_speed *= 1.1
  38. if (ball_pos.x < 0 or ball_pos.x > screen_size.x):
  39. ball_pos = screen_size*0.5
  40. ball_speed = INITIAL_BALL_SPEED
  41. direction = Vector2(-1, 0)
  42. game_started = false
  43. get_node("Ball").position = ball_pos
  44.  
  45.  
  46. #PAD Movement
  47. var left_pos = get_node("LPallete").position
  48.  
  49. if (left_pos.y - pad_size.y * 0.5> 0 and Input.is_action_pressed("left_move_up")):
  50. left_pos.y += -PAD_SPEED * delta
  51. if (left_pos.y + pad_size.y * 0.5 < screen_size.y and Input.is_action_pressed("left_move_down")):
  52. left_pos.y += PAD_SPEED * delta
  53.  
  54. get_node("LPallete").position = left_pos
  55.  
  56. var right_pos = get_node("RPallete").position
  57.  
  58. if (right_pos.y - pad_size.y * 0.5 > 0 and Input.is_action_pressed("right_move_up")):
  59. right_pos.y += -PAD_SPEED * delta
  60. if (right_pos.y + pad_size.y * 0.5 < screen_size.y and Input.is_action_pressed("right_move_down")):
  61. right_pos.y += PAD_SPEED * delta
  62.  
  63. get_node("RPallete").position = right_pos
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment