TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Space Invaders in C

478 点作者 loadzero超过 5 年前

24 条评论

loadzero超过 5 年前
Author here.<p>This is a bit of a love letter to Space Invaders, and video games in general.<p>I started working on this as a simple emulation project, to rekindle my own passion in low level video game hacking, but then realized with a bit of care I could take it further.<p>So, this is not a simple clone of the game, but rather a painstaking recreation of the source code in clean, readable C code.<p>I wanted to make something nice that would last a while, as a tribute to the original, and hopefully function a bit like a rosetta stone for future audiences.<p>Enjoy.
评论 #21726884 未加载
评论 #21724515 未加载
vagab0nd超过 5 年前
Story time: I wrote an 8080 emulator to play the Space Invaders. I implemented just enough instructions so I could play the game. Problem was, the game&#x27;s title screen would show, but after a few seconds, it would immediately jump back to the beginning.<p>First I thought &quot;oh I must have implemented an instruction wrong&quot;. So I re-checked all the instructions, read through the Intel 8080 programming manual multiple times. I did find a few errors related to the carry flag, but fixing them didn&#x27;t change anything.<p>I then started to actually debug the game code. This was significantly harder than I&#x27;d expected. Since the game was in assembly, it was not obvious what each instruction did. What I had to do was pretty much annotate each and every instruction, along with all the memory locations. E.g. address 0xABCD is for player score, 0xCDEF is for player position, instruction X is for drawing the bunker, etc.<p>It was truly a pain, but it paid off. So the game had an interrupt handler registered to the display refresh signal. And I finally realized the game was constantly interrupted when it was not supposed to. Turns out I had forgotten to disable the signal when entering the handler.<p>I fixed it, and it ran perfectly.
nikconwell超过 5 年前
Great write up, thanks. I was intrigued by the &quot;tilt&quot; keymapping on the last line of your posting. <a href="https:&#x2F;&#x2F;computerarcheology.com&#x2F;Arcade&#x2F;SpaceInvaders&#x2F;" rel="nofollow">https:&#x2F;&#x2F;computerarcheology.com&#x2F;Arcade&#x2F;SpaceInvaders&#x2F;</a> notes:<p>&gt; In the early eighties you would have found the Space Invaders cabinet in an arcade right next to the pinball machines. So a &quot;tilt&quot; switch, like you would find in a pinball machine, would not have seemed as strange as it does today. If you shake, slap, or otherwise physically abuse an SI cabinet you will get a TILT message and your game will end.
评论 #21722145 未加载
评论 #21720820 未加载
jacquesm超过 5 年前
Interesting metrics, you&#x27;d expect C to do a lot better in the line-count department than assembly.<p>I tried building it on Ubuntu, if you follow the instructions the SDL library include files will end up in a directory called SDL so you have to include SDL&#x2F;SDL.h and even then the build fails with lots of SDL related definitions missing (SDL_Window for instance).<p>That&#x27;s because you really should be doing<p><pre><code> sudo apt-get install libsdl2-dev </code></pre> Then change the include file line to<p><pre><code> #include &lt;SDL2&#x2F;SDL.h&gt; </code></pre> and type:<p><pre><code> make </code></pre> The roms can be found here:<p><a href="http:&#x2F;&#x2F;www.freevintagegames.com&#x2F;MAME&#x2F;invaders.php" rel="nofollow">http:&#x2F;&#x2F;www.freevintagegames.com&#x2F;MAME&#x2F;invaders.php</a><p>After downloading you&#x27;ll have to rename the files because the names will all be uppercase:<p><pre><code> cd inv1 mv INVADERS.E invaders.e mv INVADERS.F invaders.f mv INVADERS.G invaders.g mv INVADERS.H invaders.h cd .. </code></pre> Now the game should work:<p><pre><code> .&#x2F;bin&#x2F;si78c </code></pre> Some minor nitpicks about the code:<p>- bracket your ifs and place the starting { on the same line as if&#x2F;while, or one day you&#x27;ll sit there staring at the screen for 8 hours trying to figure out why your code no longer works due to an accidentally deleted line.<p>So:<p><pre><code> while (num &lt; 64) { int has = SDL_PollEvent(&amp;event_buffer[num]); if (!has) break; num++; } </code></pre> becomes:<p><pre><code> while (num &lt; 64) { if (SDL_PollEvent(&amp;event_buffer[num])) { break; } num++; } </code></pre> Neat project!
评论 #21720573 未加载
评论 #21722211 未加载
评论 #21720027 未加载
评论 #21721833 未加载
评论 #21720991 未加载
评论 #21719986 未加载
评论 #21724978 未加载
评论 #21722763 未加载
Tepix超过 5 年前
If you like this kind of thing, there&#x27;s also a faithful re-implementation of the original Elite game, with (readable) source code.<p>Quoting Wikipedia:<p>&quot;... <i>around 1999 Christian Pinder developed Elite: The New Kind as a modern PC port of the original BBC Micro version. He achieved a faithful port by reverse-engineering the original assembly written BBC Micro version and recreating a platform neutral C code variant from it, but at David Braben&#x27;s request this version was withdrawn from distribution in 2003. In September 2014, on Elite&#x27;s 30th birthday, Ian Bell blessed Elite: The New Kind and re-released it for free on his website. Since then, Elite: The New Kind is also distributed again in version 1.1 by Christian Pinder; a source code mirror is hosted on GitHub.</i>&quot;<p>Link: <a href="https:&#x2F;&#x2F;github.com&#x2F;fesh0r&#x2F;newkind" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;fesh0r&#x2F;newkind</a>
评论 #21723180 未加载
ZeroGravitas超过 5 年前
The white invaders on a black background was iconic in my childhood, so I was kind of blown away when I first saw a real arcade machine with the painted backdrop and the screen reflected over it with a half silvered mirror (or whatever crazy tech they used in those days).
评论 #21723798 未加载
guiambros超过 5 年前
Amazing work, thank you for sharing! The amount of detail is prey impressive.<p>How hard would it be to port the sound as well?
评论 #21719873 未加载
theunamedguy超过 5 年前
Excellent work! Clean C is underrated nowadays.
i_am_not_elon超过 5 年前
Both the project and the writeup are awesome, Jason! Thank you for sharing - I enjoyed it!
arrakeen超过 5 年前
see also Cannonball, which I discovered recently—a reimplementation and improvement of OutRun<p><a href="https:&#x2F;&#x2F;github.com&#x2F;djyt&#x2F;cannonball" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;djyt&#x2F;cannonball</a>
jimws超过 5 年前
This is an awesome project with awesome write up. I miss the days of pixelated console games. The constrained environment led to so much creativity. Are there any new games like this being written anymore?
评论 #21721201 未加载
zoomablemind超过 5 年前
Nice job! Such a pleasure to read a clean C code.<p>BTW, in case anyone tries to build it on Ubuntu 14.04 x86 with gcc, you&#x27;d need `-std=gnu99 -D_GNU_SOURCE` flags, otherwise it barks about stack_t in ucontext.
评论 #21725568 未加载
FullyFunctional超过 5 年前
I love it. Thanks for doing this.<p>How long did it take you? How did you verify the memory accuracy? It sounds like you co-simulated your implementation with the emulation?
评论 #21719761 未加载
jonny383超过 5 年前
Hats off, very cool!!!<p>I have always dreamed of building a classic ROM to C transpiler for native, portable, future-proof(ish) binary builds (just like this).
ianai超过 5 年前
Oh I really appreciate having a neat, complicated C program to explore and learn from!<p>Could this be done for Galaga? That’s my personal favorite.
评论 #21721453 未加载
jonathanzufi超过 5 年前
This is magnificent. Thank you for doing this.
ddtaylor超过 5 年前
&gt; Error code: MOZILLA_PKIX_ERROR_SELF_SIGNED_CERT<p>Not accessible where HTTPS is required here is a mirror that has SSL <a href="https:&#x2F;&#x2F;archive.is&#x2F;E6y37" rel="nofollow">https:&#x2F;&#x2F;archive.is&#x2F;E6y37</a>
bachmeier超过 5 年前
Is this open source? I don&#x27;t see a license with the code, but maybe I missed it.
评论 #21720510 未加载
avip超过 5 年前
Was able to build for alpine, but I get a segfault on running.
hoseja超过 5 年前
Too bad that for a lot of newer games, the source will never be released. We need abandonware Indiana Jones.
评论 #21720175 未加载
joosters超过 5 年前
A build process of ‘make 2&gt;&#x2F;dev&#x2F;null’ is an ominous sign for any project...
评论 #21720009 未加载
rootVIII超过 5 年前
really cool
thenewnewguy超过 5 年前
Somewhat off-topic rant, but why do websites display long videos as GIFs? Is there some size or compatibility benefit? Because it&#x27;s really annoying to not be able to pause or rewind these GIFs that take the place of where a video would usually be.<p>Also, get an HTTPS certificate friend, it&#x27;s 2019. Dreamhost provides LE certs literally for free.
评论 #21719836 未加载
评论 #21720916 未加载
评论 #21719750 未加载
评论 #21719787 未加载
app4soft超过 5 年前
&gt; as long as they support <i>ucontext</i> and <i>SDL2</i><p>Why not SDL1.x? It would be more portable,<p>What about Symbian support?[0]<p>[0] <a href="http:&#x2F;&#x2F;anotherguest.se&#x2F;" rel="nofollow">http:&#x2F;&#x2F;anotherguest.se&#x2F;</a>