Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Minecraft in 500 lines with Pyglet - PyCon UK

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.

  • Login to see the comments

Minecraft in 500 lines with Pyglet - PyCon UK

  1. 1. Minecraft in 500 lines of Python Richard Donkin @rdonkin
  2. 2.
  3. 3. Why Python? Easy to write Great community Fast enough Compilers (Cython) JIT (PyPy) 3D and Game Libraries
  4. 4. Demo
  5. 5. - Michael Fogleman Early version:
  6. 6. Minecraft Essentials Objects are blocks, in 3D grid Operations Move: W, S, A, D Look around: move mouse Add or remove blocks: click mouse Textures
  7. 7. How Pyglet Helps 3D gaming library - Simple wrapper for OpenGL Pure Python Library not a framework This is a quick Minecraft code walkthrough, not a Pyglet tutorial Tutorial based on Asteroids:
  8. 8. Overview of Structure Window class Subclass of Pyglet window class User interaction, movement, rendering Model class Models 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
  9. 9. Window & Startup Code
  10. 10. Building stuff Mouse locking, sight vector, hit testing Remove Block – STONE is special
  11. 11. 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°
  12. 12. Highlighting ‘target block’ Hit-test from player position to target block Draw line around visible edges of block, using Pyglet
  13. 13. Model
  14. 14. 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
  15. 15. 1100 commits later… Major fork 8,000 lines 19 contributors Multi-player Cython
  16. 16. Pyglet and More 2D game toolkits: Cocos2D: - uses Pyglet Fife: - esp. RTSs and RPGs Add-on libraries that can work with Pyglet: Rabbyt – sprites: Pymunk – physics: Alternative frameworks: Pygame – larger community, more game-specific, open source book at
  17. 17. Wrap Up Concise, elegant code You can do a lot in 500 lines!
  18. 18. Thank You Download slides: Richard Donkin @rdonkin
  19. 19. 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 sector Track player position in sector If player moves between sectors, determine which adjacent sectors to show – see change_sectors()