TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Is it possible to write games like Pac-Man in a functional language? (2008)

99 pointsby gus_leonelalmost 2 years ago

30 comments

raincolealmost 2 years ago
It&#x27;s probably not the author trying to say, but at a higher level a game is just:<p>new_game_state = simulate_a_frame(old_game_state, user_input, time_delta)<p>So of course it&#x27;s possible. Whether it&#x27;s practical is another question.
评论 #36549328 未加载
评论 #36549083 未加载
评论 #36549835 未加载
评论 #36549574 未加载
评论 #36548775 未加载
评论 #36550405 未加载
评论 #36551993 未加载
评论 #36551211 未加载
trompalmost 2 years ago
A Quake clone written in Haskell [1] suggests that it is quite possible. That&#x27;s just one of the games listed on [2], along with game engines and libraries.<p>[1] <a href="https:&#x2F;&#x2F;wiki.haskell.org&#x2F;Frag" rel="nofollow noreferrer">https:&#x2F;&#x2F;wiki.haskell.org&#x2F;Frag</a><p>[2] <a href="https:&#x2F;&#x2F;wiki.haskell.org&#x2F;Applications_and_libraries&#x2F;Games" rel="nofollow noreferrer">https:&#x2F;&#x2F;wiki.haskell.org&#x2F;Applications_and_libraries&#x2F;Games</a>
评论 #36549144 未加载
评论 #36551795 未加载
agentultraalmost 2 years ago
Totally possible. Here’s a link to a game recently released on Steam that is written in Haskell and whose source is open: <a href="https:&#x2F;&#x2F;github.com&#x2F;incoherentsoftware&#x2F;defect-process">https:&#x2F;&#x2F;github.com&#x2F;incoherentsoftware&#x2F;defect-process</a><p>I recently hacked together an asteroids clone in Haskell with SDL2 and not much else. It’s not super pretty but it works.<p>I’ve talked to folks who’ve been using the newer effects libraries taking advantage of the new delimited continuation primops in GHC 9.6 for their game dev. Even with very high level libraries their reporting acceptable performance.<p>With enough dedication I’m certain anyone could make whatever game they wanted in an FP language.
评论 #36552083 未加载
评论 #36553252 未加载
epolanskialmost 2 years ago
Functional core + imperative shell can describe any kind of problem.<p>The biggest problem with the approach is that mainstream functional languages put most effort in the functional core rather than the imperative shell modeling.<p>Effect systems (ZIO for Scala, effect-ts) are the only places where fibers allow composing, scheduling and managing &quot;processes&quot; in a functional way.
FrustratedMonkyalmost 2 years ago
It is completely possible of course.<p>I think questions like this are really getting at, is how do you update the &#x27;huge-game-state&#x27; without mutating it, changing variables.<p>People have a very ingrained feeling that the game state is updated by updating variables.<p>But in functional languages, the state is immutable, so if you are new to functional languages you ask &#x27;man how do I update this&#x27;.<p>Then, if this new person takes next step and is told &#x27;you make a copy of the game state&#x27;, they are like &#x27;man that must be a big performance hit to copy the whole thing&#x27;.<p>So I think questions like this are really about how to handle a state with immutable data. How to make a copy efficiently, like in parts, or whatever. And then of course, you are down the rabbit whole of changing your brain thinking towards functional style.
评论 #36549647 未加载
评论 #36549484 未加载
评论 #36552704 未加载
dahartalmost 2 years ago
If there was one single thing I learned from my functional programming class in college, it’s that there exists a way to implement imperative programming in a pure functional language. I distinctly remember the aha that Turing complete means Turing complete, and functional vs imperative is a style that can be emulated in any Turing complete language. Now for the life of me I can’t remember what the exact trick or mechanism was, does anyone know what I’m talking about? I’m pretty sure it leveraged one of the named functional constructs, similar to a thunk or y-combinator or something. I’m old enough that use of “monad” wasn’t much of a thing at the time.
评论 #36563054 未加载
评论 #36551078 未加载
评论 #36553545 未加载
评论 #36550763 未加载
megapolissalmost 2 years ago
Fable [1] (F# to JS compiler) has pac-man game as one of samples [2]<p>[1] <a href="https:&#x2F;&#x2F;fable.io&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;fable.io&#x2F;</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;fable-compiler&#x2F;repl&#x2F;blob&#x2F;master&#x2F;public&#x2F;samples&#x2F;games&#x2F;pacman.fs">https:&#x2F;&#x2F;github.com&#x2F;fable-compiler&#x2F;repl&#x2F;blob&#x2F;master&#x2F;public&#x2F;sa...</a>
egonschielealmost 2 years ago
Hey, it&#x27;s my time to shine! I rewrote Chips Challenge in Haskell years ago: <a href="https:&#x2F;&#x2F;github.com&#x2F;egonSchiele&#x2F;chips">https:&#x2F;&#x2F;github.com&#x2F;egonSchiele&#x2F;chips</a><p>It&#x27;s not <i>better</i> than using C, but worked just fine when I wrote it in 2014. I&#x27;d say, unless you&#x27;re planning to sell it, write the game in whatever language you enjoy.
garethrowlandsalmost 2 years ago
I like this. He shows some of the basic ideas of functional reactive programming and of entity component systems. And then the thing about large objects not being efficient to update, well it&#x27;s not efficient in C either, or in databases or in anything really. Understanding how data transforms and the dependencies between data is usually A Good Thing.
arunixalmost 2 years ago
The other parts:<p>Part 2: <a href="https:&#x2F;&#x2F;prog21.dadgum.com&#x2F;24.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;prog21.dadgum.com&#x2F;24.html</a><p>Part 3: <a href="https:&#x2F;&#x2F;prog21.dadgum.com&#x2F;25.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;prog21.dadgum.com&#x2F;25.html</a><p>Part 4: <a href="https:&#x2F;&#x2F;prog21.dadgum.com&#x2F;26.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;prog21.dadgum.com&#x2F;26.html</a><p>Follow up: <a href="https:&#x2F;&#x2F;prog21.dadgum.com&#x2F;37.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;prog21.dadgum.com&#x2F;37.html</a>
评论 #36548811 未加载
评论 #36549550 未加载
aputsiakalmost 2 years ago
In Conrad Barski&#x27;s book, Land of Lisp, you are taught to do so. Check out the website: <a href="http:&#x2F;&#x2F;landoflisp.com" rel="nofollow noreferrer">http:&#x2F;&#x2F;landoflisp.com</a>
评论 #36548891 未加载
marcelralmost 2 years ago
In university I had a mind bending experience writing snake in purely functional javascript, I was so proud of it and showed my friends and the reaction was “oh god”<p>I can’t say they were wrong
opyatealmost 2 years ago
Just to pitch in with a Clojurescript&#x2F;Quill example, where the game loop takes a function that takes game state, modifies it, and returns it for the next iteration of the game loop.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;opyate&#x2F;minild72&#x2F;blob&#x2F;master&#x2F;src&#x2F;pong&#x2F;core.cljs#L59">https:&#x2F;&#x2F;github.com&#x2F;opyate&#x2F;minild72&#x2F;blob&#x2F;master&#x2F;src&#x2F;pong&#x2F;core...</a>
CJeffersonalmost 2 years ago
I’ve heard Edwin Brady, who made Idris (and Whitespace…) use the term “pac-man complete” when discussing languages, particularly functional ones.
joeld42almost 2 years ago
I&#x27;ve tried this a lot, and I will say no, it&#x27;s not realistically possible if you&#x27;re being a purist.<p>The simple counterexample, take setpixel, which takes an image buffer, and returns an image buffer with one pixel changed. Now imagine having a large bitmap and using setpixel to draw a sprite to it. For every non-transparent pixel in the sprite you need a whole copy of the whole buffer. Obvs there are lots of improvements and cleverness but compared to the speed and simplicity of an imperative code just modifying an image buffer in-place it&#x27;s vastly slower, more complicated or both.<p>However you can manage to write &quot;most&quot; of your game in a functional way and if you&#x27;re careful about it, and use tricks like copy-on-write to make some things act functional, you can do it. Writing your game in a functional mindset, and keeping data immutable when you can has a lot of benefits.
评论 #36553103 未加载
评论 #36553138 未加载
评论 #36556221 未加载
bitwizealmost 2 years ago
There&#x27;s Monadius, a clone of Gradius written in Haskell: <a href="https:&#x2F;&#x2F;github.com&#x2F;tanakh&#x2F;monadius">https:&#x2F;&#x2F;github.com&#x2F;tanakh&#x2F;monadius</a>
simonmicalmost 2 years ago
Some Haskell pacmans:<p>- <a href="https:&#x2F;&#x2F;github.com&#x2F;EgorDm&#x2F;fp-pacman">https:&#x2F;&#x2F;github.com&#x2F;EgorDm&#x2F;fp-pacman</a> (<a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=DlifcJ4cexw">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=DlifcJ4cexw</a>)<p>- <a href="https:&#x2F;&#x2F;github.com&#x2F;Jcklly&#x2F;PacMan">https:&#x2F;&#x2F;github.com&#x2F;Jcklly&#x2F;PacMan</a><p>- <a href="https:&#x2F;&#x2F;github.com&#x2F;ajkavanagh&#x2F;pacman">https:&#x2F;&#x2F;github.com&#x2F;ajkavanagh&#x2F;pacman</a><p>and other games:<p>- <a href="https:&#x2F;&#x2F;wiki.haskell.org&#x2F;Applications_and_libraries&#x2F;Games" rel="nofollow noreferrer">https:&#x2F;&#x2F;wiki.haskell.org&#x2F;Applications_and_libraries&#x2F;Games</a><p>See also the #haskell-game chat on Matrix and Libera IRC.
rydgelalmost 2 years ago
I remember making Flappy Bird in Haskell with FRP. It was a very different but pleasant experience.
giovannibonettialmost 2 years ago
I think the most fun and behinner-approachable way to build games with functional programming is with Elm [1].<p>See a few (small, demo) games built by the community in [2] .<p>Notice Elm has abandoned the FRP approach in favor of Model-View-Update [3].<p>[1] <a href="https:&#x2F;&#x2F;elm-lang.org&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;elm-lang.org&#x2F;</a> [2] <a href="https:&#x2F;&#x2F;github.com&#x2F;rofrol&#x2F;elm-games">https:&#x2F;&#x2F;github.com&#x2F;rofrol&#x2F;elm-games</a> [3] <a href="https:&#x2F;&#x2F;elm-lang.org&#x2F;news&#x2F;farewell-to-frp" rel="nofollow noreferrer">https:&#x2F;&#x2F;elm-lang.org&#x2F;news&#x2F;farewell-to-frp</a>
vogon_laureatealmost 2 years ago
TIL that a 6502 didn’t have division and remainder instructions. Yikes.
评论 #36548612 未加载
评论 #36553640 未加载
评论 #36548543 未加载
评论 #36548972 未加载
评论 #36550633 未加载
评论 #36549672 未加载
asimpletunealmost 2 years ago
Part of me questions the old view that functional programming isn’t performant.<p>If you think about it, 1.) most performance comes down to memory access. 2.) It is widely known that accessing memory off the stack is faster than allocating memory on the heap. 3.) calling a function pushes everything onto the stack, starts a new stack pointer, and when it’s done releases everything from the stack.<p>So my thinking is if by definition functional programming is about organizing your program as a pure composition of functions, doesn’t that also imply an ideal memory access pattern? Can someone tell me where I&#x27;m wrong?<p>I guess the one issue where this comes into doubt is with strings of unknown length, and other data types that must be heap allocated, but even in those cases it seems that problem would equally affect all other paradigms.
评论 #36551241 未加载
评论 #36550590 未加载
评论 #36552938 未加载
innocentoldguyalmost 2 years ago
It is both possible and relatively easy. I wrote a game similar to Pac-Man using Elixir, only I used characters from Firefly instead. I used it in several job interviews to prove I can code.
hgsgmalmost 2 years ago
A game as tiny as pacman is trivial to make run fast enough even with the most naive functional style like rebuilding the entire game state every frame.
sphalmost 2 years ago
Should add (2008) to the title.
评论 #36548837 未加载
mock-possumalmost 2 years ago
… would it be presumptive of me to think that there are <i>no</i> games which could not be written in a functional language?<p>I mean like… what’s stopping you?
aask_almost 2 years ago
I mean... I wrote OOP tetris in PowerShell, so I&#x27;d say yes? github.com&#x2F;aask42&#x2F;particleTetris
ziml77almost 2 years ago
I want to know if it can be done such that it performs just as well as the original on the original hardware.
donaldboughalmost 2 years ago
Awesome write up. Great seeing a talk on functional programming with more technical details and examples.
NikkiAalmost 2 years ago
Pretty sure I remember a FRP pacman being written in response to those post, way back then in 2008.
foobarbecuealmost 2 years ago
I enjoyed this article. Is there source code to accompany it?