ansi-terminal-game-0.5.0.0: sdl-like functions for terminal applications, based on ansi-terminal

Copyright© 2017-2019 Francesco Ariis
LicenseGPLv3 (see LICENSE file)
MaintainerFrancesco Ariis <fa-ml@ariis.it>
Stabilityprovisional
Portabilityportable
Safe HaskellNone
LanguageHaskell2010

Terminal.Game

Contents

Description

Machinery and utilities for 2D terminal games.

New? Start from Game.

Synopsis

Running

type FPS = Integer Source #

Frames per second.

data Event Source #

An Event is a Tick (time passes) or a KeyPress.

Constructors

Tick 
KeyPress Char 
Instances
Eq Event Source # 
Instance details

Defined in Terminal.Game.Layer.Object.Interface

Methods

(==) :: Event -> Event -> Bool #

(/=) :: Event -> Event -> Bool #

Show Event Source # 
Instance details

Defined in Terminal.Game.Layer.Object.Interface

Methods

showsPrec :: Int -> Event -> ShowS #

show :: Event -> String #

showList :: [Event] -> ShowS #

Generic Event Source # 
Instance details

Defined in Terminal.Game.Layer.Object.Interface

Associated Types

type Rep Event :: Type -> Type #

Methods

from :: Event -> Rep Event x #

to :: Rep Event x -> Event #

Arbitrary Event Source # 
Instance details

Defined in Terminal.Game.Layer.Object.Interface

Methods

arbitrary :: Gen Event #

shrink :: Event -> [Event] #

Serialize Event Source # 
Instance details

Defined in Terminal.Game.Layer.Object.Interface

Methods

put :: Putter Event #

get :: Get Event #

type Rep Event Source # 
Instance details

Defined in Terminal.Game.Layer.Object.Interface

type Rep Event = D1 (MetaData "Event" "Terminal.Game.Layer.Object.Interface" "ansi-terminal-game-0.5.0.0-Ip1MpuHsTkxFVdvGo6fHOU" False) (C1 (MetaCons "Tick" PrefixI False) (U1 :: Type -> Type) :+: C1 (MetaCons "KeyPress" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Char)))

data Game s Source #

Game definition datatype, parametrised on your gamestate. The two most important elements are the function dealing with logic and the drawing one. Check alone (you can compile it with cabal new-run -f examples alone) to see a simple game in action.

Constructors

Game 

Fields

playGame :: Game s -> IO () Source #

Entry point for the game execution, should be called in main.

You must compile your programs with -threaded; if you do not do this the game will crash at start-up. Just add:

ghc-options:      -threaded

in your .cabal file and you will be fine!

Game Logic

Some convenient function dealing with Timers (Timed) and Animations.

Usage of these is not mandatry: Game is parametrised over any state s, you are free to implement game logic as you prefer.

Timers

data Timed a #

A timed resource is a timer which, at any given moment, points to a specific item (like an animation).

Example:

timer = creaTimedRes (Times 1 Elapse) [(2, "a "), (1, "b "), (2, "c ")]
test t | isExpired t = putStrLn "Fine."
       | otherwise   = do putStr (fetchFrame t)
                          test (tick t)

   -- λ> test timer
   -- a a b c c Fine.
Instances
Functor Timed

Mapping on frames.

Instance details

Defined in Control.Timer.Tick

Methods

fmap :: (a -> b) -> Timed a -> Timed b #

(<$) :: a -> Timed b -> Timed a #

Eq a => Eq (Timed a) 
Instance details

Defined in Control.Timer.Tick

Methods

(==) :: Timed a -> Timed a -> Bool #

(/=) :: Timed a -> Timed a -> Bool #

Show a => Show (Timed a) 
Instance details

Defined in Control.Timer.Tick

Methods

showsPrec :: Int -> Timed a -> ShowS #

show :: Timed a -> String #

showList :: [Timed a] -> ShowS #

Generic (Timed a) 
Instance details

Defined in Control.Timer.Tick

Associated Types

type Rep (Timed a) :: Type -> Type #

Methods

from :: Timed a -> Rep (Timed a) x #

to :: Rep (Timed a) x -> Timed a #

type Rep (Timed a) 
Instance details

Defined in Control.Timer.Tick

creaTimer :: a -> a -> Integer -> Timed a #

A simple off/on timer expiring in fixed number of ticks.

Example:

timer = creaTimer Nothing (Just "Over!") 4
test t | isExpired t = print (fetchFrame t)
       | otherwise   = do print (fetchFrame t)
                          test (tick t)

   -- λ> test timer
   -- Nothing
   -- Nothing
   -- Nothing
   -- Nothing
   -- Just "Over"!

creaBoolTimer :: Integer -> Timed Bool #

Shorthand for: creaTimer False True i.

creaTimerLoop :: a -> a -> Integer -> Timed a #

A looped version of creaTimer.

