# Haskell: Game Programming with GIF Streams

DevFeed: [Haskell: Game Programming with GIF Streams](<https://devfeed.tech/articles/haskell-game-programming-with-gif-streams-30819.md>)

Original publisher: [Read original article](<https://hookrace.net/blog/haskell-game-programming-with-gif-streams/>)

Published: 2023-09-06T22:00:00Z

Content type: tutorial

Language: en

Sources: [Dennis Felsing](<https://devfeed.tech/sources/dennis-felsing.md>)

Topics: [Haskell](<https://devfeed.tech/topics/haskell.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Code](<https://devfeed.tech/topics/code.md>), [image animation](<https://devfeed.tech/topics/image-animation.md>), [HTTP](<https://devfeed.tech/topics/http.md>), [browser](<https://devfeed.tech/topics/browser.md>)

Tags: [animation](<https://devfeed.tech/tags/animation.md>), [browser](<https://devfeed.tech/tags/browser.md>), [gif](<https://devfeed.tech/tags/gif.md>), [haskell](<https://devfeed.tech/tags/haskell.md>), [http](<https://devfeed.tech/tags/http.md>), [programming](<https://devfeed.tech/tags/programming.md>)

## AI overview

A tutorial for implementing the Snake game in Haskell using the gifstream framework. The game generates animated GIF frames through an HTTP server, displays them in a browser, and accepts WASD input from the terminal.

## Source excerpt

Note: This is translated from the German original, which was used as a homework for the Programming Paradigms course at Karlsruhe Institute of Technology a long long time ago when I was co-holding the practical courses. Snake is a computer game in which a snake has to be moved through a playing field. Eating food increases the snake's length. When the snake collides with a wall or itself the game ends. In this homework you will implement Snake in Haskell. For this you will require the framework from the gifstream repository. The output of the game happens in an animated GIF stream, which you can watch in your browser. 64 colors are supported, which can be respresented as Int tuples of (0,0,0) up to (3,3,3). type RGB = (Int,Int,Int) A single frame of a GIF is defined as a list of rows, wherein each row is a list of RGB values. type Frame = [[RGB]] The framework provides a \texttt{server} function, which runs an HTTP server on the supplied port. The server sends each client a new frame of the GIF animation at the set interval. In the passed logic function new frames will be generated dynamically. server :: PortNumber -> Int -> Logic -> IO () The Snake.hs file contains the basis for writing the Snake game. Compile the game and run it (You'll need to install the network and random Haskell packages too): $ ghc -O3 -threaded Snake.hs [1 of 2] Compiling GifStream ( GifStream.hs, GifStream.o ) [2 of 2] Compiling Main ( Snake.hs, Snake.o ) Linking Snake ... $ ./Snake Listening on http://127.0.0.1:5002/ Open the supplied address in a browser. By pressing the WASD keys in the terminal you can influence the GIF in your browser. Other participants in your network can watch the GIF stream as well, by using your network IP address instead of 127.0.0.1. Furthermore it is possible to record the GIF stream and watch it later: wget -O game.gif http://127.0.0.1:5002/ The most important function in Snake.hs is logic: logic wait getInput sendFrame = initialState >>= go where go (State ol