How do i get this?

Sorry for the code, i dont know how to get it right in this forum.

But how do i do, so my sprite cant move outside the screen?
I am already getting the hang of codea but i cant find anything about this :frowning:

This is my code

function setup()
print(β€œHello World!”)
x = 200
y = 200
w = 100
h = 54

watch()

end

– This function gets called once every frame
function draw()
– This sets a dark background color
background(0, 0, 0, 0)

-- This sets the line thickness
strokeWidth(5)

-- Do your drawing here
sprite("SpaceCute:Background",374,374)
y = y + 2
sprite("Space Art:Green Ship",x,y,w*2,h*2)

text("hello world",2,2)

end

function touched(touch)

x = touch.x
y = touch.y

end

function watch()

parameter.watch("x")
parameter.watch("y")

end

Aah!!

How do i get the code right in this forum!

This is my code


function setup()
    print("Hello World!")
    x = 200
    y = 200
    w = 100
    h = 54
    
    watch()
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0, 0, 0, 0)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    sprite("SpaceCute:Background",374,374)
    y = y + 2
    sprite("Space Art:Green Ship",x,y,w*2,h*2)
    
    text("hello world",2,2)
    
end

function touched(touch)
    
    x = touch.x
    y = touch.y
    
    end
    
function watch()
    
    parameter.watch("x")
    parameter.watch("y")

    end
function touched(touch)
    -- math.max(a, b) returns the highest number, either a or b. If a is larger than b, it will return a, vice versa.
    -- math.min(a, b) is pretty close to math.max, it just returns the lowest number instead.
    -- Ironically, math.max is used to set a minimum and math.min is used to set a maximum.
    x = math.max(2 * w, math.min(WIDTH - (2 * w), touch.x))
    y = math.max(2 * h, math.min(HEIGHT - (2 * h), touch.y))
end

Use an if statement., like.

If y>HEIGHT-h then
y=0
end

@SkyTheCoder Thanks alot, He cant move out of the sides but still in the roof