What are ways to add online multiplayer game ability to a Kivy app?

1,348 views
Skip to first unread message

Cliff Wang

unread,
Dec 28, 2014, 11:30:17 AM12/28/14
to kivy-...@googlegroups.com
Dear user group,

(I apologize if this question is too broad for this forum. My question was booted from StackOverflow for that reason).

I am new to programming (e.g., I just figured out binding)  and as a learning project, I am writing a Scrabble-like, turn-based game using Python3 and Kivy. I would like to eventually have my game run on iPhones and Android phones and allow two players to find and play each other online. I am having a hard time figuring out exactly how to get started. Could someone suggest the most straightforward way to add the online multiplayer functionality? I have discovered Google Play Game Services (which doesn't support Python3?), but am unsure if this will work well (or be easy to figure out for a new programmer) with Kivy.  Perhaps there is something like Google Play Game Services that will work more easily with Kivy? I would appreciate any guidance.

Thank you much,

Cliff

Marco Gazzetta

unread,
Dec 28, 2014, 3:53:12 PM12/28/14
to kivy-...@googlegroups.com
Hey, Cliff,

Did you look at tito's Kivy 2048 on github? https://github.com/tito/2048 . If I am not mistaken, it uses the Google Play Game Services SDK for achievements. In general, interfacing with a third party Java SDK is one of the things Kivy does really, really well.

I would assume GPGS is not necessarily available on iOS, so you may want to check that out before you decide to go that route, just in case you actually want to publish your game on iOS, too.

Just a suggestion off the top of my tiny head: since your game seems to be turn-based, did you consider using email or SMS to exchange turn information? The advantage would be that it works universally without relying on external infrastructure.

$0.02
Marco

Cliff Wang

unread,
Dec 28, 2014, 5:22:11 PM12/28/14
to kivy-...@googlegroups.com
Thanks for the 2c. I believe Google Play Game Services supports iOS to some degree now. However, if doing it by email is a better approach, I can definitely learn how to do that. I just thought that something like Google Play Game Services would we be better because I wouldn't be remaking basic handling tasks and that security would be better.  I would hate to have people's email addresses broken into because of a game I made. I assume someone has already written a turn based online game in Kivy?  If so, I would be open to hearing other suggestions. 

I did see the Tito 2048, but didn't quite absorb it yet. So the key is to use Kivy to interface with Java SDK and not to figure out a Python SDK?

Thanks again for the quick reply.

Cliff

Marco Gazzetta

unread,
Dec 29, 2014, 12:06:41 AM12/29/14
to kivy-...@googlegroups.com
$0.02 more, then...

What do you know, there is an iOS implementation of GPGS. Thanks for pointing that out. It appears it's an Objective C library, so you should be able to use pyobjus to wrap it. If you don't mind, it would be great if you posted a sample of your wrapper so that others (including me) can learn from your brilliance.

And you are correct: Kivy has facilities to make native SDKs available in Python. That includes callbacks in Python, too. I just checked the Kivy 2048 code, and it does indeed implement GPGS using that approach. As far as I can tell, it's Android only at this point, but potentially making it work on iOS might not be a big problem at all.

Cliff Wang

unread,
Dec 29, 2014, 10:02:15 AM12/29/14
to kivy-...@googlegroups.com
So sounds like my homework is to learn pyjnius and then pyobjus. I'm confused though, does this mean that GPGS does not support python directly despite this: https://developers.google.com/api-client-library/python/apis/games/v1 ? Or maybe this only supports python 2.7, which I should avoid at this point?

I'm far from brilliant and no one should be following my coding examples right now, but I would be happy to contribute if I can get that far. I may need some external brilliance for help.

Cliff

Marco Gazzetta

unread,
Dec 29, 2014, 11:06:29 AM12/29/14
to kivy-...@googlegroups.com
Nah, Python 2.7 is mighty fine. The problem might rather be that the library is only supported on desktop OSs, as the page states:
  • Operating systems:
    • Linux
    • Mac OS X
    • Windows
I looked at the downloadable, though, and it is Python only. Also, it lists as only requirements

httplib2>=0.8
oauth2client>=1.3
uritemplate>=0.6

So you may very well use it / try to use it on Kivy. You'd have to package the library and unavailable dependencies with your Kivy app, but that may really be much easier than going the native route. I also note that the release date of the package is mid-October: it may well be that tito would have used the Python library had it been available when he wrote 2048.

Personally, I would probably start out trying to package the most simple app possible: maybe a button that connects to GPGS when pressed. That way you get an idea of how to include all the required dependencies into your package. There is one example that comes with Kivy in which Twisted is included. It may be helpful to your efforts.

Once you get GPGS/Python running on Android, you know your approach will work and you can continue with game development! Post a success story, please! It would be really awesome if we can get a pure Python library for Google services working for Kivy.

Cliff Wang

unread,
Dec 29, 2014, 11:37:28 AM12/29/14
to kivy-...@googlegroups.com
Great. This has been helpful. I have been a cancer wet-lab researcher and only started programming now that I have some time off between careers.  I've gotten relatively far, but every thing I learn is completely new. Basic things that take you an hour take me two weeks because I have learn fundamental principles. I really appreciate the help and am so impressed by the development community.

Another question: if I am able to figure out the GPGS/Python/Kivy, will it matter that my game logic is in Python3? Will I need to convert it to Python 2.7? I know that if I currently just try to run the console version (without Kivy) of the game in Python 2.7, it doesn't work, but I could probably change things to make it work. Thanks. Knowing this could be helpful as I attempt the next steps.

Marco Gazzetta

unread,
Dec 29, 2014, 12:17:32 PM12/29/14
to kivy-...@googlegroups.com
Congrats to you! It's never easy to start something new, and programming is one of the worst habits to pick up...

You can approach the problem of Python versioning in many different ways. I would suggest you go the path of least resistance (since there is already enough resistance):
  1. create a simple sample (i like the phrase!) using Python 2.7 and the GPGS Python extension (with dependencies) for 2.7
  2. switch Python 3.0 for 2.7 on the desktop and see if it still runs; if not, figure out the modifications required
  3. switch Python 3.0 for 2.7 on the mobile app and see if it still runs; if not, figure out the mods required
  4. add GPGS to your game

I have a hunch, given you are new to programming, that modifying your original code to work in 2.7 may just be much easier. There is probably less code to look at and modify, and you are probably not using a lot of features available only in 3.0. But it's just a hunch, and only you know how much code you wrote.

If you go that route, the project plan might look like this:

  1. make your code work with Python 2.7
  2. create a sample app that uses the GPGS and build/deploy/test on Android
  3. integrate GPGS into your game

Gabriel Pettier

unread,
Jan 1, 2015, 2:19:03 PM1/1/15
to kivy-...@googlegroups.com
Hi

There are a lot of ways to make a multiplayer app, and turn based does
make a lot of things easier, if you can make use of google play game
service, it will still certainly save you a lot of pain (as writting a
server with player authentication, scaling when you get more players,
and so on, can be a real hurdle, as well as keeping it alive so your
players can play at all).

If it's your first project, i must say it's quite a serious one, and
it'll certainly keep you busy for months if not years, so in a similar
note to Marco, i'd advise doing very simple tests of the various bits to
see how they work (including kivy, so your engine works with bindings
and other asynchroneous logic), then figure how to arrange them
together, if you go too far with any part at first, you may end up with
pieces that don't fit, and give yourself a lot more work.

Also, yes, currently kivy only supports 2.7 on android/ios, so you'll
want to retrofit your code into using it.

Anyway, it's a fun journey you are starting, i can only say good luck :)

Oh, and show us your progress! ;)

