The Art of Writing Wargames in Python Using Kivy

Presented by Dorian Pula / @dorianpula

Prologue

Who am I?

  • Software Developer @ Points
    • Develop eCommerce platform for Loyalty Programs
    • Dockerized Flask REST API microservices
  • Open Source
    • Rookeries - CMS for developers & designers 
    • Contributor to Fabric, Ansible & core Python
  • Board gamer → Video gamer → Developer
    • Apparently too late to the wargaming scene.
    • Started programming from copying/porting
      Apple 2 Basic games to Microsoft QBasic.
    • First open source project was a game: justCheckers.

Building a Game in Kivy @ PyCon

  • Day 1 - Sprinting on Flask
  • Day 2-4 - Kivy Game Camp sprinting.
    • Because making a game is more fun, than
      submitting PRs for a web framework...
    • Hosted by Amy & Kjell Woodling from
      Leap, Learn, Fly!
    • Improved Game Camp and Kivy documentation.

Setting the Board

PyQt / PySide

  • PySide / PyQt
    • Python bindings for C++ based on Qt framework.
    • Powerful screen + widget managements.
    • Need C++ experience to effectively use.
  • PyOtherside - QML bindings for Python.

PyGTK, Pyglet, PyGame, etc.

  • PyGTK
    • Python bindings for C based GTK.
  • PyGame
    • Bindings for SDL, more low-level.
  • Pyglet
    • OpenGL based scene-graph library.
  • Electron
    • Javascript frontend.
    • Can use Python backend.
  • Tcl

Kivy

  • Pythonic!
  • Flexible due to OpenGL rendering.
  • Event loop is good for games and
    interactive apps. 
  • UI layout with declarative KV
    (Classic widget + layout container).
  • Reasonable cross-platform build setup.

Crafting the Game

Kivy Basics

  • Standard UI widget / layout based.
    • Widgets encapsulate behaviour and look.
    • Layout (with nesting) to position widgets.
  • Use of KV language to declare the UI.
    • Can mix Python and KV.
  • Standard event loop to update the screen.
    • Nothing drawn outside of the event loop.

KV Example

#:import random random
    
<Widget>:
    canvas.after:
        Color:
            rgba: 1,1,1,.5
        Line:
            rectangle: self.x, self.y, self.width, self.height
            width: 2
                
<DebugLabel@Button>:
    size: self.parent.size
    pos: self.parent.pos
    background_color: random.random(), random.random(), random.random(), 0.6
    text: 'debuglabel'

Pro Tip: Use randomly coloured translucent elements to help with layouts!

Hexagon Layouts

  • Standard Layouts:
    • Grid
    • Box
    • Float (free floating absolute positioning)
  • No such thing as a Hexagon Grid layout. :(  
    Gotta build your own.
  • Lots of math involved:
    • Positioning hexes together.
    • Adding map positional coordinates.
    • Using a cubic coordinates for movement and distance.
    • Thankfully Amy & Kjell are mathematicians. :)

Map Building

Unit Control via Events

  • Having the ability to select and control a unit.
    • Give it commands (like move or attack).
  • Kivy has a rich input event system:
    • Multiple touch events out of the box.
    • Each Widget can has hooks for on_EVENT().
      • e.g. on_touch_down(), on_pressed()
    • Events broadcast from parent to children.
    • Can create and use custom events.

Kivy Gotchas

  • Three different phases for running painting
    instruction on a canvas.
    • before, during and after initialization.
  • Kivy allows for changeable window sizes.
    • Need to be aware of positioning in a
      dynamic manner.
  • Events don't propagate unless you return true,
    in the event handler method.

Forward Into the World

Targetting Desktops with PyInstaller

  • PyInstaller is NOT an installer.
  • PyInstaller is a bundler for Python modules
    + libraries.
    • Single directory mode
    • Single binary mode (runnable zip archive)
  • Relatively straight forward with "recipes" for Kivy.
  • Works for all desktop OS: Windows, Linux, OS X.

How about mobile devices?

  • Kivy project: python-for-android
    • Deals with bundling of an app into an APK.
  • Use Buildozer for building apps:
    • Automates lot of the setup and linking.
    • Three easy steps:
      1. buildozer init
      2. Edit the spec file
      3. buildozer android debug deploy
      4. Debug/fix when things break
    • Works with iOS and Android.

Building Game Engines +
Map Editors

  • Real game devs usually don't build game engines.
    • Game engines can be time intensive and complex.
    • Standard use of third-party engines.
  • Use a entity-component architecture.
    • Every on-screen entity is made of a bunch of
      self-contained components.
  • Use a tile or map editor.

KivEnt + Tiled

Epilogue

Aftermath

  • Kivy makes game development possible and easy.
    • App development in general in Python.
  • Game dev means lots and lots of math and tweaking.
    • Experiment, run, tweak, run, repeat.
  • Use KivEnt for more serious game dev.
  • Cross-platform app / game development possible
    in Python with Kivy.

Thank you!

Questions?