How to fix this pong game?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Shlopynoobnoob

the ball only moves on the x-axis, here’s the code:

extends Area2D
var screen_size
var direction = Vector2(1.0, 0.0)
var ball_speed = 89

func _ready():
    screen_size = get_viewport_rect().size

func _process(delta):
	position += direction * ball_speed * delta
	if (position.x < 0  or position.x > screen_size.x):
		direction.x = -direction.x

how to implement the perfect direction.y ?

:bust_in_silhouette: Reply From: estebanmolca

Here you can download the original example of godot pong:

2D Pong Demo

But in your code it’s easy, you just have to put the same thing again but using the y axis:

func _process(delta):
    position += direction * ball_speed * delta
    if (position.x < 0  or position.x > screen_size.x):
        direction.x = -direction.x
    if (position.y < 0  or position.y > screen_size.y):
        direction.y = -direction.y

And put some speed on the y axis to see the changes, for example:
var direction = Vector2(1.0, 1.5)

WOW, you are amazing you helped me two times, thanks a lot

Shlopynoobnoob | 2019-12-25 10:10