每次 pset 和 sprite 碰撞时,如何使分数计数增加 1

How to make score count go up by 1, everytime pset and sprite collide

所以我是编程新手,目前正在尝试使用 pico-8 进行编码。

我开始了一个精灵应该从顶部掉落的游戏,当与我的 pset(点)碰撞时,我希望我的分数增加 1。 截至目前,我遇到了两种不同的结果。 第一个分数不断快速上升的地方,以及每次我的点超过精灵左上角像素 y 和 x 时分数都会上升的地方。 我不知道如何解决它,我真的很想知道它有什么问题。

(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

你有两个问题。最主要的是这两行:

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

这不会增加“当像素接触 sx 和 sy”时的分数。相反,当像素 与目标在同一水平或垂直线上 时,它会增加分数。

您需要的是同时检查两者的条件。您可以使用 and 来做到这一点。用这一行替换这两行:

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

您遇到的第二个问题是 每一帧 都会评估此检查。因此,像素与目标重合的每一帧,分数都会增加。考虑到游戏每秒做 30 帧,分数增加得非常快。

你需要的是一个额外的变量来检查是否已经有一个“触摸”。

您可以在选项卡 1 中将其初始化为 false:

touch = false

然后用在前面的if上。我们要的是:

  • 当像素接触到目标时,增加分数,但前提是它在前一帧没有接触到它。独立于此,我们想在之后将 touch 设置为 true,这样下一帧就不会激活分数。
  • 当像素没有接触到目标时,我们必须将触摸重置为false

所以用这个代替我提到的前一行:

if x == sx and y == sy then
  if(not touch) score+=1
  touch = true
else
  touch = false
end