haskelldrawgloss

Draw squares for a game board in haskell


I would like to be able to draw something like a chess board using haskell and gloss.

In any other language I could do something like

for( i=0; i < 10; i++){
   for( j=0; j < 10; j++){
      drawSquare(radius, i, j)
    }
}

and that would be it, but I am new to haskell and I have no idea how this can be done. I am using Gloss and I am able to draw things manually, but I want to be able to create them procedurally not 1 by 1 until I draw 100 squares.


Solution

  • If you work inside the IO monad, you can use the same style. For instance

    printSquares :: IO ()
    printSquares =
       forM_ [0..9] $ \x ->
          forM_ [0..9] $ \y -> do
             putStrLn "Here's a square!"
             putStrLn ("Square(" ++ show x ++ ", " ++ show y ++ ")")
             -- add here the actual drawing Gloss commands
    

    I'm not familiar with Gloss to suggest the actual drawing commands.


    Update: it seems Gloss has a different interface than using IO as above. You probably need something like

    squares :: Picture
    squares = Pictures [ square x y | x<-[0..9], y<-[0..9] ]
    
    square :: Float -> Float -> Picture
    square x y = Polygon [(x,y), (x+1,y), (x+1,y+1), (x,y+1) ]
       -- even better: use rectangleWire and then translate
       -- rectangleUpperWire also seems useful