On Mon, Dec 29, 2014 at 09:17:32AM -0800, Marco Gazzetta wrote:
> Congrats to you! It's never easy to start something new, and programming is
> one of the worst habits to pick up...
>
> You can approach the problem of Python versioning in many different ways. I
> would suggest you go the path of least resistance (since there is already
> enough resistance):
>
> 1. create a simple sample (i like the phrase!) using Python 2.7 and the
> GPGS Python extension (with dependencies) for 2.7
> 2. switch Python 3.0 for 2.7 on the desktop and see if it still runs; if
> not, figure out the modifications required
> 3. switch Python 3.0 for 2.7 on the mobile app and see if it still runs;
> if not, figure out the mods required
> 4. add GPGS to your game
>
> I have a hunch, given you are new to programming, that modifying your
> original code to work in 2.7 may just be much easier. There is probably
> less code to look at and modify, and you are probably not using a lot of
> features available only in 3.0. But it's just a hunch, and only you know
> how much code you wrote.
>
> If you go that route, the project plan might look like this:
>
> 1. make your code work with Python 2.7
> 2. create a sample app that uses the GPGS and build/deploy/test on
> Android
> 3. integrate GPGS into your game
>
>
> On Monday, December 29, 2014 8:37:28 AM UTC-8, Cliff Wang wrote:
> >
> > Great. This has been helpful. I have been a cancer wet-lab researcher and
> > only started programming now that I have some time off between careers.
> > I've gotten relatively far, but every thing I learn is completely new.
> > Basic things that take you an hour take me two weeks because I have learn
> > fundamental principles. I really appreciate the help and am so impressed by
> > the development community.
> >
> > Another question: if I am able to figure out the GPGS/Python/Kivy, will it
> > matter that my game logic is in Python3? Will I need to convert it to
> > Python 2.7? I know that if I currently just try to run the console version
> > (without Kivy) of the game in Python 2.7, it doesn't work, but I could
> > probably change things to make it work. Thanks. Knowing this could be
> > helpful as I attempt the next steps.
> >
> > On Monday, December 29, 2014 8:06:29 AM UTC-8, Marco Gazzetta wrote:
> >>
> >> Nah, Python 2.7 is mighty fine. The problem might rather be that the
> >> library is only supported on desktop OSs, as the page states:
> >>
> >> - *Operating systems*:
> >> - Linux
> >> - Mac OS X
> >> - Windows
> --
> You received this message because you are subscribed to the Google Groups "Kivy users support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

