How do I create a multiplayer game?

Hey! How do I create a multiplayer game?

Master help me!

We are here to help you help yourself - so when you have a specific problem with a specific piece of code, we will be there.

You will find numerous example programs, complete with code, in the App Inventor Gallery.


1 Like

Here are several examples using CloudDB and FireBaseDB, they are tutorials in Spanish, but you can download the .aia files and install them on two devices to test them.

hi Thank you your teach

Hi! I’m also interested in creating a multiplayer gama, but I’d like going a step beyond those examples, and don’t know exactly how. Let me explain myself: I’d like that a potential user of the app access to the app and give his availability to play with others, and in the same way he can see a list of the available players in order to choose one of them… I think I’d know more or less how to do that with the FirebaseDB component, but no idea how to start the DB values once the people leave the app or simple turn off the phone… Sorry, I don’t know if I explain myself clear enough. Any helping hand is more than welcome!

If you need some help with FirebaseDB then here you go
check this link

This might require the app to repeat its announcement of availability periodically (once a minute?) to the central database, with user name and current timestamp.
The central database would keep a tag for a list of all the recent (last 5 minutes?) announcements.
The announcement code would responsible both for adding the current (re)announcement and for purging oldest (stale) announcement(s) from the central list of announcements.

1 Like

Thanks ABG, that’s a great idea! So even if I leave the app and then re-enter later (for example, the next day) I could delete all the entries that don’t correspond to the let’s say 5 last minutes. I’m going to try this, and I’ll come back if I have any problem with the implementation.

Thanks a lot again!!!

Hi everybody! I followed the indications by ABG and finally got it! I was able to create a very preliminary list of the people involved using the app in different devices, and choosing one of them, both players (the one who chose, and the one chosen) start the game.

Even when I need to improve that process (checking names to avoid repeating and things like that), I’m facing a new problem with the multiplayer situation. My first idea was to implement the famous kind of simple tennis in which each player manages a “racket” and push against the other a ball avoiding being surpassed. I was able to get a good communication between the movement of the two rackets in both devices but my new issues are related to the common factor: the ball.

If the movement of the ball was 100% determinated for fixed ex-ante physics rules, theoretically I should see the same position for the ball in both devices (I think it’s not exactly so because the different dimensions of both phones… but I think it’s something I can solve working in relative terms related to each screen). But what if I introduce in the movement a random component? This random component should be exactly the same (what is the opposite idea of being random! :wink: in both cases, otherwise the movement of the ball will be different in each device.

I’m thinking about setting in advance that one of the devices is the MASTER and the another is the SLAVE, and so the movement of the ball is computing for the first one and the second one is who reads from the common DB (but even in this case the MASTER should be “listening” very carefully to the SLAVE because the hits with the racket in the SLAVE device)… So I’m not sure if it’s a good idea.

Again, any idea on this topic or any suggestion in general is more, more, than welcome!

Doing a 2 player Pong with Ai2 over FireBaseDB in real time would be a project worthy of a MMO programmer.

Master this, and you could make the big bucks.

I’m not a part of that world, but I have some AI2-level ideas you could try …

You are going to have delays, no matter what you try, so don’t go for continual tracking of the ball movement. That is probably doomed to fail, and would expensively flood whatever FireBase server you are using.

The least possible message frequency you would need is to send or receive a message for each paddle hit or miss.

Differences in device shape between opponents is a problem. A player with a long thin device might see the ball bounce several times off the sides before it reached the opponent, but the opponent’s device might not have the length to support that number of bounces. Incidence angles and (relative) x,y paddle locations and bounce locations would differ across players.

So a message from an opponent should describe

  • the opponent’s Canvas Height and Width,
  • the opponent’s paddle size and location at strike or miss time in their coordinates,
  • the sequence of edge bounce locations the ball hit on its way to you,
  • the location on your edge where it will hit and the hit angle
  • the ball speed

At this point, you will need to use some tricks from Albert Einstein (relative geometry in space and time) and a stage magician (showing the user something plausible while hiding what is really happening.)

I’m going to suggest that the time to send a message to the server is when the ball arrives at your paddle, but before you let the player see what the results are from your paddle hit/miss, even before you see the ball leave your paddle. I’m guessing at that moment you have exhausted your attention to the ball and are resting for an instant while the ball moves away from you. In that Event when the ball hits your edge (and paddle, hopefully), you must extrapolate the return path of the ball (including its speed and expected wall bounces and arrival location and angle at your opponent’s side), and transmit that information to the server.

You must hold the ball in place and not let the ball bounce until you have received a return message from the server with what happened on your opponent’s side, as described above.

When the return message arrives, only then does your app send the ball off your paddle along the extrapolated path, bouncing off edges as extrapolated, and bouncing off the opponent’s paddle (or not), and bouncing off the walls on its way back to your paddle, until it reaches your edge, where hopefully you have moved your paddle to meet it. During this show, the app should move the opponent’s paddle image to where it was when the ball reached his side.

The MoveTo block is good for this, when combined with ball Speed.

To avoid some trigonometry extrapolating the path of the ball bouncing off your paddle, you could send a super-fast hidden 1 px Ball to bounce off the paddle and track its edge bounces until it reaches the opponent wall, and send its path as your message.

This scheme allows for an unlimited server delay, experienced as stickiness in the user’s paddle.

1 Like

Here is the tutorial on this topic have a look