Window limits


I am new to the forum, and programming. This I do for entertainment.

I tried to put limits on the player window, and the heart, which is the enemy off the screen, as shown in the video I posted below.
I used Video Game controller that posting Nat

I would appreciate someone guide me to add the limits.

Ps: sorry for my bad English

Hello @Anak, do you mean limits to how far a sprite can go ?
Something like should work fine:

if sprite.x > WIDTH then
   sprite.x = WIDTH
if sprite.x < 0 then
   sprite.x = 0

if sprite.y > HEIGHT then
   sprite.y = HEIGHT

if sprite.y > 0 then
   sprite.y = 0

you could also do:

math.max(0, math.min(WIDTH, sprite.x))
math.max(0, math.min(HEIGHT, sprite.y))

which would make code a bit more tidy, although a tiny bit slower (you won’t notice it)


@Xavier: actually, your version using math.max/math.min won’t do anything unless you assign the result :wink:

sprite.x = math.max(0, math.min(WIDTH, sprite.x))
sprite.y = math.max(0, math.min(HEIGHT, sprite.y))

@toadkick - haha yeah forgot that ty :wink: