提问人:floxam 提问时间:2/23/2023 最后编辑:floxam 更新时间:2/27/2023 访问量:86
如何使用光泽库在屏幕上移动自定义图像
how to approach moving custom images around the screen using the gloss library
问:
我正在尝试构建一个简单的光泽游戏,该游戏使用 BMP 文件作为精灵,这些精灵将在加载后进行动画和移动。我正在尝试使用动画或模拟功能让其中一些图像移动。
simulate 的参数之一是类型 (model -> Picture)(即采用用户定义的数据类型存储游戏状态并返回 Picture 的函数),而通常用于加载文件的 loadBMP 函数是 loadBMP :: FilePath -> IO Picture。
很难掌握我应该在更高级别上做什么才能让图像移动,特别是考虑到 loadBMP 会返回 Picture IO 而不是 Picture。有什么想法吗?对不起,如果这还不够具体。
module Main(main) where
import Graphics.Gloss
import System.IO
import Control.Monad
import Graphics.Gloss.Data.ViewPort
data GameState = State{
boardpic::Picture
, ballpic :: Picture
, ballCoords :: (Float, Float)
, ballSpeed :: (Float, Float)
, paddle1pic::Picture
, p1Coords :: (Float, Float)
, paddle2pic::Picture
, p2Coords:: (Float,Float)
, paused::Bool
, turnscore::Integer
} deriving (Show)
moveBall time state = state {ballCoords = (xnew, ynew)}
where
(xposbefore, yposbefore) = ballCoords state
(xspeed, yspeed) = ballSpeed state
(xnew, ynew) = (xposbefore + xspeed*time, yposbefore+xspeed*time)
render::GameState->Picture
render state =
pictures[board,
paddle1,
paddle2,
ball]
where
board = boardpic state
ball = translate 0 0 $ ballpic state
paddle1 = translate (-300) 0 $ paddle1pic state
paddle2 = translate 300 0 $ paddle2pic state
--calculates running sum of score for the player from list
score xs = tail.reverse $ foldl f [0] xs
where
f (y:ys) x = (x+y):y:ys
window :: Display
window = InWindow "SuperPong" (800, 800) (0,0)
background::Color
background = blue
fps :: Int
fps = 60
printState::GameState->IO ()
printState state = do
putStrLn $ show $ boardpic state
putStrLn $ show $ ballpic state
putStrLn $ ("ball coordinates: ")++(show $ ballCoords state)
putStrLn $ ("paddle1 coords: " )++(show $ p1Coords state)
putStrLn $ ("paddle2 coords: " )++(show $ p2Coords state)
putStrLn $ ("paused or not: " ) ++ (show $ paused state)
putStrLn $ ("score: " )++(show $ turnscore state)
--paddlecollision::
--outofbounds::
main :: IO ()
main = do
pad1 <- loadBMP "paddle1.bmp"
pad2 <- loadBMP "paddle2.bmp"
board <- loadBMP "board.bmp"
ball1 <- loadBMP "ball.bmp"
let initstate = State{
boardpic = board
, ballpic = ball1
, ballCoords = (-10, 10)
, ballSpeed = (50, -50)
, paddle1pic = pad1
, p1Coords = (-300, 0)
, paddle2pic = pad2
, p2Coords = (300, 0)
, paused = False
, turnscore = 0
}
let update _ = moveBall
printState initstate
let allpics = render initstate
simulate window background fps initstate render update
更新
答: 暂无答案
评论
main
Picture
model
animate
animate
simulate
ballCoords