pong tutorial errors

gibberingmouthergibberingmouther Posts: 11Member
edited November 2016 in Programming

i put this here rather than in the tutorials section since i didn't want to clutter that one up. a mod can move it if they feel it is appropriate.

i'm getting the following error: "Unexpected token: if:" at line 28 of my code.

code: http://pastebin.com/YzVqrg0n

i don't know why i'm getting this error. i just started with Godot (but i do have some programming background) and i'm just working through the tutorials for now.

Answers

  • gibberingmouthergibberingmouther Posts: 11Member
    edited November 2016

    nevermind apparently if statements need indendation. i'd delete this thread but i'll leave it here in case someone else has the same problem.

    edit: okay, that didn't fix everything. now i get an "unexpected indent" error at line 49. if i don't indent, i get the previous error. not sure what to do?

  • danjodanjo Posts: 81Member
    edited November 2016 Answer ✓

    You have to indent your code properly.
    See http://docs.godotengine.org/en/stable/reference/gdscript.html#example-of-gdscript
    and http://www.diveintopython.net/getting_to_know_python/indenting_code.html

    This should give you no indentation error:

    extends Node2D
    
    # Member variables
    var screen_size
    var pad_size
    var direction = Vector2(1.0, 0.0)
    
    # Constant for pad speed (in pixels/second)
    const INITIAL_BALL_SPEED = 80
    # Speed of the ball (also in pixels/second)
    var ball_speed = INITIAL_BALL_SPEED
    # Constant for pads speed
    const PAD_SPEED = 150
    
    func _ready():
        screen_size = get_viewport_rect().size
        pad_size = get_node("left").get_texture().get_size()
        set_process(true)
    
    func _process(delta):
        var ball_pos = get_node("ball").get_pos()
        var left_rect = Rect2( get_node("left").get_pos() - pad_size*0.5, pad_size )
        var right_rect = Rect2( get_node("right").get_pos() - pad_size*0.5, pad_size )
        # Integrate new ball postion
        ball_pos += direction * ball_speed * delta
    
        # Flip when touching roof or floor
        if ((ball_pos.y < 0 and direction.y < 0) or (ball_pos.y > screen_size.y and direction.y > 0)):
            direction.y = -direction.y
    
        # Flip, change direction and increase speed when touching pads
        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
    
        # Check gameover
        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)
    
        get_node("ball").set_pos(ball_pos)
    
        # Move left pad
        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)
    
        # Move right pad
        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)
    
Sign In or Register to comment.