minecraft in 500 lines with pyglet - pycon uk
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.TRANSCRIPT
![Page 1: Minecraft in 500 lines with Pyglet - PyCon UK](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/1.jpg)
Minecraft in 500 lines of Python
Richard Donkin@rdonkin
cloudfindhq.com
![Page 2: Minecraft in 500 lines with Pyglet - PyCon UK](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/2.jpg)
http://xkcd.com/353/
![Page 3: Minecraft in 500 lines with Pyglet - PyCon UK](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/4.jpg)
Demo
![Page 5: Minecraft in 500 lines with Pyglet - PyCon UK](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/5.jpg)
github.com/fogleman/Minecraft - Michael Fogleman
Early version:
![Page 6: Minecraft in 500 lines with Pyglet - PyCon UK](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/9.jpg)
Window & Startup Code
![Page 10: Minecraft in 500 lines with Pyglet - PyCon UK](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/10.jpg)
Building stuff
Mouse locking, sight vector, hit testing
Remove Block – STONE is special
![Page 11: Minecraft in 500 lines with Pyglet - PyCon UK](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/13.jpg)
Model
![Page 14: Minecraft in 500 lines with Pyglet - PyCon UK](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/16.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 17: Minecraft in 500 lines with Pyglet - PyCon UK](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/17.jpg)
Wrap Up
Concise, elegant code
You can do a lot in 500 lines!
![Page 18: Minecraft in 500 lines with Pyglet - PyCon UK](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/18.jpg)
Thank YouDownload slides:
https://www.slideshare.net/rdonkin/minecraft-in-500-lines-with-pyglet-pycon-
uk
Richard Donkin@rdonkin
cloudfindhq.com
![Page 19: Minecraft in 500 lines with Pyglet - PyCon UK](https://reader033.vdocuments.mx/reader033/viewer/2022050804/54b4da9a4a7959e55c8b4601/html5/thumbnails/19.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()