creaBoolTimerLoop :: Integer -> Timed Bool #

Shorthand for: creaTimerLoop False True i.

fetchFrame :: Timed a -> a #

Fetches the current resource of the timer.

isExpired :: Timed a -> Bool #

Checks wheter the timer is expired (an expired timer will not respond to tick).

Animations

type Animation = Timed Plane Source #

An Animation is a series of timed time-separated Planes.

data Loop #

Number of times to repeat the animation.

Constructors

AlwaysLoop

Loops forever, never expires.

Times Integer ExpBehaviour

Repeats the cycle for a fixed number of times.

Instances
Eq Loop 
Instance details

Defined in Control.Timer.Tick

Methods

(==) :: Loop -> Loop -> Bool #

(/=) :: Loop -> Loop -> Bool #

Show Loop 
Instance details

Defined in Control.Timer.Tick

Methods

showsPrec :: Int -> Loop -> ShowS #

show :: Loop -> String #

showList :: [Loop] -> ShowS #

Generic Loop 
Instance details

Defined in Control.Timer.Tick

Associated Types

type Rep Loop :: Type -> Type #

Methods

from :: Loop -> Rep Loop x #

to :: Rep Loop x -> Loop #

type Rep Loop 
Instance details

Defined in Control.Timer.Tick

data ExpBehaviour #

Expire behaviour.

Constructors

Reach

Expires upon reaching last frame.

Elapse

Expires when last frame is over.

Instances
Eq ExpBehaviour 
Instance details

Defined in Control.Timer.Tick

Show ExpBehaviour 
Instance details

Defined in Control.Timer.Tick

Generic ExpBehaviour 
Instance details

Defined in Control.Timer.Tick

Associated Types

type Rep ExpBehaviour :: Type -> Type #

type Rep ExpBehaviour 
Instance details

Defined in Control.Timer.Tick

type Rep ExpBehaviour = D1 (MetaData "ExpBehaviour" "Control.Timer.Tick" "timers-tick-0.4.1.0-86aYijwsL07macaL4hOFs" False) (C1 (MetaCons "Reach" PrefixI False) (U1 :: Type -> Type) :+: C1 (MetaCons "Elapse" PrefixI False) (U1 :: Type -> Type))

tick :: Timed a -> Timed a #

Ticks the timer (one step).

reset :: Timed a -> Timed a #

Resets the timer to its original state.

getFrames :: Timed a -> [(Integer, a)] #

Return a list of all frames plus their duration.

Drawing

To get to the gist of drawing, check the documentation of %.

Plane

data Plane Source #

A two-dimensional surface (Row, Column) where to blit stuff.

Instances
Eq Plane Source # 
Instance details

Defined in Terminal.Game.Plane

Methods

(==) :: Plane -> Plane -> Bool #

(/=) :: Plane -> Plane -> Bool #

Show Plane Source # 
Instance details

Defined in Terminal.Game.Plane

Methods

showsPrec :: Int -> Plane -> ShowS #

show :: Plane -> String #

showList :: [Plane] -> ShowS #

Generic Plane Source # 
Instance details

Defined in Terminal.Game.Plane

Associated Types

type Rep Plane :: Type -> Type #

Methods

from :: Plane -> Rep Plane x #

to :: Rep Plane x -> Plane #

type Rep Plane Source # 
Instance details

Defined in Terminal.Game.Plane

type Rep Plane

type Coords = (Row, Column) Source #

Rows and Columns are 1-based (top-left position is 1 1).

type Width = Integer Source #

Expressed in Columns.

type Height = Integer Source #

Expressed in Rows.

blankPlane :: Width -> Height -> Plane Source #

Creates an empty, opaque Plane.

stringPlane :: String -> Plane Source #

Creates Plane from String, good way to import ASCII art/diagrams.

stringPlaneTrans :: Char -> String -> Plane Source #

Same as stringPlane, but with transparent Char.

makeTransparent :: Char -> Plane -> Plane Source #

Adds transparency to a plane, matching a given character

makeOpaque :: Plane -> Plane Source #

Changes every transparent cell in the Plane to an opaque ' ' character.

paperPlane :: Plane -> String Source #

A String (n divided and ended) representing the Plane. Useful for debugging/testing purposes.

planeSize :: Plane -> (Width, Height) Source #

Dimensions or a plane.

Draw

type Draw = Plane -> Plane Source #

A drawing function, usually executed with the help of %.

(%) :: Coords -> Plane -> Draw infixl 4 Source #

Pastes one Plane onto another. To be used along with & like this:

 d :: Plane
 d =          blankPlane 100 100  &
     (3, 4) % box '_' 3 5         &
     (a, b) % cell 'A' # bold

