Log In  


Hi, I am totally new to coding and have been learning little bits on pico-8 for a couple of weeks now. I was trying to practise a very basic wraparound effect, so that a circle being moved with the arrow keys can go off one side of the screen and reappear on the other.

This is the code:
'function _init()

x=63
y=63
speed=2
radius=15
leeway=2*radius

end

function _draw()
cls()
circfill(x,y,radius,8)

end

function _update()

if btn(➡️) then x+=speed end
if btn(⬅️) then x-=speed end

if x>(127+leeway) then x=(0-leeway) end

end'

The above code works as expected, but I originally wrote the wraparound effect line as:
'if x=(127+leeway) then x=(0-leeway) end'

(So 'if x=' rather than 'if x>')

But with the original version I get a syntax error of 'then' expected near '='

Can anyone explain to me why one version is functional but the other isn't?

Many thanks for any help!



= is the assignment operator. Use the == conditional operator instead when you want to check if two things are equal.


I think the “wraparound effect” could be a perfect way to understand the modulo operator %

The modulo returns the remainder of a division, for example the result of 10%7 is 3

You can see the number after the modulo sign % as a limit to wrap numbers around:

0%7=0
1%7=1
2%7=2 
3%7=3
…
6%7=6
7%7=0
8%7=1
9%7=2
…

The result will only be between 0 (included) and the number after the % sign (excluded)

So you can use the modulo operator to easily wrap your screen coordinates! ^w^

Once you’ll get used to this operator, you’ll see a lot of potential other applications in your programs!



[Please log in to post a comment]