Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- var screen_size
- var pad_size
- var direction = Vector2(1.0 , 0.0)
- var game_started = false
- const INITIAL_BALL_SPEED = 120
- var ball_speed = INITIAL_BALL_SPEED
- const PAD_SPEED = 180
- func _ready():
- screen_size = get_viewport_rect().size
- pad_size = get_node('LPallete').get_texture().get_size()
- func _process(delta):
- #starting the game
- if not game_started:
- if Input.is_action_just_pressed("ui_accept"):
- game_started = true
- direction = Vector2(randf() * 2 - 1, randf() * 2 - 1).normalized()
- return
- #ball movement
- var ball_pos = get_node("Ball").position
- var left_rect = Rect2( get_node("LPallete").position - pad_size*0.5, pad_size )
- var right_rect = Rect2( get_node("RPallete").position - pad_size*0.5, pad_size )
- ball_pos += direction * ball_speed * delta
- if ((ball_pos.y < 0 and direction.y < 0) or (ball_pos.y > screen_size.y and direction.y > 0)): direction.y = -direction.y
- #pad collision
- if ((left_rect.has_point(ball_pos) and direction.x < 0) or (right_rect.has_point(ball_pos) and direction.x > 0)):
- direction.x = -direction.x
- direction.y = randf()*2.0 - 1
- direction = direction.normalized()
- ball_speed *= 1.1
- if (ball_pos.x < 0 or ball_pos.x > screen_size.x):
- ball_pos = screen_size*0.5
- ball_speed = INITIAL_BALL_SPEED
- direction = Vector2(-1, 0)
- game_started = false
- get_node("Ball").position = ball_pos
- #PAD Movement
- var left_pos = get_node("LPallete").position
- if (left_pos.y - pad_size.y * 0.5> 0 and Input.is_action_pressed("left_move_up")):
- left_pos.y += -PAD_SPEED * delta
- if (left_pos.y + pad_size.y * 0.5 < screen_size.y and Input.is_action_pressed("left_move_down")):
- left_pos.y += PAD_SPEED * delta
- get_node("LPallete").position = left_pos
- var right_pos = get_node("RPallete").position
- if (right_pos.y - pad_size.y * 0.5 > 0 and Input.is_action_pressed("right_move_up")):
- right_pos.y += -PAD_SPEED * delta
- if (right_pos.y + pad_size.y * 0.5 < screen_size.y and Input.is_action_pressed("right_move_down")):
- right_pos.y += PAD_SPEED * delta
- get_node("RPallete").position = right_pos
Advertisement
Add Comment
Please, Sign In to add comment