signature.asc

Cliff

unread,
Jan 1, 2015, 3:37:40 PM1/1/15
to kivy-...@googlegroups.com
Wow. It seems like a much harder task than I anticipated. I thought it would be not too bad, as I only need to pass a data file back and forth (well and then a few other things).  It's definitely concerning. Yes, I'm a beginner (like I don't know what Twisted is yet). Is Marco's first suggestion to use email a better way to go? Or, in your opinion, is the Google Play Services Python 2.7 route one that will eventually work? I would hate to invest months on it to find out that it was never going to work in the first place. Are there any prior Kivy examples of such an online interactive program?

Thanks,

Cliff

Gabriel Pettier

unread,
Jan 6, 2015, 6:23:57 PM1/6/15
to kivy-...@googlegroups.com
It's a hard task for a beginner. And it's far from impossible, you'll
just learn a lot of stuff along the way, so whatever, it won't be wasted
:).

Of course, players could certainly exchange a file, you could associate
a file type to your app, so it opens it when user gets the mail, and
send a mail on behalf on your user when his turn is done, it'll be a bit
cumbersome, but nothing preventing it to work.

I'd still advise trying to use Google play service, or any equivalent
service to pass messages between two clients, because there are a few of
them (but i'm not the best placed to advise for one to use).

A good way to be sure you don't waste months on a way that's not going
to work, is to try a service with a very simple test, even simpler than
any chat program, have two "player" register to the service, and
exchange a message, or store a value, and have the server process it to
update them, depending on how the service you use work, if it work, you
know you can start using that with your programs. The whole projects
will likely take months, but you can be sure there will be a result if
you check each part is feasible in the first place. Experimenting is
key, polishing is what takes the most time :).

Twisted is "just" a network-oriented event framework for python, it's
big and powerful, one of the most impressive things, really, but may be
a bit hard to get into if you are just starting with programing, they
have much better examples these days, though, so you can certainly get
what you need from it without understanding all of it's concepts.
> > an email to kivy-users+...@googlegroups.com <javascript:>.
signature.asc

Gabriel Pettier

