19
Minecraft in 500 lines of Python Richard Donkin @ rdonkin cloudfindhq.com

Minecraft in 500 lines with Pyglet - PyCon UK

Embed Size (px)

DESCRIPTION

A short walkthrough of some of the code from an impressive 500 line Python game - a simple version of Minecraft, using the Pyglet 3D library. Links to resources for Pyglet and Python gaming generally. Downloading the file for use in PowerPoint / LibreOffice will let you see the builds on some of the code slides, which may help a bit.

Citation preview

Page 1: Minecraft in 500 lines with Pyglet - PyCon UK

Minecraft in 500 lines of Python

Richard Donkin@rdonkin

cloudfindhq.com

Page 2: Minecraft in 500 lines with Pyglet - PyCon UK

http://xkcd.com/353/

Page 3: Minecraft in 500 lines with Pyglet - PyCon UK

Why Python?

Easy to write

Great community

Fast enough Compilers (Cython)JIT (PyPy)

3D and Game Libraries

http://xkcd.com/353/

Page 4: Minecraft in 500 lines with Pyglet - PyCon UK

Demo

Page 6: Minecraft in 500 lines with Pyglet - PyCon UK

Minecraft Essentials

Objects are blocks, in 3D grid

OperationsMove: W, S, A, DLook around: move mouseAdd or remove blocks: click mouse

Textures

Page 7: Minecraft in 500 lines with Pyglet - PyCon UK

How Pyglet Helps3D gaming library - pyglet.org

Simple wrapper for OpenGLPure PythonLibrary not a framework

This is a quick Minecraft code walkthrough, not a Pyglet tutorial

Tutorial based on Asteroids: steveasleep.com

Page 8: Minecraft in 500 lines with Pyglet - PyCon UK

Overview of Structure

Window classSubclass of Pyglet window classUser interaction, movement, rendering

Model classModels the world as blocks in 3D grid

“The Whole World is an (x, y, z) Dictionary”Dictionary world[position]

Where position is tuple (x, y, z)Each entry contains texture such as

Page 9: Minecraft in 500 lines with Pyglet - PyCon UK

Window & Startup Code

Page 10: Minecraft in 500 lines with Pyglet - PyCon UK

Building stuff

Mouse locking, sight vector, hit testing

Remove Block – STONE is special

Page 11: Minecraft in 500 lines with Pyglet - PyCon UK

Look around

Motion matters (dx, dy), not absolute position

Get ‘look vector’ rotation (as x, y degrees) & add proportion of motion

Mouse left/right is x (look left/right)

And forward/back is y (look up/down) – max ±90°

Page 12: Minecraft in 500 lines with Pyglet - PyCon UK

Highlighting ‘target block’

Hit-test from player position to target block

Draw line around visible edges of block, using Pyglet

Page 13: Minecraft in 500 lines with Pyglet - PyCon UK

Model

Page 14: Minecraft in 500 lines with Pyglet - PyCon UK

Adding a Block

Remove any existing block at (x, y, z) position from the ‘world dictionary’

Set texture for this block at position

Update sectors (16x16 2D grid) to record this block

Enables speedup by only rendering some sectors

Show the block, and check if any neighbours now visible

Page 15: Minecraft in 500 lines with Pyglet - PyCon UK

1100 commits later…

github.com/boskee/Minecraft

Major fork

8,000 lines

19 contributors

Multi-player

Cython

Page 16: Minecraft in 500 lines with Pyglet - PyCon UK

Pyglet and More2D game toolkits:

Cocos2D: http://cocos2d.org/ - uses PygletFife: http://www.fifengine.net/ - esp. RTSs and RPGs

Add-on libraries that can work with Pyglet:Rabbyt – sprites: http://arcticpaint.com/projects/rabbyt/ Pymunk – physics: http://pymunk.org

Alternative frameworks:Pygame – larger community, more game-specific, open source book at http://inventwithpython.com/

Page 17: Minecraft in 500 lines with Pyglet - PyCon UK

Wrap Up

Concise, elegant code

You can do a lot in 500 lines!

Page 19: Minecraft in 500 lines with Pyglet - PyCon UK

Sect0rization

Technique used to speed up rendering of world – only render nearby sectors

Sector = 16x16 2D region of world In Model, dictionary sectors[sector] maps from sector to list of positions in that sectorTrack player position in sectorIf player moves between sectors, determine which adjacent sectors to show – see change_sectors()