我刚开始使用 Haskell,想在不安装额外库的情况下制作一个简单的实时游戏。我需要编写一个扫描键盘输入的循环,但如果没有输入,游戏也必须运行。我该怎么做呢?
最佳答案
随着我找到新的解决方案,这个答案正在更新
经过几个小时的学习,我想出了以下代码:
{- Simple game loop example. -}
import System.IO
import System.Timeout
inputTimeout = 50000 -- in microseconds
initialGameState = 100
type GameState = Int -- change to your own gamestate type here
nextGameState :: GameState -> Char -> GameState
nextGameState previousGameState inputChar =
-- REPLACE THIS FUNCTION WITH YOUR GAME
case inputChar of
's' -> previousGameState + 1
'a' -> previousGameState - 1
_ -> previousGameState
loop :: GameState -> IO () -- game loop
loop gameState =
do
putStrLn (show gameState)
hFlush stdout
c <- timeout inputTimeout getChar -- wait for input, with timeout
case c of
-- no input given
Nothing -> do loop gameState
-- quit on 'q'
Just 'q' -> do putStrLn "quitting"
-- input was given
Just input -> do loop (nextGameState gameState input)
main =
do
hSetBuffering stdin NoBuffering -- to read char without [enter]
hSetBuffering stdout (BlockBuffering (Just 80000)) -- to reduce flickering, you can change the constant
hSetEcho stdout False -- turn off writing to console with keyboard
loop initialGameState
几点注意事项:
inputTimeout
微秒,但不完全是。理论上,非常快的输入可以降低这一点,计算延迟会增加这一点。 更新代码
在之前的代码中,如果游戏
nextGameState
函数需要很长时间来计算,输入字符会堆积在标准输入中,程序的 react 会被延迟。以下代码通过始终从输入中读取所有字符并仅获取最后一个字符来解决此问题。 {- Simple game loop example, v 2.0. -}
import System.IO
import Control.Concurrent
frameDelay = 10000 -- in microseconds
initialGameState = 100
type GameState = Int -- change to your own gamestate type here
nextGameState :: GameState -> Char -> GameState
nextGameState previousGameState inputChar =
-- REPLACE THIS FUNCTION WITH YOUR GAME
case inputChar of
's' -> previousGameState + 1
'a' -> previousGameState - 1
_ -> previousGameState
getLastChar :: IO Char
getLastChar =
do
isInput <- hWaitForInput stdin 1 -- wait for char for 1 ms
if isInput
then
do
c1 <- getChar
c2 <- getLastChar
if c2 == ' '
then return c1
else return c2
else
do
return ' '
gameLoop :: GameState -> IO () -- game loop
gameLoop gameState =
do
threadDelay frameDelay
putStrLn (show gameState)
hFlush stdout
c <- getLastChar
case c of
'x' -> do putStrLn "quitting"
_ -> do gameLoop (nextGameState gameState c)
main =
do
hSetBuffering stdin NoBuffering -- to read char without [enter]
hSetBuffering stdout (BlockBuffering (Just 80000)) -- to reduce flickering, you can change the constant
hSetEcho stdout False -- turn off writing to console with keyboard
gameLoop initialGameState
关于haskell - 如何在纯 Haskell 中编写一个简单的实时游戏循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41533451/