0

I have written a python game using pygame module. It runs fine from terminal.

I have a game directory (let's call it myGame) which contain all the resources(images and sounds) and all the python scripts which use those.

When I run the game through terminal (let's say python3 mainPythonModule.py), the game runs fine.

I tried to create a game.desktop file. But when I click it, a black window appears and crashes almost instantly.

I am using Ubuntu 16.04 LTS. And my game is written in python3.5 and uses pygame module.

My game.desktop file looks like this

I don't want the solution answered for this question because I still have to type in terminal.

I would like to create a desktop applcation, such that by clicking on the icon, the game would start. Just like when we download any game, after installation, we just click on the game icon and it runs, I want something like that. How can I do that?

Edit : I don't think this question is a possible duplicate of this question. I want to know how to develop a desktop application in Ubuntu 16.04, such that by clicking on some icon, the game would start.

Final Edit : First of all, I would like to thank everyone who responded. I really am sorry for wasting your precious time.

The issue is resolved. The problem was all the resource (images and sounds) were loaded via relative path and not through absolute path. Now the game.desktop file runs fine.

alphax
  • 3
  • 1
  • 4
  • Possible duplicate of [How do I execute cmd commands through a batch file?](https://stackoverflow.com/questions/16727941/how-do-i-execute-cmd-commands-through-a-batch-file) – TheNavigat Dec 07 '17 at 09:55
  • This might help: https://stackoverflow.com/questions/3957717/how-transform-a-python-program-py-in-an-executable-program-in-ubuntu –  Dec 07 '17 at 11:21
  • The way to go is to do as they said, add the first line with the shebang and make it executable. Or, otherwise, write a shell script that calls the Python interpreter or something like that, if you need more preparation before running the program. Then, create the `.desktop` file that calls it. There are plenty of applications that do either of these. If it is not working for you maybe you can provide more details about what you have and the behavior you are seeing. – jdehesa Dec 07 '17 at 12:03
  • in `game.desktop` you have to use full path to python file. If it loads some images then in code you have to use `sys.argv[0]` get full path to folder with images and use it when load files. Without this it will search images in folder when you run `game.desktop` - ie. it can be home folder or desktop folder. – furas Dec 07 '17 at 15:31

1 Answers1

0

I think what are you trying to do is some kind of launcher.

Here you have my repo, it contains a Tkinter GUI with a PyGame call on a button.

Feel free to study/use it as you want, look at the file desktop_application.py and if you don`t care about the coin part you can just skip that and take what you need.

https://github.com/FilippoLeone/PyGameLauncher

And you can compile everything as an executable with PyInstaller.

Install PyInstaller from PyPI:

pip install pyinstaller

Go to your program’s directory and run:

pyinstaller yourprogram.py

F. Leone
  • 594
  • 3
  • 14
  • I have not gone through entire code, but I guess I would still need to run desktop_application.py from terminal. I want some kind of icon, whereby when we click it, it runs the application. I don't want anything to do with terminal. Just like when we download any game, after installation, we just click on the game icon and it runs, I want something like that. Thanks – alphax Dec 07 '17 at 11:47
  • You can easily compile that program into an executable file as i did and then you can create a symlink with your own icon to make it user-friendly. I suggest you to use PyInstaller as it is easy to use and it`s tested on my repo aswell. – F. Leone Dec 07 '17 at 11:51
  • Here is what it looks like compiled on Ubuntu 17 https://i.imgur.com/tmFUJL9.png Where app is your launcher/game executable. – F. Leone Dec 07 '17 at 11:56
  • I did as you said, but my program crashes as soon as I click on the launcher. Same thing happened when I created a .desktop file. Can you help me? – alphax Dec 07 '17 at 12:29
  • Please provide more details, what did you end up using and how. In that way we can provide you additional help. – F. Leone Dec 07 '17 at 12:40