Hashtop

Cena bola pong

Nov 5th, 2020
241
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. parent é um kinematicbody2d
  2. collisionshape2d - capsule e radius 25 com height 0
  3. sprite - texture bola com scale 0.1, 0.1
  4. visibility notifier 2d - default no meio da bola e com sinal conectado no código
  5.  
  6. //código
  7. extends KinematicBody2D
  8.  
  9. var init_pos = Vector2.ZERO
  10.  
  11. export var speed := 300
  12. export var speed_increment := 15
  13.  
  14. var velocity
  15. var dir = Vector2.ZERO
  16.  
  17. var collision
  18.  
  19.  
  20. func _ready() -> void:
  21.     init_pos = get_global_position()
  22.     print(init_pos)
  23.    
  24.     while dir.y == 0:
  25.         dir.y = round(rand_range(-10, 10) / 10)
  26.     while dir.x == 0:
  27.         dir.x = round(rand_range(-10, 10) / 10)
  28.    
  29.     dir = dir.normalized()
  30.  
  31.  
  32. func _physics_process(delta: float) -> void:
  33.     velocity = speed * dir
  34.     collision = move_and_collide(velocity * delta)
  35.    
  36.     if collision:
  37.         var collider = collision.collider
  38.        
  39.         if collider.name.find("wall") != -1:
  40.             dir.y *= -1
  41.         elif collider.name.find("player") != -1:
  42.             dir.x *= -1
  43.            
  44.             if dir.y == 0:
  45.                 dir.y = round(rand_range(-10, 10) / 10)
  46.            
  47.             speed += speed_increment
  48.  
  49.  
  50. func _on_VisibilityNotifier2D_screen_exited() -> void:
  51.     position = init_pos
  52.  
RAW Paste Data