1

So i'm new to programming and is currently trying out coding in pico-8.

I made the start to a game where the sprite is supposed to fall down from the top, and when colliding with my pset (point), i would like my score count to go up by 1. As of now i've come across 2 different outcomes. The first where the score is going up insanely fast constantly, and one where the score goes up everytime my point goes past the sprites top left pixels y and x. I don't know how to fix it, and i really want to know whats wrong with it.

(Tab 1)
col=11
    sx=40
    sy=20
    x=64
    y=64
    score=0

function _init()
cls()   
    
    
end

function _update()
    
cls()   
    
    movement()

    border()
    
    point()
    
    sprite1() 
    
    if (x == sx) then score +=1 end
    if (y == sy) then score +=1 end


end
  
  

(Tab 2)
function _draw()

print("score:",0,0)

print(score,25,0)


end

(Tab 3)
-- movement

function point()
pset(x,y,col)

end


function movement()


if btn(⬅️) then x -= 1 end
if btn(➡️) then x += 1 end 
if btn(⬆️) then y -= 1 end 
if btn(⬇️) then y += 1 end
end 



-- sprite1
s1=1

function sprite1()
spr(s1,sx,sy)

end

(Tab 4)
-- border

function border()

if x>=127 then y=60 end
if x<=0 then y=60 end
if y>=127 then x=60 end
if y<=0 then x=60 end

if x>=127 then x=60 end
if x<=0 then x=60 end
if y>=127 then y=60 end
if y<=0 then y=60 end
end
Trylle
  • 13
  • 2

1 Answers1

2

You have two problems. The main one is these two lines:

    if (x == sx) then score +=1 end
    if (y == sy) then score +=1 end

That will not increase the score "when the pixel touches sx, and sy". Instead, it will increase the score when the pixel is on the same horizontal or vertical line as the target.

What you want is a condition that checks both simultaneously. You can use and in order to do that. Replace both lines with this one:

if (x == sx and y == sy) then score += 1

The second problem you have is that this check is evaluated every single frame. So every single frame that the pixel coincides with the target, the score increases. Given that games do 30 frames per second, the score increases very fast.

What you need is an extra variable that checks wether there has been already a "touch".

You could initialize it to false in tab 1:

touch = false

And then use it on the previous if. What we want is:

  • when the pixel is touching the target, increase score, but only if it was not touching it on the previous frame. Independently of that, we want to set touch to true afterwards, so the next frame will not activate the score.
  • when the pixel is not touching the target, we must reset touch to false.

So replace the previous line I mentioned with this instead:

if x == sx and y == sy then
  if(not touch) score+=1
  touch = true
else
  touch = false
end
kikito
  • 51,734
  • 32
  • 149
  • 189