Tiled Maps & Tile Based Movement
Pages 75
- Home
- A draggable scrollbar using a slider
- AdBuddiz Android advertisements integration for Kivy apps
- Advanced Graphics: In Progress
- Android native embedded browser
- Android SDK NDK Information
- Android style menu app skeleton
- Background Service using P4A android.service
- Batch installer for windows(KivyInstaller)
- Breaking changes in Kivy
- Building Portable Package
- Button(s) in settings panel
- Buttons in Settings panel
- Community Guidelines
- Connecting Kivy with Anaconda (OSX)
- Contextual Menus
- Contextual menus
- Control alpha of all the children
- Creating a 64 bit development environment with MinGW on Windows
- Creating a Release APK
- Data driven variables with kivy properties
- Debugging widget sizes
- Deep Linking with iOS and Android
- Delayed Work using Clock
- Drag and Drop Widgets
- Dragable Widget
- Draggable Scalable Button
- Editable ComboBox
- Editable Label
- Embedding a Carousel inside a TabbedPanel
- Forms
- GestureBox
- KEP001: Instantiate things other than widgets from kv
- Kivy 2.0 api breaks
- Kivy Blogs and Blog Posts
- Kivy clock experiments
- Kivy Python 2 Support Timeline
- Kivy Technical FAQ
- Linking ScreenManager to a different Widget
- List of Kivy Projects
- Markup Summary
- Menu on long touch
- On touch current widget
- On touch on current widget
- Packaging Kivy apps written in Python 3, targeting Windows using Nuitka
- Pyjnius Vibrator Example
- rand0m app
- Release Checklist
- Sample Gestures
- Scaler for Retina screen
- Scollable Options in Settings panel
- Scrollable Label
- Setting up garden with Mac Ports
- Setting Up Kivy with various popular IDE's
- Setting up Pycharm on OSX (older versions)
- Simple slider with value in label
- Snippet template
- Snippets
- Snippets awaiting moderation
- Starting Kivy App and Service on bootup on Android
- Styling a Spinner and SpinnerOption in KV
- Talks and tutorials
- Theming Kivy
- Tiled Maps & Tile Based Movement
- Tiling the background of a widget with an image, pixel perfect
- Ubuntu Touch
- Updating widget content from a items list
- User Snippets
- Using Asynchronous programming inside a Kivy application
- Using Kivy with an existing Python installation on Windows (64 or 32 bit)
- Using Visual C Build Tools instead of Visual Studio on Windows
- Viewport with fixed resolution autofit to window
- wiki_proposed
- Windows RT
- Working with Python threads inside a Kivy application
- Show 60 more pages…
Clone this wiki locally
What are we trying to do?
The first thing I always like to do when using a new framework is load a Tiled .tmx file and display the map in the framework, and get tile-based movement for characters up and running.
- Create a class that loads Kivy-compatible images from Tiled map tile sets
- Create a Kivy Widget that displays a loaded map
- Create a Character widget that can walk around the map with user input
Prerequisites
Install PyTMX. The plan is to create a class that loads images that Kivy can use while leveraging the existing PyTMX code.
Loading Tiled Maps (KivyTiledMap.py)
The key things that need to be done to override the PyTMX class are:
- Override loadTileImages() on TiledMap
- Actually call loadTileImages() before it's time to display them
The class to use for loading a tile set image is:
from kivy.core.image import Image
And you can easily get the texture in the image via:
image = Image(filepath)
texture = image.texture
The most important concept is that we want to grab each tile out of the tile set's source image file with that texture's get_region() capability:
tile = texture.get_region(x, y, tileset.tilewidth, tileset.tileheight)
Displaying Tiled Maps (TiledGrid.py)
The best idea I had for displaying Tiled information was, conceptually:
- Create a Widget inheriting from GridLayout
- Load a KivyTiledMap and set the number of rows/columns to the tile map width and height
- Go through the map and add
from kivy.uix.image import Imagewidgets to the grid
This solution, however, has a couple of problems - it doesn't handle multiple layers, and it doesn't handle disabling the drawing of tiles off the screen. It shouldn't be too bad to extend this class to fit those needs, hopefully.
Creating a Moving Character (Character.py)
One important Kivy aid to know about is from kivy.animation import Animation. Coupled with TiledGrid, it allows calls like this to "just work":
coords = self.map_grid.get_tile_position(self.current_tile.x, self.current_tile.y)
anim = Animation(x=coords[0], y=coords[1])
anim.start(self)
The way this Character widget works is by checking input for keyboard arrow inputs, and then asking the TiledGrid if the move is valid. If it is, then it gets the true screen coordinates of the tile's position and starts an Animation moving to those coordinates.