Write an implementation of pac-man in Haskell, using the Gloss library
- User
Write an implementation of pac-man in Haskell, using the Gloss library
- Assistant
Write an implementation of pac-man in Haskell, using the Gloss library
Certainly! I can help you create a simple game in Haskell using the Gloss library. We'll build a basic game where a player can move a character around the screen and collect items, similar to some aspects of Pac-Man. This example will show you how to handle graphics, input, and game state in Gloss.
First, make sure you have the Gloss library installed. You can install it using Cabal or Stack:
cabal update
cabal install gloss
Or, with Stack:
stack install gloss
Now, let's write the Haskell code for the game.
-- file: SimpleGame.hs
import Graphics.Gloss
import Graphics.Gloss.Interface.Pure.Game
import System.Random (randomRIO)
-- | Data type representing the game state
data GameState = GameState
{ playerPos :: (Float, Float) -- ^ Player position
, foodPos :: (Float, Float) -- ^ Food position
, score :: Int -- ^ Player's score
} deriving Show
-- | Initial game state
initialState :: IO GameState
initialState = do
fx <- randomRIO (-200, 200)
fy <- randomRIO (-200, 200)
return GameState
{ playerPos = (0, 0)
, foodPos = (fx, fy)
, score = 0
}
-- | Main function to run the game
main :: IO ()
main = do
state <- initialState
play window background fps state render handleInput update
where
window = InWindow "Simple Game" (600, 600) (100, 100)
background = black
fps = 60
-- | Render the game state to the screen
render :: GameState -> Picture
render game = pictures [player, food, scoreText]
where
(px, py) = playerPos game
(fx, fy) = foodPos game
player = translate px py $ color yellow $ circleSolid 10
food = translate fx fy $ color red $ circleSolid 5
scoreText = translate (-290) 260 $ scale 0.2 0.2 $ color white $ text $ "Score: " ++ show (score game)
-- | Handle user input
handleInput :: Event -> GameState -> GameState
handleInput (EventKey (SpecialKey key) Down _ _) game = game { playerPos = newPos }
where
(px, py) = playerPos game
step = 5
newPos = case key of
KeyUp -> (px, py + step)
KeyDown -> (px, py - step)
KeyLeft -> (px - step, py)
KeyRight -> (px + step, py)
_ -> (px, py)
handleInput _ game = game
-- | Update the game state
update :: Float -> GameState -> GameState
update _ game
| collision (playerPos game) (foodPos game) = game
{ foodPos = newFoodPos
, score = score game + 1
}
| otherwise = game
where
newFoodPos = (fx, fy)
(fx, fy) = randomPosition (playerPos game)
-- | Check for collision between player and food
collision :: (Float, Float) -> (Float, Float) -> Bool
collision (x1, y1) (x2, y2) = dist < 15
where
dist = sqrt ((x2 - x1)^2 + (y2 - y1)^2)
-- | Generate a new random position for the food, avoiding the player's position
randomPosition :: (Float, Float) -> (Float, Float)
randomPosition (px, py) = (fx, fy)
where
range = (-280, 280)
fx = unsafePerformIO $ randomRIO range
fy = unsafePerformIO $ randomRIO range
-- Note: 'unsafePerformIO' is generally discouraged. For a proper implementation,
-- you should handle randomness within the IO monad or use a random number generator
-- passed through the game state.
Explanation:
play function from Gloss.Notes:
unsafePerformIO in randomPosition is generally discouraged as it breaks referential transparency and can lead to unpredictable behavior. A proper implementation should handle randomness in a pure way or pass a random generator through the game state.unsafePerformIO. In a more advanced implementation, consider integrating a random number generator into your game state or using a library like MonadRandom.Running the Game:
To run the game, compile the code with GHC:
ghc --make SimpleGame.hs -threaded
Then execute the compiled program:
./SimpleGame
Use the arrow keys to move the yellow circle (the player) around the screen. When the player touches the red circle (the food), the score increases, and the food moves to a new random location.
Extending the Game:
This simple game demonstrates the basics of handling graphics, input, and state in Gloss. You can extend it by:
unsafePerformIO.Resources:
Let me know if you have any questions or need further assistance with specific features!
Not yet included in a published release.