8 ms·
Writing space invaders with Go
- JoeAltmaier 9y agoCool, and a great programming tutorial! I was thinking I might see Go in a device-control application, but alas not. The program uses a modern computer's video system. The original Space Invaders code likely had to shift pixels out to the video display in realtime. So a different programming feat entirely, but still cool!
- jacquesm 9y agoThe Sinclair ZX80 worked like that. As a result of which if you pressed a key the video would blank.
- stuartmalcolm 9y ago..only in fast mode iirc
- sly010 9y agoI have used go in an embedded application (raspberrypi) in a soft realtime manner controlling and LCD over I2C and staying under a 1.5ms looptime. The hardest part is to undo all the things the kernel does and get access to memory so you can do memory based io, but luckily the go stdlib has everything you need to make it happen.
- makapuf 9y agoWell for this kind of things, bare metal and C are hard to beat. Is bare metal go possible?
- weberc2 9y agoThere are projects which modify Go to allow it to run on bare metal. I don't recall any of the names though and I can't speak to their quality.
- kqr 9y agoEven for soft realtime I'd much sooner reach for Ada than C...
- makapuf 9y agoInteresting, can you develop why ?
- kqr 9y agoSure! There are a few major reasons: - The built-in concurrency primitives are designed with real time systems in mind, freeing you from the need of constructing a cyclic executive and scheduling manually. - The tools to deal with time constraints and diagnose why deadlines were not met are very high quality. The Ravenscar profile essentially guarantees that all scheduling decisions are known statically. - Not strictly real time related, but since real time systems are often safety priority systems I'll mention it anyway: Ada makes memory management hard to get wrong. If you're interested, I think Programming Real-time with Ada 2005 over at Embedded.com[1] provides a really good overview. Many of those features are meant for safety critical hard deadline systems and are consequently overkill for bedroom experiments, but for those projects you can just take the default basics and get 98% of the way there. (If the article seems outdated in some aspects, recall that there were some further imptovements to the standard in 2012.) [1]: https://www.embedded.com/design/prototyping-and-development/4025713/Programming-real-time-with-Ada-2005 https://www.embedded.com/design/prototyping-and-development/...
- jsd1982 9y agoNice! I have a project of my own [1] in a similar vein although not as low level as yours. Using an RPi3 with the official TFT LCD and touchscreen and rendering graphics via OpenVG. Getting about 45fps (~22ms per frame) on the Pi itself to render an 800x480 UI filled with some round-rect buttons, rotary dials, and text. Rolled my own very basic UI controls library and cross-platform host isolation library [2] to achieve this. Tested and working on darwin, win64, and raspberry pi 3. [1] https://github.com/JamesDunne/axewitcher-ovg https://github.com/JamesDunne/axewitcher-ovg [2] https://github.com/JamesDunne/golang-openvg https://github.com/JamesDunne/golang-openvg
- takumo 9y agoLooks cool but really should mention early on that it doesn't use SDL or OpenGL to render the game but uses a specific feature of iTerm2 to render a series of images in the terminal, on other terminals it just shows a black screen.
- nottorp 9y agoOuch. Not so interesting after all then. Does Go even have some "standard" SDL bindings? I was googling the other day (with the aim of learning Go through doing something fun) and I found like 6 results only on the first page. Didn't go and try any of them because to be honest that's depressing... i bet that if there are 6 different bindings none is feature complete. Is Go useful just for server side stuff then?
- sesteel 9y agohttps://github.com/go-gl https://github.com/go-gl has opengl and glfw bindings which would serve many of the same purposes.
- nottorp 9y agoSee? That's the problem. Two different bindings recommended already. Better than 6 tbh, i might look at those 2. And to the twitch guy I'm sorry, I'm old enough to prefer written documentation :)
- darethas 9y agoThis is awesome, and something I was doing as well for fun! Go's image package has some pretty nice primitives. One thing I think would make this even simpler is you could simply sequence the individual sprites using image.NewRGBA and simply setting the Pix slice to the values to draw the appropriate images, then you can avoid going to the os at all!
- JustSomeNobody 9y agoAs someone who is just learning Go, this is a really cool bite-size project to read and learn from. Very cool!
- JayOtter 9y agoFor anyone interested in making little games in Golang, Termloop[1] is a terminal-based engine (though it uses text to render rather than this iTerm-specific feature). [1] https://github.com/joelotter/termloop https://github.com/joelotter/termloop
- citeguised 9y agoAlso, for non terminal-based things, Raylib is really nice. It's originally a C-based games-library, but the bindings work well for playing around in Golang. https://github.com/gen2brain/raylib-go https://github.com/gen2brain/raylib-go
- crehn 9y agoFWIW, here's a single-file terminal snake in Go: https://github.com/hoffa/snake https://github.com/hoffa/snake
- hu3 9y agoNeat. This one works on Linux too.
- krapp 9y agoDupe - posted yesterday by the original project author, but with only two comments[0] I think a moderator should consider merging this thread with the original, if possible. [0]https://news.ycombinator.com/item?id=16287030 https://news.ycombinator.com/item?id=16287030
- masswerk 9y agoIt may be worth noting that the sprites are registered trademarks of Taito. So be careful… (I learned this the hard way: I once had a rather poplar 404 page featuring a variant of the game [1], until I received a cease and desist letter from Taito…) [1] Compare: https://gizmodo.com/this-space-invaders-404-page-is-the-funnest-404-error-p-510768455 https://gizmodo.com/this-space-invaders-404-page-is-the-funn...
- scottlu2 9y agoWhat year did you receive the cease and desist and was it from Brinks Hofer / Brinks Gilson & Lione?
- masswerk 9y agoSo, I just had a look: Oct 2013, processed by a local attorney (Vienna/Austria). This was following to the page gaining some traction and popular attention in Japan. (Without intending any interpretation of this: I believe, in principle, you have to defend a trademark in order to uphold the rights – and at this point it was well in their radar.)
- rileyphone 9y agoMy understanding is that Go's ffi overhead[1] makes game development impractical at a certain level, due to the amount of opengl calls you have to make. Is this still the case? [1] https://github.com/dyu/ffi-overhead/blob/master/README.md https://github.com/dyu/ffi-overhead/blob/master/README.md
- gameswithgo 9y agoThat test was using Go 1.8, did 1.9 introduce any improvement? There appear to be improvements coming for 1.10 and 1.11: https://github.com/golang/go/issues/14939 https://github.com/golang/go/issues/14939 You don't necessarily have to make a large number of calls into C per frame when using opengl though, I'm not sure it would be a deal breaker.
- gameswithgo 9y agoLooks like no, no substantive improvement in 1.9
- Xeoncross 9y agoI re-tested with 1.9 and got: # Go 500000000 in 53058 ms # C 500000000 in 1989 ms
- gameswithgo 9y agograb the 1.10 beta and try?? =)
- Narishma 9y agoWouldn't that only be an issue for high-end AAA games though?
- gameswithgo 9y agoJust doing some napkin math. It would be about 13,000 calls into C per frame, to waste 1 ms with call overhead. 1ms would still be a lot when you only have ~16ms to work with,but I don't think even AAA games make within an order of magnitude of that many opengl calls per frame? Even writing in pure C you want to batch things as much as possible. I think typical is well under 1000 calls per frame, but I've only got experience with small hobbyist projects.
- AndrewOMartin 9y agoThe invaders positions shouldn't all be updated every cycle, they should be updated one at a time, or in a separate thread, this is what the arcade machine does and due to its limited processing speed this naturally leads to the aliens speeding up as fewer aliens remain. This implementation starts with the aliens going at what might be considered "full speed", and is probably much more difficult. https://www.giantbomb.com/space-invaders/3030-5099/ https://www.giantbomb.com/space-invaders/3030-5099/
- shoshino 9y agohttps://github.com/asib/spaceinvaders https://github.com/asib/spaceinvaders / https://news.ycombinator.com/item?id=10078928 https://news.ycombinator.com/item?id=10078928
- wmiel 9y agoHere's also a cool example of a game written in go, although using JS to display GUI: http://blog.u2i.com/we-made-a-multiplayer-browser-game-in-go-for-fun/ http://blog.u2i.com/we-made-a-multiplayer-browser-game-in-go...