(#) :: Plane -> Draw -> Plane infixl 8 Source #

Apply style to plane, e.g.

cell 'w' # bold

(&) :: a -> (a -> b) -> b infixl 1 #

& is a reverse application operator. This provides notational convenience. Its precedence is one higher than that of the forward application operator $, which allows & to be nested in $.

>>> 5 & (+1) & show
"6"

Since: base-4.8.0.0

mergePlanes :: Plane -> [(Coords, Plane)] -> Plane Source #

Shorthand for sequencing Planes, e.g.

          firstPlane  &
 (3, 4) % secondPlane &
 (1, 9) % thirdPlane

is equal to

 mergePlanes firstPlane [((3,4), secondPlane),
                         ((1,9), thirdPlane)]

cell :: Char -> Plane Source #

A 1x1 cell.

box :: Char -> Width -> Height -> Plane Source #

A box of dimensions w h.

textBox :: String -> Width -> Height -> Plane Source #

A text-box. Assumes ' ' are transparent.

data Color #

ANSI's eight standard colors. They come in two intensities, which are controlled by ColorIntensity. Many terminals allow the colors of the standard palette to be customised, so that, for example, setSGR [ SetColor Foreground Vivid Green ] may not result in bright green characters.

Constructors

Black 
Red 
Green 
Yellow 
Blue 
Magenta 
Cyan 
White 
Instances
Bounded Color 
Instance details

Defined in System.Console.ANSI.Types

Enum Color 
Instance details

Defined in System.Console.ANSI.Types

Eq Color 
Instance details

Defined in System.Console.ANSI.Types

Methods

(==) :: Color -> Color -> Bool #

(/=) :: Color -> Color -> Bool #

Ord Color 
Instance details

Defined in System.Console.ANSI.Types

Methods

compare :: Color -> Color -> Ordering #

(<) :: Color -> Color -> Bool #

(<=) :: Color -> Color -> Bool #

(>) :: Color -> Color -> Bool #

(>=) :: Color -> Color -> Bool #

max :: Color -> Color -> Color #

min :: Color -> Color -> Color #

Read Color 
Instance details

Defined in System.Console.ANSI.Types

Show Color 
Instance details

Defined in System.Console.ANSI.Types

Methods

showsPrec :: Int -> Color -> ShowS #

show :: Color -> String #

showList :: [Color] -> ShowS #

Ix Color 
Instance details

Defined in System.Console.ANSI.Types

data ColorIntensity #

ANSI's standard colors come in two intensities

Constructors

Dull 
Vivid 
Instances
Bounded ColorIntensity 
Instance details

Defined in System.Console.ANSI.Types

Enum ColorIntensity 
Instance details

Defined in System.Console.ANSI.Types

Eq ColorIntensity 
Instance details

Defined in System.Console.ANSI.Types

Ord ColorIntensity 
Instance details

Defined in System.Console.ANSI.Types

Read ColorIntensity 
Instance details

Defined in System.Console.ANSI.Types

Show ColorIntensity 
Instance details

Defined in System.Console.ANSI.Types

Ix ColorIntensity 
Instance details

Defined in System.Console.ANSI.Types

color :: Color -> ColorIntensity -> Plane -> Plane Source #

Set foreground color.

bold :: Plane -> Plane Source #

Apply bold style to Plane.

invert :: Plane -> Plane Source #

Swap foreground and background colours of Plane.

Testing

testGame :: Game s -> [Event] -> s Source #

Tests a game in a pure environment. You can supply the Events yourself or use recordGame to obtain them.

setupGame :: Game s -> [Event] -> Game s Source #

As testGame, but returns Game instead of a bare state. Useful to fast-forward (e.g.: skip menus) before invoking playGame.

recordGame :: Game s -> FilePath -> IO () Source #

Play as in playGame and write the session to file. Useful to produce input for testGame and replayGame. Session will be recorded even if an exception happens while playing.

readRecord :: FilePath -> IO [Event] Source #

Reads a file containing a recorded session.

narrateGame :: Game s -> [Event] -> IO s Source #

Similar to testGame, runs the game given a list of Events. Unlike testGame, the playthrough will be displayed on screen. Useful when a test fails and you want to see how.

See this in action with cabal new-run -f examples alone-playback.

errorPress :: IO a -> IO a Source #

Wraps an IO computation so that any error gets displayed along with a <press any key to quit> prompt. Some terminals shut-down immediately upon program end: errorPress makes it easier to beta-test games on those terminals.

Cross platform

Good practices for cross-compatibility:

  • choose game dimensions of no more than 24 rows and 80 columns. This ensures compatibility with the trickiest terminals (i.e. Win32 console);
  • use ASCII characters only. Again this is for Win32 console compatibility, until this GHC bug gets fixed;
  • employ colour sparingly: as some users will play your game in a light-background terminal and some in a dark one, choose only colours that go well with either (blue, red, etc.);
  • some terminals/multiplexers (i.e. tmux) do not make a distinction between vivid/dull, do not base your game mechanics on that difference.