1

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

我开始了一个游戏,其中精灵应该从顶部掉下来,当与我的 pset(点)碰撞时,我希望我的分数增加 1。截至目前我遇到了 2不同的结果。第一个分数不断快速上升,每次我的点超过精灵左上角像素 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
4

1 回答 1

1

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

    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 上使用它。我们想要的是:

  • 当像素接触目标时,增加分数,但前提是它在前一帧没有接触它。与此无关,我们想设置touchtrue之后,所以下一帧不会激活分数。
  • 当像素没有接触到目标时,我们必须将 touch 重置为false

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

if x == sx and y == sy then
  if(not touch) score+=1
  touch = true
else
  touch = false
end
于 2022-02-04T10:53:46.700 回答