How to make a 2 player game with AI
Split the keyboard down the middle and you have a two player game by tonight. Reach for the internet first and you will still be debugging on Sunday.
Jump to a section
Ask for one screen, one keyboard, and two sets of keys. Player one takes W, A, S and D. Player two takes the arrow keys. There is no server, no matchmaking and no lag, and the whole thing is an evening rather than a project.
Two people in the same room share a keyboard. Two people in different rooms need a network, a host, and a way to agree on what just happened. The first is a game. The second is a distributed system with a game attached.
Four steps to a game two people can play#
Claude, ChatGPT or Gemini all handle this. The prompt says one keyboard out loud, because a model asked for two players will otherwise reach for the network.
Hands on both halves of the keyboard. It feels silly and it finds the fault in step three in about ten seconds.
Press and hold W while pressing the left arrow. If one player stops moving, you have the held-key fault, and there is a section on it below.
Nothing you do alone tells you whether the game is fair. Five minutes with somebody else beats an hour of your own testing.
Build a two player game as ONE self-contained .html file. Both players
share one keyboard on one screen. NO network code, no server, no sockets.
GAME: two round creatures on a circular platform seen from above. Each
one pushes the other. Fall off the edge and you lose the round. First to
three rounds wins the match.
CONTROLS: player one uses W, A, S, D. Player two uses the arrow keys.
Both must be able to move at the same time.
INPUT, this is the important part: keep an object of currently held keys,
set true on keydown and false on keyup, and read that object once per
frame. Do NOT move anybody inside the keydown handler.
ROUNDS: a three second countdown, then the round starts. Between rounds
show the score. A key restarts the match without reloading the page.
FEEL: pushing is a shove, not a magnet. Both creatures keep sliding for a
moment after a hit, and the platform shrinks slightly each round.
ART: canvas shapes and flat colour, one colour per player. No images.
Base all movement on seconds elapsed between frames, capped at 0.05.
Put every tunable number in one CONFIG object at the top.
The input paragraph is the one to keep when you change everything else. It is the difference between a two player game and a game two people take turns watching.
Why one player freezes when the other holds a key#
This is the fault in almost every first build, and people usually blame their keyboard for it. The game is moving players inside the keydown event. That event fires once, pauses, then repeats on a clock the operating system owns, and it only repeats for the key pressed most recently.
If the game already has the fault, you do not need to describe any of that. Paste this instead.
Both players cannot move at the same time. Rewrite the input handling:
keep a held object, set held[e.key] = true on keydown and false on keyup,
and move both players from that object inside the animation frame. Nothing
should move inside an event handler. Keep everything else the same.
Cheap keyboards cannot report more than a few keys at once, and the keys that clash are usually close together. If one player stops only when the other is holding two keys, try moving player two to the arrow keys and the number pad before rewriting anything.
What makes a two player game fair#
Fairness is not a feature you add. It is a set of small decisions you either get right early or unpick later.
- Mirror the controls, not the keys. Both players need the same number of actions reachable without moving a wrist. Four directions and one button each is the ceiling for a shared keyboard.
- Give nobody the better seat. If one side of the screen sees more of the level, the game is decided before anybody presses a key. Symmetry beats balance work.
- Make rounds short. Thirty seconds, then a result. Losing a long game to somebody sitting next to you is a worse feeling than losing six short ones.
- Restart without the keyboard leaving the table. One key, straight into the next round. Reaching for a mouse ends a session more reliably than losing does.
- Say who won in large text. Two people watching one screen need the result from across the sofa, not in the corner in grey.
When is online play worth it?#
Once the game is good on one keyboard, and not before. Online turns one source of truth into several, and every disagreement between them becomes a bug you can only see with two machines and two people.
If you publish on Agent Games, the hard part is already hosted. Every game here can join a room of up to 32 players, send messages to everybody or to one player, and ask for a stable host, without running a server. The site carries the messages and never looks at them.
arcade.room.join(); // or join("CODE") to play with a friend
arcade.room.on("open", (me) => {});
arcade.room.on("join", (who, all) => {});
arcade.room.on("message", (msg, from) => {});
arcade.room.send({ x: 120, y: 48 }); // everybody, or send(data, playerId)
arcade.room.players; // players[0] is the same host for everyone
Authority still has to live somewhere, and a relay cannot referee. Pick the first player in the roster as the host, let that one machine decide what happened, and have everybody else draw what it says. The full reference is in the agent docs.
What to check before you call it finished#
Two player games fail in places a single player game never visits. Two people press things at the same time, and one of them is always doing something you did not plan for.
- Hold four keys down at once, two per player, and watch for either one stalling.
- Let one player win three times in a row and check the match actually ends and offers another.
- Press every key during the countdown. A player who can move before the round starts will.
- Leave the tab, come back, and see whether both players are still where you left them.
🤝2 player games on this site
Common questions#
Can AI make a 2 player game?
Yes, and a shared keyboard version is one of the easier things to ask for. One screen, one file, two sets of keys, no server. Online play with two separate computers is a much larger job and is worth leaving until the game is already fun.
Why can only one player move at a time?
The game is moving players inside the keydown event, which repeats only for the most recently pressed key. Record held keys on keydown and keyup instead, and read that record once per frame. There is a repair prompt for this above.
Which keys should each player use?
W, A, S and D for player one, the arrow keys for player two, and one action key each that is nowhere near the other player's hand. If keys stop registering under load, move player two to the number pad, because cheap keyboards drop simultaneous presses.
Can two people play on one phone?
On a tablet, yes, with a button cluster in each bottom corner. On a phone there is not enough room for four thumbs, so treat two player games as a keyboard or tablet thing and let the phone version be single player.
How do I make an online two player game?
You need something to carry messages between the two machines and a rule about who decides what happened. Games published here can use the rooms the site hosts. They take up to 32 players and give every client the same stable host, so the game needs no server of its own.
What is the easiest two player game to build first?
Anything decided in under a minute with one button each. Sumo on a shrinking platform, a race to a button, a tug of war. Avoid anything with an inventory, a map or a turn order on the first attempt.
🌐Multiplayer games written by AI
Where to go next#
When somebody has played it twice in a row without being asked, publish it. It gets a page recording the model, the prompt and the tokens behind it, and a friend can play it from a link without installing anything.
For the full method behind all of this, read how to make a game with AI. For prompts you can send unchanged, including repairs for the faults in the checklist above, use the prompt guide. More games to borrow ideas from are in 2 player and multiplayer.
Built something? Put it on Agent Games.
Publishing takes a file and a minute. Your game gets its own page listing the model, the prompt and the token count, and anyone can play it without signing up.
Read next