Minecraft in 500 lines with Pyglet - PyCon UK

14.203 visualizações

Publicada em

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.

Publicada em: Tecnologia
1 comentário
6 gostaram
Sem downloads
Visualizações totais
No SlideShare
A partir de incorporações
Número de incorporações
Incorporações 0
Nenhuma incorporação

Nenhuma nota no slide
  • Pyglet is actually 60,000 lines of code, and requires some understanding of OpenGL, but is still quite easy to usePanda3D is an alternative higher-end 3D library, more complex to use than Pyglet. Other 3D and gaming libraries are discussed later.
  • STONE is specially treated because the base of the world is stone (grey colour) – this stops you drilling through the bottom of the world, which would look bad. To see this, remove the layer of earth blocks on the ground until you get to stone.
  • Now 8000 lines of code, including full inventory, multi-player client/server, etc. Supports compiling to C using Cython for speed, but not as easy to get started with as the original fogleman version.
  • Panda3D and PyOpenGL are two other 3D frameworks.
  • Minecraft in 500 lines with Pyglet - PyCon UK

    1. 1. Minecraft in 500 lines of Python Richard Donkin @rdonkin cloudfindhq.com
    2. 2. http://xkcd.com/353/
    3. 3. Why Python? Easy to write Great community Fast enough Compilers (Cython) JIT (PyPy) 3D and Game Libraries http://xkcd.com/353/
    4. 4. Demo
    5. 5. github.com/fogleman/Minecraft - 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 - pyglet.org 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: steveasleep.com
    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… github.com/boskee/Minecraft Major fork 8,000 lines 19 contributors Multi-player Cython
    16. 16. Pyglet and More 2D game toolkits: Cocos2D: http://cocos2d.org/ - uses Pyglet Fife: 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/
    17. 17. Wrap Up Concise, elegant code You can do a lot in 500 lines!
    18. 18. Thank You Download slides: https://www.slideshare.net/rdonkin/minecraft-in-500-lines-with-pyglet-pycon-uk Richard Donkin @rdonkin cloudfindhq.com
    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()