Snap! Online Shooter

I used the MQTT library to make a small online game.

Arrow keys or WASD to move the player. The green player is you.

Kill the other player. Just shoot the enemy (red square) enough times.

It might take a while to find someone to play with (if someone is online at all). Just wait. The screen might flicker a few times. That's normal.

Everything in the game was made in Snap! (Including the artwork)

Nice. I'm curious: P1 is always you and P2 the other guy, right? So why doesn't P2 just start out red instead of changing color in the code?

Was too lazy to make a new red square. I just used the color effect.

I played a few times and every time I lost :cry:
Does that mean that someone else was actually competing, or will the game spawn a bot adversary if it takes too long to wait for a competitor?

Matchmaking works like so:

  • Connect to a topic on a broker
  • Listen to the topic for a while (find other players waiting for people to join)

If the system finds a player

  • Send a message to the server ("server" being the person who created a new "room"; you'll know if you were the server owner if you spawned in on the left side) indicating that a player is joining
  • Both computers agree to meet each other at a specific time (the black screen; your computer is waiting to start at the time specified)
  • If one of the players does not connect in time (usually a severe error and one of the players has to quit, or a hacker was detected and kicked from the game) then the other player will move back into the matchmaking phase

If no players were found

  • Create a new "server" (can be joined when another player is at the "listening" phase)
  • Wait for other players to join
  • If people still do not join, start a game with a bot

If the game starts but the game keeps erroring out (usually a hacker, on normal circumstances you'll usually only get one when you first start the game, I don't know why)

  • Spawn a bot

I plan to make it so that if you can beat the bot its aim will slowly get worse and worse.

Also, when you are in a match this is what's happening behind the curtains:
When the game starts, both players send "packets" indicating their position, mouse position, etc.
When you receive a packet, some variables are updated on the enemy's velocity and gravity. The game will predict where the player is while it waits for the next packet (usually around every 0.3 seconds a new one is received). That's why if you are playing with a real player, you'll occasionally see people fall off edges and magically teleport back to the top of the edge.

If you wanted, you could make a great tutorial on client/server programming by taking this program and tearing it apart, with snippets of code followed by text explaining each snippet.

PS That big comment used to also be in the project notes, which are now empty!

The project notes wouldn't save for some reason.

It's more like creating a room and having a player join it, I just called it a "server" because one client hosts this room and allows other players to discover that room.

Despite MQTT being a publish/subscribe model, it's used for peer-to-peer communication. And a gatekeeper handling the game lobby.

Sure. But among the various possible ways for those peers to interact, one way is to appoint one of them the server for the purpose of this game.

This is added to the game.
I also increased the accuracy of the bot starts when you first start the project.

I fixed a bug where you could infinitely jump. I also added a thumbnail because the project was featured.

@bh I updated the thumbnail on the project but it is stuck on a black screen on the front page.
Also, my project notes wont save.

Alas, probably nobody will have time to look at this until after Snap!Con.