minecraft in 500 lines of python with pyglet
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.TRANSCRIPT
![Page 1: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/1.jpg)
Minecraft in 500 lines of Python
Richard Donkin@rdonkin
cloudfindhq.com
![Page 2: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/2.jpg)
github.com/fogleman/MinecraftEarly version:
![Page 3: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/3.jpg)
http://xkcd.com/353/
![Page 4: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/4.jpg)
Why Python?Easy to write
Great community
Fast enough Can use compilers (Cython etc) or JIT (PyPy)Or use a spare GPU …
Libraries, broad usage
Raspberry Pi to scientific computing to cloud apps
http://xkcd.com/353/
![Page 5: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/5.jpg)
Pyglet3D gaming library - pyglet.org
Simple to learnDoesn’t mandate program structureWraps OpenGLCan use py2exe / py2app
This is a quick Minecraft code walkthrough, not a tutorial
Tutorial based on Asteroids: steveasleep.com
![Page 6: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/6.jpg)
Minecraft Essentials
Objects are blocks, in 3D grid
OperationsMove: W, S, A, D – and Tab to flyMouse to look (or fly)Mouse to add or remove blocks
Textures
![Page 7: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/7.jpg)
OverviewWindow class
Subclass of Pyglet window classUser interaction, movement, rendering
Model classModels the world as blocks in 3D grid
Dictionary world[position]
where position is tuple (x, y, z)Contains texture such as
![Page 8: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/8.jpg)
Window & Startup Code
![Page 9: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/9.jpg)
Model
![Page 10: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/10.jpg)
Look around
X axis is horizontal (look left/right)
Y axis is vertical (look up/down) – max ±90°
Only motion matters, not absolute position
Python tuple assignment
![Page 11: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/11.jpg)
Building stuff
Mouse locking, sight vector
Hit testing, block add/remove
![Page 12: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/12.jpg)
Adding a Block
Remove any existing block at (x, y, z) position from the ‘world dictionary’
Set texture for this block at position
Create or update sector (16x16 2D grid) in which this block resides – enables speedup by only rendering some sectors
Show the block, and check if any neighbours now visible
![Page 13: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/13.jpg)
Highlighting ‘target block’
Hit-test from player position to target block
Draw line around visible edges of block, using Pyglet
![Page 14: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/14.jpg)
1100 commits later…
github.com/boskee/Minecraft
Major fork
8,000 lines
19 contributors
Multi-player
Optionally, compile to C via Cython
![Page 15: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/15.jpg)
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 16: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/16.jpg)
Summary
Concise, elegant code
Choose the right framework
Small games to get started
Write your own!Try pyweek.org challenges
![Page 17: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/17.jpg)
Thank YouRichard Donkin
@rdonkin cloudfindhq.com
![Page 18: Minecraft in 500 lines of Python with Pyglet](https://reader036.vdocuments.mx/reader036/viewer/2022081412/540dc6018d7f72747e8b4b57/html5/thumbnails/18.jpg)
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()