Upload
richard-donkin
View
6.679
Download
1
Tags:
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
Minecraft in 500 lines of Python
Richard Donkin@rdonkin
cloudfindhq.com
http://xkcd.com/353/
Why Python?
Easy to write
Great community
Fast enough Compilers (Cython)JIT (PyPy)
3D and Game Libraries
http://xkcd.com/353/
Demo
github.com/fogleman/Minecraft - Michael Fogleman
Early version:
Minecraft Essentials
Objects are blocks, in 3D grid
OperationsMove: W, S, A, DLook around: move mouseAdd or remove blocks: click mouse
Textures
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
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
Window & Startup Code
Building stuff
Mouse locking, sight vector, hit testing
Remove Block – STONE is special
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°
Highlighting ‘target block’
Hit-test from player position to target block
Draw line around visible edges of block, using Pyglet
Model
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
1100 commits later…
github.com/boskee/Minecraft
Major fork
8,000 lines
19 contributors
Multi-player
Cython
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/
Wrap Up
Concise, elegant code
You can do a lot in 500 lines!
Thank YouDownload slides:
https://www.slideshare.net/rdonkin/minecraft-in-500-lines-with-pyglet-pycon-
uk
Richard Donkin@rdonkin
cloudfindhq.com
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()