Live data from Hacker News

Simple Minecraft Clone in 580 lines of Python

github.com

61–70 of 113 posts

Re: Simple Minecraft Clone in 580 lines of Python

#61

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

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!

I needed to run the last command as sudo:

sudo pip-2.6 install pyglet

After that it worked perfectly. Thanks!

Re: Simple Minecraft Clone in 580 lines of Python

#62
post #36

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

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

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.

Re: Simple Minecraft Clone in 580 lines of Python

#63
post #57
post #56

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

Maybe you should let your 10 year old be bored. http://www.amle.org/Publications/RMLEOnline/Articles/Vol29No...

Ooh, a sample size of one! Sounds compelling!

Re: Simple Minecraft Clone in 580 lines of Python

#64
post #14

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): # reca…

> If someone were to make something like this a step-by-step tutorial/template appropriate for a classroom setting it would be HUGE.

(emphasis mine :-))

That's the first thing I thought. I teach kids to do creative/interesting things with computers. It's hard to get them writing code[0] (it's not a classroom--they come voluntarily and explore what technology topics they like), one of the older (14) kids started out doing that, however, because he wanted to write Minecraft mods. Those are written in Java, in an Eclipse environment working on a decompiled (and only partially annotated) version of the Minecraft .jar. That was not how I envisioned a gentle introduction to computer-programming :) But he was persistent, and got it to work. This only goes to show what's really important: not if the language is simple, complex, elegant, whether the subject matter is easy to grasp, no. It's just a matter of whether the thing they need to learn is on the path towards a goal they really really want to achieve[1].

But yes, Minecraft is a huge thing among these kids. Others are cobbling together multi-player servers from scrap computers.

While in a sense, brooksbp (sibling comment) is right, there's a LOT going on in these 580 lines of code (I haven't looked at it yet, but I can imagine), 580 lines is not that much either. And if it's something they can run, edit, run, mess around with, see why it broke, edit, run, etc, that's enough to get started. They don't need to understand the whole thing right away. In fact even something as simple (and hardly programming) as changing the colour or textures is awesome because it teaches them that whatever's going on behind those graphics is yours and yours to control (unlike, say, the TV or their aunt's iPhone).

That 14 year-old kid is currently experimenting with Unity, trying to make an actual game. Unfortunately, I haven't had time to get acquainted with that environment myself, so I can only give him general advice. I'm way more comfortable with Python (especially a mere 580 lines of it :P) so who knows.

[0] Which still mystifies me, I was writing my first lines of BASIC at the age of 9, without help from my parents or anyone except library books and magazines. That probably makes me an outlier, but enough children pass through here that I'd expect to see at least maybe one or two. I guess it's different because back then that machine (an Amstrad PCW) wouldn't do anything interesting except word-processing unless I coded it myself.

[1] That was actually a big lesson for me :) I kind of knew it to be the case, but witnessing the process really nailed it down for me.

Re: Simple Minecraft Clone in 580 lines of Python

#65

Earlier quoted context omitted.

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…

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 "LESSNONYMOU…

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.

Re: Simple Minecraft Clone in 580 lines of Python

#66
post #6

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

Please, 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.

Re: Simple Minecraft Clone in 580 lines of Python

#67
post #18

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?

I'm exactly the opposite. Whenever I see a project consisting of 50,000 lines of code, I am amazed that somebody had the stamina to bite down and actually write all that code.

I could never imagine myself writing 50,000 lines, I'm way too lazy for that. Fifty-thousand lines? You could write a universe in that, with a high-level language like Python (or in assembly, a smallish planet).

Semi-kidding aside, it's part laziness and part DRY-principle. I hate writing code twice, even if it's two chunks with similar functionality. If they are so similar (in an abstract way), there's probably a reason for that, and I try to capture that reason into my code by unraveling the similar parts from the specific parts. That usually makes it smaller.

Can you imagine what happens if those 50K lines (comments/white-space aside) all implement nearly unique functionality? Those 64k demos linked upthread are what happens. I'm going to guess those are 100K-250K lines C++ [1], maybe? But then, a prize-winning 64k probably does a little bit more than a basic partial Minecraft engine :-P

Since you're asking "how do their minds work?", maybe I can get a little bit obsessive over this optimizing (wouldn't call it OCD--that would trivialize the disorder), but I do really get a slight "mind itch" if I notice code with duplicate functionality.

[0] Sometimes it doesn't. But often there's other advantages, too. If that's not the case either (or I'm on a deadline), I grit my teeth and write the duplicate code, but I try to make it really obvious that it is duplicate. That way at least it'll compress well :P

[1] Not even the 4K demos use pure assembly any more, compiler tricks and exe packers have become amazing in the past decade since I stopped making them. Not counting the external procedural/generative design tools/Werkzeuge, btw.

Re: Simple Minecraft Clone in 580 lines of Python

#68
post #23

Earlier quoted context omitted.

It's all about standing on the shoulders of giants.

Well, 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/

> 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

Re: Simple Minecraft Clone in 580 lines of Python

#69
post #32
post #18

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?

Actually this demo lacks any of big or mid-sized features (as in implementation) of Minecraft, such as infinite world, terrain generation, mobs, mechanisms (pistons/redstone).

That's because its not a game is a proof of concept

Re: Simple Minecraft Clone in 580 lines of Python

#70
post #62

Earlier quoted context omitted.

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

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.…

[deleted]
Post reply on HN