Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

Minecraft in 500 lines of Python with Pyglet

73.766 visualizaciones

Publicado el

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.

Minecraft in 500 lines of Python with Pyglet

  1. 1. Minecraft in 500 lines of Python Richard Donkin @rdonkin
  2. 2. Early version:
  3. 3.
  4. 4. 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
  5. 5. Pyglet 3D gaming library - Simple to learn Doesn’t mandate program structure Wraps OpenGL Can use py2exe / py2app This is a quick Minecraft code walkthrough, not a tutorial Tutorial based on Asteroids:
  6. 6. Minecraft Essentials Objects are blocks, in 3D grid Operations Move: W, S, A, D – and Tab to fly Mouse to look (or fly) Mouse to add or remove blocks Textures
  7. 7. Overview Window class Subclass of Pyglet window class User interaction, movement, rendering Model class Models the world as blocks in 3D grid Dictionary world[position] where position is tuple (x, y, z) Contains texture such as
  8. 8. Window & Startup Code
  9. 9. Model
  10. 10. 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
  11. 11. Building stuff Mouse locking, sight vector Hit testing, block add/remove
  12. 12. 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
  13. 13. Highlighting ‘target block’ Hit-test from player position to target block Draw line around visible edges of block, using Pyglet
  14. 14. 1100 commits later… Major fork 8,000 lines 19 contributors Multi-player Optionally, compile to C via Cython
  15. 15. 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
  16. 16. Summary Concise, elegant code Choose the right framework Small games to get started Write your own! Try challenges
  17. 17. Thank You Richard Donkin @rdonkin
  18. 18. 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()