【发布时间】:2015-10-30 23:58:40
【问题描述】:
我正在尝试用 Haskell 制作游戏。现在,我正在处理敌人的碰撞问题:也就是说,如果敌人与玩家发生碰撞,则分数会重置为 0,并且敌人会死亡。但是,我就是不知道如何。
我已经考虑过do 块,但这是不对的(我不想返回 IO)。我找不到任何可以为我指明正确方向的东西,让我怀疑这是否可能......
我拥有的(有点难看的)代码是这样的:
newEnemies = updateEnemies enemies
updateEnemies :: [Point] -> [Point]
updateEnemies [] = []
updateEnemies (x:xs) | hitPlayer (nextLocation x) = -- TODO: Reset multipliers
updateEnemies xs
| otherwise = nextLocation x : updateEnemies xs
where
hitPlayer :: Point -> Bool
hitPlayer (x, y) | abs (fst newPosition - x) < 10 && abs (snd newPosition - y) < 10
= True
| otherwise = False
nextLocation loc = (loc + ((dirToPlayer loc) * (100, 100) * (timeStep, timeStep)))
dirToPlayer loc = normalizeV (playerPosition - loc)
你们谁能指出我正确的方向,还是我太习惯命令式语言了?
谢谢!
【问题讨论】:
-
如果您的函数不返回分数,您希望它如何重置分数?如果你想重构,那么我建议你使用 State monad。相反,如果您想从当前代码逐步改进,请考虑创建
updateEnemies :: Score -> [Point] -> (Score, [Point])。如果命中,您返回(0, updateEnemies xs)。 -
那么,我尝试做的事情不起作用?我同样害怕。无论如何,谢谢,我要试试别的!