Advertisement
rodrigofbm

Game 2D

Sep 9th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. extends Node2D
  2.  
  3. var screen_size
  4. var pad_size
  5.  
  6.  
  7. var ballspeed = 80
  8. var ball_direction = Vector2(-1,0)
  9. var PAD_SPEED = 150
  10.  
  11.  
  12. func _ready():
  13.  
  14.    
  15.     screen_size = get_viewport_rect().size
  16.     pad_size = get_node("left").get_texture().get_size()
  17.     set_process(true)
  18.  
  19.  
  20.  
  21. func _process(delta):
  22.     var ball_pos = get_node("ball").get_pos()
  23.     var left_rect = Rect2( get_node("left").get_pos() - pad_size/2, pad_size )
  24.     var right_rect = Rect2( get_node("right").get_pos() - pad_size/2, pad_size )
  25.    
  26.     ball_pos += ball_direction*ballspeed*delta
  27.    
  28.     if ( (ball_pos.y < 0 and ball_direction.y < 0) or (ball_pos.y > screen_size.y and ball_direction.y > 0)  ):
  29.         ball_direction.y = - ball_direction.y
  30.    
  31.     if ( (left_rect.has_point(ball_pos) and ball_direction.x < 0) or (right_rect.has_point(ball_pos) and ball_direction.x > 0)):
  32.         ball_direction.x=-ball_direction.x
  33.         ballspeed*=1.1
  34.         ball_direction.y=randf()*2.0-1
  35.         ball_direction = ball_direction.normalized()
  36.    
  37.     if (ball_pos.x < 0 or ball_pos.x > screen_size.x):
  38.         ball_pos = screen_size/2
  39.         ballspeed = 80
  40.         ball_direction=Vector2(-1,0)
  41.          
  42.     get_node("ball").set_pos(ball_pos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement