10 ms·
Simple Minecraft Clone in 580 lines of Python
- doctoboggan 14y agoThis is incredibly inspiring for young kids. I am going to study this so I can teach it to my younger brother who loves Minecraft and is trying to learn Python.
- Millennium 14y agoNice. Next stop: Dwarf Fortress.
- arsey 14y agoSimple Dwarf Fortress Clone in 58000 lines of Python
- deleted 14y ago[deleted]
- codewright 14y agoLispers that preach of concision mere mortals cannot possibly dream of, this is your call to arms.
- cturner 14y agoI mess around with roguelike development and have been migrating from python to scheme for brevity. I started with python. I've had lots of problems with it, but I'm far stronger in python than anything else, so have been in a sense stuck on it despite separate attempts over the last twelve months to get to momentum in C, C++ and racket. During the recent 7-day roguelike challenge I found myself still stuck on my old python codebase, now creating DSLs in order to define entities and relationship opportunities between them (e.g. water -> planted_seed = sapling). I found I was able to significantly reduce code size, it's cleaner, and it's far sturdier. That was the point where I realised I needed to drop everything and make the jump to scheme, which I've been doing since. This derailed my 7-day roguelike effort a couple of weeks ago, but it's progress. OT, but another benefit I've found is that it's trivial to write zero-dependency, multiplatform code in racket. "raco exe main.rkt". Whereas getting that done with with python is fiddly. With the previous game I released, I spent more time trying to get exes for the major platforms than I did on everything else combined.
- sudont 14y agoI think that would actually be great for an introduction to actor-based modeling. That is, if you think that it's a valid discipline. SimPy looks interesting, it would definitely be a more "pure" approach than scripting RePast. http://simpy.sourceforge.net/ http://simpy.sourceforge.net/
- j2kun 14y agoI used a variant of this code for a project for my students. Just had to clean it up a bit and organize the code into parts they should be reading (game logic) and parts they should ignore unless they're really curious (mostly the OpenGL stuff).
- doctoboggan 14y agoIf possible could you post a link to your code? I was going to go through and pep8/document everything but if you've already done that it would be awesome to see.
- tripzilch 14y agoPlease, if you have a link or would agree to send us a copy on request, that would be amazing. I haven't seen the code yet, so I don't know how badly it is needed, but I am definitely going to use this project for teaching programming. And if you already have a cleaned-up version, that would be a great help. If, for some reason you don't want to link the code here, I put my email in my profile info.
- codygman 14y agoPlease put this online, it'll be a very very valuable resource.
- cdelsolar 14y agoThis is really freaking cool. I can't believe how short the code is.
- josephagoss 14y agoHow are the graphics being drawn? Is Pyglet being used to so this?
- gamegoblin 14y agoPyglet provides native windowing and a full OpenGL wrapper.
- scottrogowski 14y agoMore than 100 lines of this are textures! Got this error though. Any ideas? OSError: dlopen(/System/Library/Frameworks/QuickTime.framework/QuickTime, 6): no suitable image found. Did find: /System/Library/Frameworks/QuickTime.framework/QuickTime: mach-o, but wrong architecture /System/Library/Frameworks/QuickTime.framework/QuickTime: mach-o, but wrong architecture
- TkTech 14y agoIt was built with an old version of pyglet trying to run on 64bit python. Update.
- grimgrin 14y agoUnless I'm just terribly mistaken, good luck getting pyglet to run on OS X right now. However, I really hope I am wrong, because that means I was just being a fool last week and will soon be using pyglet.
- TrevorJ 14y agoThat's sad to hear - I ran in to that last week with pygame (can't run it in a 64 bit environment on OS X).
- macmac 14y agoLooks like pyglet now runs on OS X: http://www.pyglet.org/news.html http://www.pyglet.org/news.html
- TkTech 14y agoPyglet is very slow for releases, it's almost always better to build it right out of hg (pip install hg+https://pyglet.googlecode.com/hg/ https://pyglet.googlecode.com/hg/). It's had Cocoa support for something like a year, at least.
- slunk 14y agoI got a similar error. "arch -i386 python main.py" solve the problem for me.
- rodly 14y agoIf someone were to make something like this a step-by-step tutorial/template appropriate for a classroom setting it would be huge. Imagine a class at High School where you start off learning the basics of Python for the first few weeks and the rest of the semester is spent writing the chunks of code into a template that has lots of notes for guidance. I'd image something like : def setDefaultBlockColor(color): # recall that this is a function that takes in a variable called 'color' as its argument, write the code that will set the current game files configuration file to either 'red', 'blue' or 'green' (p.s. DONT FORGET TO INDENT YOUR CODE!) Just writing this function and seeing it work once you load up the game is enough to hook almost anyone that has the potential to enjoy programming but just doesn't know it yet.
- brooksbp 14y agoThere's an awful LOT happening behind those 580 lines. I'm not sure whether it is a good thing to start at such an unrealistically high level of abstraction for a 3d game.
- deleted 14y ago[deleted]
- csmatt 14y agoDon't worry, it is a good thing. The ones who want to take it further will naturally start digging into what's going on behind the scenes. Where would games be if we didn't have abstraction?
- jessedhillon 14y agoAs something for beginners, this would be great. When you start learning a new skill, it's important to have that feeling of tangible achievement to keep students motivated. Anyone interested enough to pursue a career in game dev will dig into pyglet to understand what's happening, as they start to push the limits of the knowledge gained from this kind of exercise. And then here's the other nice lesson: students can learn that they don't need to know everything before they start making cool and useful stuff. The confidence that comes from knowing that is just as important (more so?) as being able to implement pyglet from scratch if necessary. The former is a skill used daily, whereas the latter -- well, consider how often in your engineering career you casually rely on the work of others to power your creation. Has anyone ever required you to write a WSGI implementation, or a framework? That's been my experience, at least.
- sebastianavina 14y agoI remember the times when I had time and readed this things until I digested them...
- nsxwolf 14y agoThese seemingly "big" games written in a handful of lines of code always make me feel very, very stupid. I know if I made this, I could easily imagine writing 50,000 lines. How do people do this? How do their minds work?
- brooksbp 14y agoIt's all about standing on the shoulders of giants.
- nsxwolf 14y agoWell, that's certainly true when we're talking about the underlying 3D graphics libraries, IO, etc. But it seems like there's still a lot more to the rest of it, and that it shouldn't fit in 500 lines of code. This one really gets me as well: http://www.mojang.com/notch/j4k/l4kd/ http://www.mojang.com/notch/j4k/l4kd/
- tripzilch 14y ago> it shouldn't fit in 500 lines of code. Talk to me about "shouldn't" when we start breaking the Shannon-limit or other hard information-theoretical limits :-P
- kaoD 14y agoTake a look at the code, it's not THAT hard when you have the right tools (language, libraries, knowledge...) though it's quite awesome nonetheless. IMHO the game actually shows how powerful Python + Pyglet (OpenGL actually) are. Wanna get your mind blown even harder? Check some 4kb demos[1]... and that's binary size! No JVM/Python runtime supporting it. [1] http://www.youtube.com/results?search_query=4kb+demo&page=&utm_source=opensearch http://www.youtube.com/results?search_query=4kb+demo&pag...
- nwh 14y agoIf you want to show off the demoscene, try the 64KB prod "Chaos Theory". https://youtu.be/4DjBq2O0XXk https://youtu.be/4DjBq2O0XXk
- shurcooL 14y agoI remember being as excited about Liero as kids are about Minecraft today. Minecraft is great, but so was Liero.
- anonymous 14y agoSigh, right in the nostalgia. Liero was better though, it had fluid simulation. You could flood a level with water and drown people, or better - oil and then set it aflame. And it had guided missiles, which you could spam, making one long queue of keyboard-guided death. It was basically Worms Quake.
- arocks 14y agoPyglet is vastly more pythonic than pygame for writing games. I have found it much easier to teach pyglet for newcomers and the code is easier to read too. Since it uses ctypes, it is very easy to port pyglet to a platform that supports OpenGL. Unfortunately there is very little momentum in the project. Last year an alpha version was released after a gap of two years. I truly hope that pyglet gets the popularity it deserves.
- gamegoblin 14y agoPyOpenGL is by far the most pythonic of them all, though this comes at the cost of being around 4x slower than pyglet. Pyglet is a good compromise between the two.
- tartley 14y agoSince they are interoperable, you can write in pyopengl for Punic code, then convert your half dozen opengl calls in your innermost loops into pyglet for performance.
- javert 14y agoJust curious, what is it that makes the performance not so great? Could performance be significantly improved by doing a re-write in Lua, for example? (Probably not... it's probably something in Pyglet itself...) (FWIW, it's choppy compared to Minecraft, which is obviously much more complex and has a greater rendering distance.)
- Qantourisc 14y agoIt's rather simple: you are proccessing huge grids of 3D data, it's always going to be less then-optimal unless you write parts of it in languages that can do this fast. (This also includes interaction between blocks if you are going to processing a lot of those.)
- Someone 14y agoYou also will want to optimize the approach. For example, do you really need to draw each block as a cube? You could draw slabs of identical blocks as a single cuboid. That will complicate the code, but may allow you to run larger worlds faster. Also, you probably want to be smarter in computing which cubes to draw and what parts of them to draw (in a 'no reflections' world, you can see at most three faces of a cube. There is code for drawing fewer faces on lines 177-185, but that is disabled. If you figure out why, you may be able to improve the code). [My guess would be that the display does not look nice enough if you do. Maybe you will get flicker when looking at blocks near edge conditions (no pun intended)?] Also, keeping angles in degrees means you have to convert to radians on every iteration. It is easy to get rid of that. That will be a tiny, tiny speed up, but if you want to get top speed, you will eventually have to make many of those. Almost every such improvement in speed will come at the cost of code readability and maintainability.
- deleted 14y ago[deleted]
- javert 14y agoSo far, all the suggestions seem to be for main.py, not Pyglet. So is this something that can be "fixed" without diving into Pyglet?
- frozenport 14y agobuilding penises out of dirt right now. I find this comment a bit distracting, and in poor taste. Everybody knows real minecraft users build castles. building castles in the sky.
- BHSPitMonkey 14y agoI expect it's at least somewhat referencing this: http://www.youtube.com/watch?v=4wgQvij3rVE#t=214s http://www.youtube.com/watch?v=4wgQvij3rVE#t=214s
- deleted 14y ago[deleted]
- just2n 14y agoCan we get a JS answer to this in the browser? I'm curious if Three.js can match this level of terseness.
- rlx0x 14y agothere already is http://voxeljs.com/ http://voxeljs.com/ poxel rendering like in the python scribble is trivial, there many much more interesting problems to solve, like managing the geometry. If anyone is truly interested they should begin here: http://0fps.wordpress.com/2012/07/07/meshing-minecraft-part-2/ http://0fps.wordpress.com/2012/07/07/meshing-minecraft-part-...
- mrspeaker 14y agoIt would have a pretty-much identical amount of code, if someone first ported pyglet!
- daGrevis 14y agoWow! This is so cool! This proves Python's ability to be RAD.
- mckoss 14y agoA fork for those having trouble getting this to run on Mountain Lion. https://github.com/mckoss/Minecraft https://github.com/mckoss/Minecraft
- lizzard 14y agoThank you!
- sklivvz1971 14y ago[rant mode on] 1. 80% of the bang takes 20% of the time 2. Writing games is 99% fine tuning and game play 3. It's just a rendering exercise, certainly not a "clone" FYI: The above is "the internet" in three lines! :-) [rant mode off]
- relic 14y agoTo be fair, he did say 'simple'. I'm assuming he meant clone in terms of the overall physical characteristics, and not so much that it was a full clone.
- daGrevis 14y agoIf anyone are having problems starting this: * You need Python2 virtualenv, * You need to install `pyglet` in it, * Start it with `bin/python main.py`;
- inovica 14y agoI have been teaching my 10 year old Python as an exercise. Recently I've noticed he's getting a little bored and I'm being very careful not to 'sicken' him, as this needs to be fun. I've just shown him the video for this and downloaded the code to show him that its possible in Python. He loves Minecraft (we watched the documentary movie about Mojang) and seeing this has given him a bit more of an interest again
- rmp2150 14y agoMaybe you should let your 10 year old be bored. http://www.amle.org/Publications/RMLEOnline/Articles/Vol29No5/tabid/694/Default.aspx http://www.amle.org/Publications/RMLEOnline/Articles/Vol29No...
- StavrosK 14y agoOoh, a sample size of one! Sounds compelling!
- tripzilch 14y agoNot only that, it's not even to do with "boredom" in any sense that I understand the word: "participants limited TV, computer and video games, music, and telephone time to 30 min per day; in the time made free thereby, they were to read, sit quietly, write in their journals, meet with friends, and so on" Calling that "boredom", says more about whoever wrote the title "Why Kids Need To Be Bored" than it says about kids' performance and boredom. Then, the result of this study seems to be: out of 3 participants, 1 showed improvement, the other 2 dropped out. No data on 2 out of 3 test subjects, really, statistically proves nothing at all. It could go either way, or completely different. And it's bad science to not clearly point this out in the abstract. There's no shame in a failed experiment, but there is a lot of shame in pretending that it shows a trend that you didn't actually measure. I'd be very surprised if research points out that more boring study material leads to improved results. Actually, with N=1 I won't be surprised no matter what.
- ubershmekel 14y agoI wrote down the basic steps to get this running on windows at https://github.com/fogleman/Minecraft/wiki https://github.com/fogleman/Minecraft/wiki
- tripzilch 14y agoThis probably needs floated towards the top a bit. I thought I couldn't get it to work, but then I saw step 6 ... Actually you should just write it in the project's README.md :)
- rufugee 14y agoPlease...someone put a Udacity course together which backs into the underlying math used to create this (for those of us who didn't have it or don't remember it)...
- Janteloven 14y agoJump speed is too fast... if you slow it it works better and is more like minecraft IMHO elif symbol == key.SPACE: if self.dy == 0: self.dy = 0.500 # jump speed
- Janteloven 14y agoI mean self.dy = 0.050 # jump speed Jump higher for more fun...
- njharman 14y agoThis. This is why Python, Pyglet, and this particular project rock. Not the jump thing. But the whole class of things there is something you don't like and can more or less instantly find it, tweak it and see changes. No hours of learning, searching through code, waiting on compilers. Just experiment -> fun -> back to experiment.
- craigching 14y agoHook this code up to redstone (the nodejs server also on the front page) and you'll really have something :) Not to detract from the demo, it's really cool!
- JulianWasTaken 14y agoThis works on PyPy (Pyglet, and therefore this) by the way. Just grab Pyglet trunk and install it, and run main.py with pypy instead of with CPython.
- pit 14y agoThis is, in a word, dope.
- orangethirty 14y agoI was thinking of writing something like this to help my girls code. Modding minecraft with Java is fun, but the language gets in the way of beginners. This python version is great for that.
- moron4hire 14y agoI think everyone forgets that Minecraft's big feature is not its graphics or its gameplay, it's the procedural content generation. Almost anyone could learn to do Minecraft-level graphics in a few weeks, and the level of interactions (combat, crafting, moving things) could be reasonably approximated, too. But its procedural content generation algorithm is quite complex. You're not going to make an algorithm quite as nice as Minecraft's without a lot of research, a lot of hard work, and a lot of tweaking time.
- GuiA 14y agoWell, to be honest, this point is also where one could beat Minecraft at its own game. The minecraft devs haven't put emphasis on world exploration in a long time, instead preferring more traditional game features (like "the end"). There are many features notch promised when the project first started that where never acted upon, and there's definitely still demand for those.
- breakyerself 14y agoCould someone annotate this and re-release it. This seems like an awesome learning tool. A little more info about what everything does would go a long way.
- skot9000 14y agoThis is amazing. I've been playing with it all night. From a high level, what would be a strategy for making this multiplayer?
- sareon 14y agoSee I look at this and absolutely amazed. I wish I could make something like this but I have no idea of how to event start. I tried making a Settlers game in Python/PyGame and so far all I have been able to do is generate the board: https://gist.github.com/sareon/5268205 https://gist.github.com/sareon/5268205 - I've been told I am not properly discretizing the difference between the drawing of the game and the logic of the game which is going to hinder the development of the game. I have a BSc and an MSc in CS - focusing on NLP and Machine Learning, so I have an idea how to program, just not how to program games like the Minecraft clone or my own Settlers game. I've tried looking for tutorials and resources online and I've followed the InventWithPython which showed me how to make a simple game similar to how I started making my own. Clearly it was inefficient for a larger game. What are some good resources out there that can help me learn how to make games such as the Minecraft clone or even how to make my own settlers game actually be a useable game?
- tunnuz 14y agoThis is really amazing, nice job.
- Aardappel 14y agoSoftware has a habit of becoming exponentially complex as the featureset increases, so I am not that impressed by small code if you cut features aggressively. To prove a point, I wrote a minecraft clone in about 60 lines of code (10x less than the Python version). It cuts a few more corners (no textures, no gravity), but has the basics of world generation, rendering, and most importantly, adding and removing blocks in front of the crosshairs to build (see the little arch I made in the middle): http://i.imgur.com/ZZWFkXn.jpg http://i.imgur.com/ZZWFkXn.jpg It's written in Lobster in about 1.5hrs, which is quite similar in features to Python + pyglet. Yes it uses a few high level calls like simplex() and camera_FPS_view(), but these have been part of the Lobster standard library for a while, so I believe that's fair. All code I created for this demo are in that screenshot.