
Simple Minecraft Clone in 580 lines of Python - wting
https://github.com/fogleman/Minecraft
======
rodly
If 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
There'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.

~~~
jessedhillon
As 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.

~~~
lessnonymous
Totally agree.

I remember writing this:

10 PRINT "LESSNONYMOUS"

20 GOTO 10

Seeing _actual things happen_ was awesome. Now we have 3D games, seeing your
name over and over wont enthrall, so being able to mod and extend a basic
Minecraft world would be an incredible gateway.

The way to hook people is exactly like you say. Make a small change, see a
huge difference. Suddenly ... YOU HAVE THE POWER!!!

10 PRINT "LESSNONYMOUS";

20 GOTO 10

~~~
eric_bullington
Wow, that just brought back a massive flash from the past. I actually
remembered how I _felt_ then I wrote those lines (or something very similar)
for the first time. It's a powerful feeling. Thanks for that.

~~~
failrate
I remember typing something similar (but probably more seditious) on the
computers at every store I came across and watching the staff not know how to
turn it off.

Probably a jerk move in retrospect, but how can you work in a computer store
and _not_ play around with the computers?!

------
ubershmekel
I wrote down the basic steps to get this running on windows at
<https://github.com/fogleman/Minecraft/wiki>

~~~
tripzilch
This 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 :)

------
j2kun
I 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
If 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.

------
arocks
Pyglet 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
PyOpenGL 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
Since 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.

------
nsxwolf
These 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?

~~~
kaoD
Take 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&pag...](http://www.youtube.com/results?search_query=4kb+demo&page=&utm_source=opensearch)

~~~
nwh
If you want to show off the demoscene, try the 64KB prod "Chaos Theory".

<https://youtu.be/4DjBq2O0XXk>

~~~
guiambros
If you want to really show off the demoscene, try Farbrausch - fr-041.

It's comparatively very big (179KB), but impressive how they were able to
squeeze so much stuff there: video
<http://www.youtube.com/watch?v=wqu_IpkOYBg>, official repository:
<http://j.mp/11ETcY0>.

If you want to dig deeper, Farbrausch published most of their material and
tools last year including the source code for fr-041. Search for farbrausch on
GitHub. But I must say, it's pretty hardcore.

~~~
n2dasun
That is so incredible.

------
doctoboggan
This 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.

------
moron4hire
I 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
Well, 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.

------
scottrogowski
More 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

~~~
scottrogowski
I got it to work on OSX 10.8!

slunk's suggestion:

`arch -i386 python main.py`

got me halfway there

You also have to run it with python 2.6 like so:

`arch -i386 /usr/bin/python2.6 main.py`

If you only have pyglet for 2.7, you will have to install it for 2.6.
Installing packages in python for 2.6 will require pip-2.6 which you get like
this:

`sudo easy_install-2.6 pip`

then

`pip-2.6 install pyglet`

Walla!

~~~
Alphasite_
Another option is to use a version of Pyglet from the main repository on
googlecode. Thats been updated to avoid the Avbin problems and add OSX 64 bit
support.

------
sklivvz1971
[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
To 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.

------
Aardappel
Software 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>

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.

------
rufugee
Please...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)...

------
Millennium
Nice. Next stop: Dwarf Fortress.

~~~
arsey
Simple Dwarf Fortress Clone in 58000 lines of Python

~~~
codewright
Lispers that preach of concision mere mortals cannot possibly dream of, this
is your call to arms.

~~~
cturner
I 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.

------
sareon
See 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> \-
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?

------
cdelsolar
This is really freaking cool. I can't believe how short the code is.

------
javert
Just 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
It'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
You 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.

~~~
javert
So far, all the suggestions seem to be for main.py, not Pyglet.

So is this something that can be "fixed" without diving into Pyglet?

~~~
Someone
That's because I only looked at main.py.

If you want the utmost speed, you get it where you can get it the easiest. I
bet the other code also can be sped up.

I would expect that the major improvements would be in main.py, though. That
Pyglet library would have to be really bad to screw up what main.py does.

------
Janteloven
Jump 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

~~~
njharman
This. 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
Hook 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!

------
breakyerself
Could 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.

------
inovica
I 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
Maybe you should let your 10 year old be bored.

[http://www.amle.org/Publications/RMLEOnline/Articles/Vol29No...](http://www.amle.org/Publications/RMLEOnline/Articles/Vol29No5/tabid/694/Default.aspx)

~~~
StavrosK
Ooh, a sample size of one! Sounds compelling!

~~~
tripzilch
Not 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.

------
mckoss
A fork for those having trouble getting this to run on Mountain Lion.

<https://github.com/mckoss/Minecraft>

~~~
lizzard
Thank you!

------
just2n
Can we get a JS answer to this in the browser? I'm curious if Three.js can
match this level of terseness.

~~~
rlx0x
there already is <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-...](http://0fps.wordpress.com/2012/07/07/meshing-minecraft-part-2/)

------
frozenport

      building 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
I expect it's at least somewhat referencing this:

<http://www.youtube.com/watch?v=4wgQvij3rVE#t=214s>

------
orangethirty
I 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.

------
JulianWasTaken
This 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.

------
skot9000
This is amazing. I've been playing with it all night. From a high level, what
would be a strategy for making this multiplayer?

------
shurcooL
I remember being as excited about Liero as kids are about Minecraft today.
Minecraft is great, but so was Liero.

~~~
anonymous
_Sigh_ , 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.

------
daGrevis
If anyone are having problems starting this:

* You need Python2 virtualenv,

* You need to install `pyglet` in it,

* Start it with `bin/python main.py`;

------
sebastianavina
I remember the times when I had time and readed this things until I digested
them...

------
josephagoss
How are the graphics being drawn? Is Pyglet being used to so this?

~~~
gamegoblin
Pyglet provides native windowing and a full OpenGL wrapper.

------
daGrevis
Wow! This is so cool!

This proves Python's ability to be RAD.

------
tunnuz
This is really amazing, nice job.

------
pit
This is, in a word, dope.

