Newbie Question. Pong Tutorial

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By XPomJohn
:warning: Old Version Published before Godot 3 was released.

I’m new to Godot so I’m following the Learning Step by Step Tutorial. For the Pong Game I can get the right paddle to move perfectly but the left paddle will only move down and the ball doesn’t move at all. My script is as follows:-

extends Node2D

member variables here, example:

var a=2

var b=“textvar”

var screen_size
var pad_size
const PAD_SPEED = 150
var ball_speed = 80
var ball_direction = Vector2(-1, 0)

func _ready():
# Called every time the node is added to the scene.
# Initialization here
screen_size = get_viewport_rect().size
pad_size = get_node(“left”).get_texture().get_size()
set_process(true)
pass

func _process(delta):
var ball_pos = get_node(“ball”).get_pos()
var left_rect = Rect2(get_node(“left”).get_pos() - pad_size/2.0, pad_size)
var right_rect = Rect2(get_node(“right”).get_pos() - pad_size/2.0, pad_size)
ball_pos += ball_direction * ball_speed * delta

if (( ball_pos.y > 0 and ball_direction.y > 0) or ( ball_pos.y > screen_size.y and ball_direction.y > 0)):
	ball_direction.y = -ball_direction.y
	
if (( left_rect.has_point(ball_pos) and ball_direction.x < 0 ) or ( right_rect.has_point(ball_pos) and ball_direction.x > 0)):
	ball_direction.x = -ball_direction.x
	ball_speed *= 1.1
	ball_direction.y  = randf() * 2.0 -1
	ball_direction = ball_direction.normalized()
	
	if (ball_pos.x < 0 or ball_pos.x > screen_size.x):
		ball_pos = screen_size * 0.5 
		ball_speed = 80
		ball_direction = Vector2(-1,0)
		
		get_node("ball").set_pos(ball_pos)
		
	
var left_pos = get_node("left").get_pos()
if (left_pos.y > 0 and Input.is_action_pressed("left_move_up")):
	left_pos.y += - PAD_SPEED * delta
	
if (left_pos.y < screen_size.y and Input.is_action_pressed("left_move_down")):
left_pos.y += PAD_SPEED * delta
get_node("left").set_pos(left_pos)

var right_pos = get_node("right").get_pos()
if ( right_pos.y > 0 and Input.is_action_pressed("right_move_up")):
	right_pos.y  += - PAD_SPEED * delta
	
if ( right_pos.y < screen_size.y and Input.is_action_pressed("right_move_down")):
	right_pos.y += PAD_SPEED * delta
	
get_node("right").set_pos(right_pos)

What am I doing wrong? Thanks

:bust_in_silhouette: Reply From: Tetane

Hi,

You set the ball_pos inside the if condition, you should put it out like this :

if (ball_pos.x < 0 or ball_pos.x > screen_size.x):
    ball_pos = screen_size * 0.5 
    ball_speed = 80
    ball_direction = Vector2(-1,0)

get_node("ball").set_pos(ball_pos)

And for the left paddle, it is outside the if condition, it should be inside :

if (left_pos.y < screen_size.y and Input.is_action_pressed("left_move_down")):
    left_pos.y += PAD_SPEED * delta

You should re-read your code more carefully :wink: (and put comments, it’s easier when someone else reads your code.)

Thanks for your quick answer. I’ll remember in future to add comments. I’ve got the ball to move but not the paddle yet which I’m working on

XPomJohn | 2016-10-01 10:28