如何使用光泽库在屏幕上移动自定义图像

问题描述 投票:0回答:1

我正在尝试用光泽度构建一个简单的游戏,该游戏使用精灵的 BMP 文件,这些精灵在加载后将被动画化并移动。我正在尝试使用动画或模拟功能来移动其中的一些图像。

simulate 的参数之一是类型(模型 -> 图片)(即采用用户定义的数据类型存储游戏状态并返回图片的函数),而通常用于加载文件的 loadBMP 函数是 loadBMP :: 文件路径 -> IO 图片。

在掌握我到底应该在更高级别上做什么以使图像移动时遇到一些困难,特别是考虑到 loadBMP 将返回图片 IO 而不是图片。有任何想法吗?抱歉,如果这还不够具体。

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

更新了

haskell io gloss
1个回答
0
投票

我不确定你的游戏逻辑,但是下面的代码可以让你的球移动。

render::GameState->Picture
render state =  
  pictures[board, 
           paddle1, 
           paddle2,
           ball]
  where
    board = boardpic state
    -- ball = translate 0 0 $ ballpic state
    ball = translate (fst (ballCoords state)) (snd (ballCoords state)) $ ballpic state
    paddle1 = translate (-300) 0 $ paddle1pic state
    paddle2 = translate 300 0 $ paddle2pic state  
© www.soinside.com 2019 - 2024. All rights reserved.