sbowde4
sbowde4

Reputation: 777

ASCII Animation Using System.Console.ANSI

I'm on my third Haskell project for my class this semester and we went from hello world, to simple math, to animating ASCII characters using Haskell and I am completely dumbfounded for this project. My professor has nothing on his website about this, and the documentation for System.Console.ANSI is not very helpful. I can not find any examples about this online either after extensive searching.

Has anyone done this or at least tell me if I am going in the right direction?

The first question is this


[16] Use the System.Console.ANSI library to draw a frame-based color drawing [4]. It must be at least 8 rows by 16 columns [4] and have at least 2 frames [8], which should alternate infinitely. Call this color-comic.hs.


my color-comic.hs looks like this

module Main (
        main
    ) where

import System.Console.ANSI
import System.IO

import Control.Concurrent


colorComic :: [IO ()]
colorComic = [printBlock, flashBlue, flashRed]


main :: IO ()
main = do 
    colorComic

pause :: IO ()
pause = do
    hFlush stdout
    -- 1 second pause
    threadDelay 1000000


flashRed :: IO ()//change text color to red
flashRed = do 
    setSGR [SetColor Foreground Vivid Red]
    pause

flashBlue :: IO ()//change text color to blue
flashBlue = do
    setSGR [SetColor Foreground Vivid Blue]
    pause

printBlock :: IO ()// print 8x16 block
printBlock = do
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"

I do not understand how to let this run infinitely, and I get this error and I am not sure why, putStrLn has always worked for me. Am I going about this correctly?

color-comic.hs:39:5: parse error on input ‘putStrLn’

The second question is a lot tougher, but I think I have an idea how to do this


[32] Use the System.Console.ANSI library to draw a colored animation in ASCII. The animation should be 25 rows x 60 cols [8], have a framerate of 30 Hz [4], last for no more than 10 seconds [4], use at least three colors [8], be such that no two consecutive frames in the animation are the same [8], and be unique [-32].


For the above question I could simply make a block of # in the correct dimensions, and then change the color of each row in succession and then redraw the whole block with the color pushed down one and a new color added and so on. I am not sure how to do Frame rate though.

Any help is deeply appreciated, I am just trying to learn.

Upvotes: 2

Views: 665

Answers (2)

luis-fdz
luis-fdz

Reputation: 181

You can avoid the recursive call at the end of printBlock and the need for the colorComic function using iterateM_ instead of mapM_ in your main function.

main :: IO ()
main = do
  resetScreen
  iterateM_ (\_ -> printBlock) ()

Upvotes: 0

sbowde4
sbowde4

Reputation: 777

I was able to make the infinite flash by using recursion, which for some reason, wasn't causing it to hang before.

module Main (
        main
    ) where

import System.Console.ANSI
import System.IO

import Control.Concurrent


colorComic :: [IO ()]
colorComic = [printBlock]


main :: IO ()
main = mapM_ (\color_comic -> resetScreen >> color_comic) colorComic

resetScreen :: IO ()
resetScreen = clearScreen >> setSGR [Reset] >> setCursorPosition 0 0

pause :: IO ()
pause = do
    hFlush stdout
    -- 1 second pause
    threadDelay 1000000


printBlock :: IO ()
printBlock = do
    clearScreen >> setCursorPosition 0 0
    setSGR [SetColor Foreground Vivid Red]
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    pause
    clearScreen >> setCursorPosition 0 0
    setSGR [SetColor Foreground Vivid Blue]
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    putStrLn "################"
    pause
    printBlock

Upvotes: 1

Related Questions