Here’s how you use PyInstaller and PyGame to create a single-file executable from a project that has a data
directory that contains resources like images, fonts, and music.
Ludum Dare 46 Picks a collection by leafo, lyn-marie charlene excelsia, Spencer Hayes, LadyAijou, Amos last updated 2020-05-01 18:41:32 Follow leafo Following leafo Unfollow leafo. The Open Source Software Collection includes computer programs and/or data which are licensed under an Open Source Initiative or Free Software license, or is public domain. In general, items in this collection should be software for which the source code is freely available and able to be used and distributed without undue restrictions, and/or.
Galcon is a series of real-time strategy video games for Android, iPhone, webOS, Windows, Linux, Mac OS X and Flash, which were developed by Phil Hassey. It is set in space and involves maneuvering fleets of ships to capture enemy planets. Based off of 'Tsuru no Ongaeshi,' the Japanese folk story more commonly known as 'The Crane Wife,' this visual novel was made in less than 48 hours for Ludum Dare 24, August 2012, with Ren'py. In 'Inu no Ongaeshi,' you play a lonely freelance writer, if, with your lack of motivation, you can even call yourself that. “(R,G,B)” is a minimalist adventure game I made for Ludum Dare 26.The theme was “Minimalism”. My goal was to experiment with pure color values and explore the flat 2D space while keeping the game itself very simple.
data
directory, you wrap it using the following function:An example of usage would be
This is mostly for convenience – it allows you to access your resources while developing, but then it’ll add the right prefix when it’s in the deployment environment.
font = Font(None, 26)
. Instead, use something like font = Font(resource_path(os.path.join('data', 'freesansbold.ttf')), 14)
..spec
file.--onefile
).--windowed
)..spec
file so that you add your data
directory (note that these paths are relative paths to your main directory.exe EXE = (pyz,
and add on the next line:app = BUNDLE(coll,
and add on the next line:.exe
or your .app
bundle in the dist
directory.Phew! That took me a long time – the better part of a few hours to figure out. This post on the PyInstaller list really helped.
So why was I trying to package a Python executable file anyway? Read on…
This weekend, I decided to participate in a 48-hour game design “competition”. Ludum Dare is a compo that asks you to create a video game from scratch in a 48-hour time period – you have to write your code and create all of your assets in that time period.
This means no reusing graphics, pictures, music, or sound from other projects, for example. You’re also not supposed to reuse code either. I decided to participate on the Thursday the day before. Most people use the previous weekend as a “warmup weekend” to test their tools, get some practice, and so forth. (My entry is located here, by the way).
I’ll do a more detailed compo writeup later, but I just want to concentrate on one thing that kept me up for hours after the competition: getting a Windows executable created from a Python project that uses PyGame and a data directory.
I rather enjoy Python as a programming language. The syntax is reasonably concise, the language does a lot of things for you, and it’s well-laid out. There’s also a lot of good support in the form of third-party libraries. I’ve been using Python for various things for the past few years (usually small scripts for data extraction and analysis in research).
One thing I had never thought about before was distributing a Python project as an executable package, and while it was on my mind throughout the entire compo, I didn’t actually learn the process of creating the package until the last hour of the comp before submission. After you submit your primary platform, Ludum Dare allows you around 48 hours to compile for Windows, since the majority of reviewers use Windows.
The ideal submission is a single binary file (an .exe file for Windows) that doesn’t have to extract a lot of data, so that it’s easy for people to download and run your game.
I went on a wild goose chase trying to find out how to make a single executable file out of a Python project that would include all of my data assets. I first tried py2exe and py2app. py2app mostly worked all right, but py2exe was a pretty big mess.
The end story is that PyInstaller is newer and shinier than py2exe, and that you need to secret sauce code that someone out there on the Internet found before I did. PyInstaller basically runs EXE files by extracting the assets into a temporary data file that has a path _MEIPASS in it ((technical details here). Be sure that you check that every file is loaded in through that wrapper. The Tree() TOC syntax was also confusing, but basically, it’s the relative path of your data files and it will automatically load all of the files in that directory. Make sure it exists in the EXE portion (Windows) or the APP portion (Mac).
There’s a Make/Build cycle in PyInstaller to generate the spec file and build it in a single step as well – I find it easier to do that to generate the spec file and do an initial binary run, then to modify the spec and run PyInstaller again with the spec file as the argument. PyInstaller is pretty smart about rebuilding, and you save a lot of time.
I think in the long run, if you compare py2exe, py2app, and PyInstaller, PyInstaller is the program worth learning. It did have a pretty sharp curve for me – it didn’t help that I was trying to do this late at night after a challenging weekend!
If you do wish to use py2app to build your Mac OS X application bundle, then do keep in mind that you need to have a import pygame._view
because of some kind of obscure issue.
Anyway, that’s all there is to this post for now.
Here’s the setup.py I used for py2app.
Here are some of my mini-games. Most of them were made for the Ludum Dare, a 48h or 72h game development competition.
Flamekeeper | |
A puzzly adventure where you have to keep the flame alive and light the big cauldron! Made in 72 hours for Ludum Dare 46 (2020) Version 1.04 (2020-12-05) | PLAY NOW (HTML5) Download: Windows |
Death Dungeon | |
Old school dungeon crawler! Made in 48 hours for Ludum Dare 44 (2019) Version 1.02 (2020-04-04) | PLAY NOW (HTML5) Download: Windows OSX Linux |
Isn't that crate? | |
A game of crate pushing and logic puzzles! Made in 72 hours for Ludum Dare 34 (2015) Version 1.01 (2018-09-12) | Download: Windows OSX Linux |
Bragmor the Barbarian | |
Fight hordes of enemy creatures in this action-packed 3rd person 'crusher'! Made in 72 hours for Ludum Dare 27 (2013) Version 1.03 (2019-12-22) | Download: Windows OSX Linux |
Balls! | |
'Balls!' is a 3d physics-based puzzle/platform game. Its minimalistic gameplay: just push the balls into the goals! Made in 72 hours for Ludum Dare 26 (2013) Version 1.01 (2018-09-15) | Download: Windows OSX Linux |
Korruptor | |
In this game you play a demon sent to earth to 'corrupt' people with heavy metal! Made in 72 hours for Ludum Dare 25 (2012) Version 1.01 (2018-09-15) | Download: Windows OSX Linux |
Temple of Evolution | |
A 3rd person action-adventure game in which you'll have an epic chase, awesome platforming AND a puzzle game! All that in full 3d made with Unity. Made in 72 hours for Ludum Dare 24 (2012). Version 1.01 (2018-09-17) | Download: Windows OSX Linux |
Tiny Shogi 48 | |
A smaller board variant of shogi (Japanese chess) made accessible by using self-descriptive symbols on the pieces. Made in 48h for Ludum Dare 23 (2012). Version 1.01 (2019-04-24) | Download: Windows OSX Linux |
Alone in the Scary Darkness with Zombies! | |
A survival horror game. Kill the 10 zombies to win the game. Ludum Dare 22 (2011). Entered in the 72h jam category. Version 1.01 (2019-04-25) | Download: Windows OSX Linux |
A Helicopter Named Freedom! | |
Pilot a helicopter to help the bad guys escape from prison. A Ludum Dare 21 48h game!(2011). Version 1.01 (2020-04-04) | Download: Windows |
Exterminate 48 | |
Dodge the enemy fire and trick them into destroying each other. Ludum Dare #18 (2010). Version 1.01 (2019-12-21) | Download: Windows |
Tropical Islands of Doom | |
Help the penguin find all the treasure chests. Ludum Dare #17 / 48h(2010). Version 1.01 (2019-12-21) | Download: Windows |
Explo 48 | |
My first Ludum Dare entry made with Unity. You need to use the clues available to you to guess where the invisible platforms are. Ludum Dare #16 (2009) Version 1.01 (2020-04-04) | Download: Windows |
Caverns 48 | |
A mini 2D platformer made in 7 hours for Ludum Dare #15 (2009). Playable but very rough. Made with Python/Pygame. | Download (Windows .exe / 3.2MB) Download (Python source / 30KB) |
The Tower 48 | |
Entry for the August 2008 Ludum Dare. Made from scratch (C++ / OpenGL) in less than 48 hours. 3D Platform game | Gameplay video Download (Windows .exe / 3.4MB) |
Lightstream 48 | |
Entry for the April 2005 Ludum Dare competition. Made from scratch in less than 48 hours. Move blocks and mirrors to direct the light and solve the levels. | Download (Windows .exe / 1.3MB) Download (Mac OS X version / 1.3MB) |
Ring 48 | |
Entry for the October 2004 Ludum Dare competition. Made from scratch in less than 48 hours. Race along a randomly generated ring-shaped track while avoiding obstacles and holes in the ground. | Download (Windows .exe / 4.76MB) |
Darx | |
A Klax clone made for a Dark Basic remakes competition. In this puzzle game you have to arrange bricks so that they make 3 of the same color in a row. | Download (Windows .exe / 2.7MB) |
Bouncy 48 | |
A mini game made from scratch in less than 7 hours for Ludum Dare's April 2003 competition. Place blocks at the right spots so that the arrow will bounce around to the exit without hitting any bomb. | Download (Windows .exe 1.06MB) Download (Python source 14KB) |
Guardian Angel | |
My first participation to a Ludum Dare 48 Hours game programming contest. As a guardian angel, you have to save the main character from all sorts of urban dangers. Ludum Dare #1 (2002) UPDATE February 5th, 2013: repackaged with a recent version of PyGame to fix the sound problems. | Download (Windows .exe & Python source 6.3MB) |