unread,
Jan 6, 2015, 6:30:56 PM1/6/15
to kivy-...@googlegroups.com
I may have been a bit pessimistic in my previous mail, btw, probably
because i spent years on a slightly more complex project (a smash-bros
like game) that ended up dead when i lost the last remains of my
motivation for it, i'm still happy i worked on that, because i learned a
lot there. Your project is certainly less complex, but as a first
projects, to say it'll keep you busy a few months, even if it's far less
than a year, is not, imho, an understatement. Of course, that's if it
works well for you, if it doesn't, you'll certainly stop long before
that :)).

The only failure is not trying ;).

On Thu, Jan 01, 2015 at 12:37:40PM -0800, Cliff wrote:
> > an email to kivy-users+...@googlegroups.com <javascript:>.
signature.asc

Cliff

unread,
Jan 6, 2015, 8:19:28 PM1/6/15
to kivy-...@googlegroups.com
Nah. I read it as you being realistic. There is indeed much to learn. Last week I had no idea what oauth or json were. This week my head hurts from trying to figure out json and getting Google APIs to execute, which I assume actually is not a big deal once you are up and running with it. The hardest thing is the lack of examples/documentation for Python and the Google APIs. Learning Python was easy because there are books and everything you ever wanted to know is on Stack Overflow. Learning Kivy was a bit more challenging, but the documentation and examples (plus friendly community answering my stupid questions) made it possible with persistence. So now I guess I'll try Google Game APIs, which *I think* has no Python examples because it is relatively newly available for Python. Yeah, I'll either get either get stuck and give up (and start looking for a job in biotech, my official expertise), switch to doing it by gmail (which has enough Python examples for me to figure out), or just learn Java/ObjectiveC or whatever the traditional route is. I only chose Python because I'm a scientist and thought it was something I should know --- and then I started programming my little game as a learning device. What a rabbit hole programming can be and these monitors are f-ing with my sleep cycle I think!....  Thanks for the encouragement.

Cliff

Cliff

unread,
Jan 9, 2015, 5:53:57 PM1/9/15
to kivy-...@googlegroups.com
Dear Kivy community,

Just in case there are people reading this thread, I posted this basic question on StackOverflow:


If you happen to have familiarity with Python and Google Client APIs, any tips would be welcome.

Cliff

Marco Gazzetta

unread,
Jan 9, 2015, 8:32:15 PM1/9/15
to kivy-...@googlegroups.com
Aloha!

Was out for a while, and got a chance to read up on the docs.

You haven't mentioned this page from the Google docs, which I believe should be your starting point:

https://developers.google.com/games/services/common/concepts/turnbasedMultiplayer

It appears that you invite people you know by using Google+ invites, which may or may not be what you want. If you are as active on Google+ as I am, probably rather not. You also have the option of auto-inviting players, which means that you specify a number of players to join and whenever someone joins an auto-play game, they may be matched to yours and start playing there.

