Is there any way I can make this code more efficient?

I am still a beginner with Lua I feel, but I have coded this game where the user must tap the screen to avoid the UFO from hitting a wall. The object is to not hit the wall for the longest time. There is slight delay from when the user taps the screen and when the UFO changes directions.

function setup()
    score = 0
    hit = 0
    x=380 --starting x
    y=400 --starting y
    d=100 --diameter of ball
    dx = 0
    dy = 0
    k = 0
end
 
function draw()
    background(0, 0, 0, 255)
    pushStyle()
    sprite("Space Art:UFO",x,y,d)
    font("Futura-CondensedExtraBold")
    textAlign(CENTER)
    fill(180, 207, 173, 255)
    lel = string.format("%.f",(ElapsedTime))
    text("Tap screen when UFO is about to hit wall.\
\
You have survived for " .. lel .. " seconds.", 380, 600)
    text("Score: " .. score,350,700)
    fontSize(40)
    popStyle()
    --[[ change direction if we hit a wall
    if x>WIDTH-d/2 or x<d/2 then dx=-dx  score = score + 1 print("You hit a wall! Try again") end
    if y>HEIGHT-d/2 or y<d/2 then dy=-dy end
    ]]
    if x>WIDTH-d/2 or x<d/2 or y>HEIGHT-d/2 or y<d/2 then y = 400 x = 380 oldscore = score print("You just got a score of " .. oldscore ..  "!") score = 0 dx = 0 dy = 0 k = 0  print("You hit a wall! Try again") else x=x+dx
    y=y+dy end

function touched(touch)
        if touch.state == BEGAN then
            k = k + 1
            if k == 1 then
            score = 1
            dx=2 --the amount x changes
            dy=2 --the amount y changes
            score = score + 2
            end 
        elseif k > 1 then
            if dx > 0 then
            dx= math.random(-5, -1) --the amount x changes
            dy= math.random(-5, -1) --the amount y changes
            score = score + 1
            elseif dx < 0 then
            dx=math.random(1,5)
            dy=math.random(1,5)
            end
            if y > 619 or y < 101 then
                score = score + 10
            elseif x > 619 or x < 101 then
                score = score + 10
            end
        end
    end
end

If you comment out line 40

-- elseif k > 1 then

It stops the delay. This is because it is waiting for you to move or remove your finger from the screen before the UFO changes direction. However i have not looked over the rest of your code to see if this affects the scoring system.
This is a very good game well done

Thanks Coder, I tweaked that and a few other things and managed to make it much more responsive and efficient. Here is my code now:

function setup()
    score = 0
    hit = 0
    x=380 --starting x
    y=400 --starting y
    d=100 --diameter of ball
    dx = 0
    dy = 0
    k = 0
    alrighty = 0
end

function draw()
    background(0, 0, 0, 255)
    pushStyle()
    sprite("Space Art:UFO",x,y,d)
    font("HelveticaNeue-Bold")
    textAlign(CENTER)
    fill(255, 255, 255, 255)
    --lel = string.format("%.f",(ElapsedTime - alrighty))
    if score == 0 then
        alrighty = ElapsedTime
    elseif score == 1 then
        alrighty = ElapsedTime
    end
    text("Score: " .. score .. "\
\
Tap screen when UFO is about to hit wall.\
\
You have survived for " .. string.format("%.f",(ElapsedTime - alrighty)) .. " seconds.", 380, 555)
    fontSize(50)
    popStyle()
    --[[ change direction if we hit a wall
    if x>WIDTH-d/2 or x<d/2 then dx=-dx  score = score + 1 print("You hit a wall! Try again") end
    if y>HEIGHT-d/2 or y<d/2 then dy=-dy end
    ]]
    if x>WIDTH-d/2 or x<d/2 or y>HEIGHT-d/2 or y<d/2 then y = 400 x = 380 oldscore = score print("You just got a score of " .. oldscore ..  "!") score = 0 dx = 0 dy = 0 k = 0 else x=x+dx
    y=y+dy end
end

function touched(touch)
        if touch.state == BEGAN then
            k = k + 1
            if k == 1 then
            dx=2 --the amount x changes
            dy=2 --the amount y changes
            score = score + 2
            end
        if touch.state == MOVING or touch.state == BEGAN and k > 1 then
            if dx > 0 then
            dx= math.random(-6, -2) --the amount x changes
            dy= math.random(-6, -2) --the amount y changes
            score = score + 1
            elseif dx < 0 then
            dx=math.random(2,6)
            dy=math.random(2,6)
            end
            if y > 619 or y < 101 then
                score = score + math.random(8,11)
            elseif x > 619 or x < 101 then
                score = score + math.random(8,11)
            end
            end
        end
end

This is a great game, what next? Saving highscores maybe?

@Coder done. :slight_smile:

function setup()
    displayMode(FULLSCREEN)
    score = 0
    hit = 0
    x=500--starting x
    y=400 --starting y
    d=100 --diameter of ball
    dx = 0
    dy = 0
    k = 0
    alrighty = 0
    rcol = 0
    bcol = 0
    gcol= 0
    alphacol= 0
    highscore = readLocalData("highscore")

    if not highscore then
        highscore = 0
    end
end

function draw()
    pushStyle()
    font("HelveticaNeue-Bold")
    textAlign(CENTER)
    fill(255, 255, 255, 255)
    if score == 0 then
        alrighty = ElapsedTime
        background(0, 0, 0, 0)
    elseif score == 1 then
        alrighty = ElapsedTime
    elseif score > 0 then
        background(rcol,gcol,bcol,alphacol)
    end
    sprite("Space Art:UFO",x,y,d)
    textMode(CENTER)
    textAlign(CENTER)
    text("Your high score is " .. highscore .. ".\
Score: " .. score .. "\
Tap screen when UFO is about to hit wall.\
\
You have survived for " .. string.format("%.f",(ElapsedTime - alrighty)) .. " seconds.", 500,500)
    fontSize(55)
    popStyle()
    --[[ change direction if we hit a wall
    if x>WIDTH-d/2 or x<d/2 then dx=-dx  score = score + 1 print("You hit a wall! Try again") end
    if y>HEIGHT-d/2 or y<d/2 then dy=-dy end
    ]]
    if x>WIDTH-d/2 or x<d/2 or y>HEIGHT-d/2 or y<d/2 then y = 400 x = 500 saveLocalData("highscore", highscore) print("Your high score is " .. highscore .. ". You just got a score of " .. score ..  ", in " .. string.format("%.f",(ElapsedTime - alrighty)) .. " seconds!" .. WIDTH .. HEIGHT) score = 0 dx = 0 dy = 0 k = 0 else x=x+dx
    y=y+dy end
    if score > highscore then
        highscore = score
    end
end

function touched(touch)
        if touch.state == BEGAN and k ~= nil then
            k = k + 1
            if k == 1 then
            dx=2 --the amount x changes
            dy=2 --the amount y changes
            score = score + 2
            end
        end
        if touch.state == BEGAN and k > 1 then
            rcol= math.random(200)
            bcol= math.random(200)
            gcol= math.random(200)
            alphacol=math.random(200)
            if dx > 0 then
            dx= math.random(-7, -3) --the amount x changes
            dy= math.random(-7, -3) --the amount y changes
            score = score + 1
            elseif dx < 0 then
            dx=math.random(3,7) --changes direction and speed to a random number between 2 and 6 if it is a negative value.
            dy=math.random(3,7)
            score = score + 1
            end
            if y > 649 or y < 101 then
                score = score + math.random(7,11)
            elseif x > 649 or x < 101 then
                score = score + math.random(7,11)
            end
        end
end