abc*_*556 -1 haskell if-statement ncurses do-notation io-monad
我正在尝试制作一个非常简单的类似蛇的游戏,如果您尝试使用已经访问过的斧头坐标,则会输掉该游戏。
到目前为止,这是起作用的代码(您可以使用箭头键移动播放器1并使用wasd移动播放器2):
import UI.NCurses
main :: IO ()
main = runCurses $ do
w <- defaultWindow
updateWindow w $ do
drawBorder Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
render
loop w [] 1 1 0 0 10 10 0 0
loop :: Window -> [(Integer, Integer)] -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Curses ()
loop w list p1x p1y p1oldDx p1oldDy p2x p2y p2oldDx p2oldDy = do
e <- getEvent w (Just 100)
let coordList = updateXY e p1oldDx p1oldDy p2oldDx p2oldDy
let p1dX = coordList !! 0
let p1dY = coordList !! 1
let p2dX = coordList !! 2
let p2dY = coordList !! 3
updateWindow w $ do
moveCursor (p1y+p1dY) (p1x+p1dX)
drawString ("#")
moveCursor (p2y+p2dY) (p2x+p2dX)
drawString ("#")
render
let updatedList = list ++ [(p1y+p1dY, p1x+p1dX)] ++ [(p2y+p2dY, p2x+p2dX)]
loop w updatedList (p1x+p1dX) (p1y+p1dY) p1dX p1dY (p2x+p2dX) (p2y+p2dY) p2dX p2dY
updateXY :: Maybe Event -> Integer -> Integer -> Integer -> Integer -> [Integer]
updateXY e p1oldX p1oldY p2oldX p2oldY
| e == Just (EventSpecialKey KeyLeftArrow) = [-1, 0, p2oldX, p2oldY]
| e == Just (EventSpecialKey KeyRightArrow) = [1, 0, p2oldX, p2oldY]
| e == Just (EventSpecialKey KeyDownArrow) = [0, 1, p2oldX, p2oldY]
| e == Just (EventSpecialKey KeyUpArrow) = [0, -1, p2oldX, p2oldY]
| e == Just (EventCharacter 'a') = [p1oldX, p1oldY, -1, 0]
| e == Just (EventCharacter 'd') = [p1oldX, p1oldY, 1, 0]
| e == Just (EventCharacter 's') = [p1oldX, p1oldY, 0, 1]
| e == Just (EventCharacter 'w') = [p1oldX, p1oldY, 0, -1]
| p1oldX /= 0 = [p1oldX, 0, p2oldX, p2oldY]
| p1oldY /= 0 = [0, p1oldY, p2oldX, p2oldY]
| p2oldX /= 0 = [p1oldX, p1oldY, p2oldX, 0]
| p2oldY /= 0 = [p1oldX, p1oldY, 0, p2oldY]
| otherwise = [1, 0, 1, 0] -- Starts moving in x-direction. Change to (0,0) for stand-still
Run Code Online (Sandbox Code Playgroud)
因此,当您移动时,将放下“#”字符。如果您转到上面已经带有“#”的坐标,则上面的代码将不执行任何操作,因此我尝试通过将循环函数添加到之前来更改循环函数let updatedList...:
if length (filter (==(p1x, p1y)) list) > 0 then gameOver w "player one lost"
if length (filter (==(p2x, p2y)) list) > 0 then gameOver w "player two lost"
Run Code Online (Sandbox Code Playgroud)
并添加一个临时gameOver函数:
gameOver w player =
updateWindow w $ do
moveCursor (10) (10)
drawString (player)
render
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在GHCI中加载此文件时,出现以下错误消息:输入'if'解析错误
if的需求else。期。可以when在需要命令式样式时使用该函数:“如果这样做,则执行此操作,否则不执行任何操作”,但是没有内置语法:
import Control.Monad
when (length (filter (==(p1x, p1y)) list) > 0) $ gameOver w "player one lost"
when (length (filter (==(p2x, p2y)) list) > 0) $ gameOver w "player two lost"
Run Code Online (Sandbox Code Playgroud)
的定义when是
when cond action = if cond then action else pure ()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
61 次 |
| 最近记录: |