The APIs are available in a number of forms. The ones listed are the Android SDK (duh!), the iOS C SDK, and a REST API. The Python SDK is not mentioned, much in line with Google half-a**ed approach to Python on Android. I would think the Python SDK is simply a wrapper around the REST API, which means you can probably make it work on Python 3.0 fairly easily if you still so desire. (In case you don't know REST, it's just a series of calls involving

In addition to active calls, a series of notifications are mentioned, which means you would have to provide some form of callback mechanism. In Android, that's fairly simple (you register an intent listener). It's probably not a whole lot harder in iOS, I just don't have the exposure.

The SDK also lists a series of functions it provides directly (like the list of games the player is currently engaged in). Those are most likely directly exposed in the SDK, including in the Python SDK.

The logic seems to be: (a) invite other players, either using Google+ or auto-invite; (b) active player sends turn data; (c) inactive players receive notification of turn data; (d) next active player is selected (by the game, presumably); (e) go to (b). This repeats until the game is over.

As a developer, you write the initial phase of the game, and you react to notifications on turn changes and game events, and you send updates when the player did his/her part.

Email, in general, should be your last resort option, since there is no unified email mechanism. SMS is unified, but has obvious limitations in amount of data transferred per message. The advantage in both cases is that invitations can just occur from your existing Contacts data. So SMS games would work if your turn data is small and you don't want auto-invites. You'd have to warn users, though, that they may incur charges.

Your concern about security is understandable in general. In this particular case, though, you'd be only sending turn data to a known recipient. For safety, you could ensure that a game invitation comes from a known Contact, so you don't start playing with random people.

As is usually the case, your particular circumstances determine what's best for your implementation. I like that you continue posting questions - people can follow your implementation efforts, which helps understand the pitfalls, hurdles, obstacles, and finally triumphs of implementation. Good luck!

Marco

Cliff

unread,
Jan 10, 2015, 9:58:48 AM1/10/15
to kivy-...@googlegroups.com
Mahalo Marco. 

Taking your original suggestion, I went ahead and converted my program to Python 2.7. It wasn't too bad and it runs now. One interesting thing is the blurry fonts that I observed in Python3 Kivy have gone away and the fonts are sharply rendered now.

Just before I read your reply I had been starting to lean towards using gmail and not GPGS. Sigh. Gmail support in Python appears to be good and I can see a path, however convoluted, for making it work. You are right that  although Google touts Python support, it's not enough of a priority for them to provide functionality or ease of use. (hm, so much exposure to Americana that arses are starred the world over). The Android SDK for example provides additional turnbased multiplayer methods 


and I find nothing analogous for the Python methods available. for example in Android there is a:

public abstract Intent getSelectOpponentsIntent (GoogleApiClient apiClient, int minPlayers, int maxPlayers)

that help with player invitations by presumably finding a list of opponents from your Google+ contacts and providing them as options for the player to select. I have not been able to find something similar for Python, which I guess means they assume you  can write one for yourself, though I currently don't see the solution. As you mention, there are also things like listening for and implementing notifications which is also worked out with methods in Android but not Python.

So, if email should be a last resort, does anyone know of a (better) Python supported service that can handle basic multiplayer turned based matches? I think I can only blindly poke at the GPGS Python APIs for so long.

Ur. Kr.

unread,
Jan 11, 2015, 12:05:27 PM1/11/15
to kivy-...@googlegroups.com
"One interesting thing is the blurry fonts that I observed in Python3 Kivy have gone away and the fonts are sharply rendered now."

I think text is blurry when the label is on a float position instead of an integer position. In python 3 when you say, for instance: 3/2 you get 1.5, whereas in python 2 you'd get 1 because the result would not be converted into a float automatically. 

To get the same effect in python 2 you'd have to either say 3.0/2.0, or float(3/2). So if you're doing any division with integers when calculating the positions of your labels, you'll get integers in python 2 and floats in python 3. 

When you ported to python 2, you probably didn't convert the calculations into float calculations, and so your labels all have integer positions, and are thus sharp.

I think this is important to note, because you might get confused when things that worked in python 3, don't immediately work in python 2 when ported, due to this difference

Cliff

unread,
Jan 11, 2015, 12:33:17 PM1/11/15
to kivy-...@googlegroups.com
Thanks. You are probably correct. I had been trying to use the // operator, that I thought would have been doing integer division and lopping of the decimal places, but it is definitely possible that elsewhere in my code I omitted this accidentally. Also, in my Python 2, I used "from future import division" or something like this. I'm not sure how it all played out though and I  suppose that's how one gets into trouble.

Damien Moore

unread,
Jan 12, 2015, 10:34:44 AM1/12/15
to kivy-...@googlegroups.com
Personally, I wouldn't give up on using the google services python API. E.g. this looks like it has most of what you need:

Getting started
https://developers.google.com/api-client-library/python/start/get_started#auth

Play Services API
https://developers.google.com/resources/api-libraries/documentation/games/v1/python/latest/

and specifically, turnBasedMatches:
https://developers.google.com/resources/api-libraries/documentation/games/v1/python/latest/games_v1.turnBasedMatches.html


Also, this isn't exactly what you want but I found it educational to write my own LAN game, and some of the concepts carry over to the web, so maybe this is useful:

https://groups.google.com/forum/#!msg/kivy-users/k21VSMUxFEo/1rReBLoddHAJ

(The network code could be a lot more robust but it works for the most part.)
Reply all
Reply to author
Forward
0 new messages