A multiplayer online Battleship game implemented in Python using pygame for the client interface and socket programming for networking.
- Multiplayer Online Gameplay: Two players can connect and play against each other over a network
- Classic Battleship Rules: Standard 10x10 grid with traditional ship placement (Carrier, Battleship, Cruiser, Submarine, Destroyer)
- Interactive GUI: Pygame-based graphical interface with mouse controls
- Real-time Communication: Server-client architecture with JSON message protocol
- Ship Placement: Interactive ship placement with rotation support
- Attack System: Click-to-attack interface with visual feedback
- Game State Management: Turn-based gameplay with proper state synchronization
Each player places 5 ships on their 10x10 grid:
- Carrier: 5 cells
- Battleship: 4 cells
- Cruiser: 3 cells
- Submarine: 3 cells
- Destroyer: 2 cells
- Players take turns attacking each other's boards
- Click on the opponent's board to attack a cell
- Hits are marked with 'X', misses with 'O'
- When all cells of a ship are hit, the ship is sunk
- First player to sink all opponent ships wins
- Python 3.11 or higher
- pip package manager
- Clone or download the game files
- Navigate to the game directory
- Install dependencies:
pip install -r requirements.txt
- Open a terminal in the game directory
- Run the server:
python server.py
- The server will start on
0.0.0.0:8888and wait for connections
- Open another terminal (or run on a different machine)
- Run the client:
python main.py
- The client will automatically connect to
localhost:8888
To play over a network:
- Start the server on one machine
- Edit
main.pyand change the host in theBattleshipClient()initialization to the server's IP address - Run clients from other machines
- Mouse Click: Place ship at cursor position
- R Key: Rotate ship orientation (Horizontal/Vertical)
- Ships must be placed within the grid boundaries
- Ships cannot overlap
- Mouse Click: Attack opponent's board
- Click on any cell in the opponent's grid to attack
- You can only attack on your turn
- Already attacked cells cannot be attacked again
battleship_game/
├── main.py # Main client application with pygame GUI
├── server.py # Game server handling multiplayer connections
├── client_network.py # Network client class for server communication
├── game_logic.py # Core game logic and rules
├── requirements.txt # Python dependencies
├── README.md # This documentation
└── bin/unused/ # Unused files from original project
- Server: Multi-threaded TCP server handling multiple client connections
- Client: Pygame-based GUI with network communication thread
- Protocol: JSON messages over TCP sockets
- Game Logic: Separate module handling game rules and state
The game uses JSON messages for client-server communication:
join_game: Request to join a gameplace_ships: Send ship placement dataattack: Send attack coordinatesgame_state: Request current game state
- Default Host:
localhost(127.0.0.1) - Default Port: 8888
- Protocol: TCP
- Message Format: JSON over UTF-8 encoded strings
"Failed to connect to server"
- Ensure the server is running before starting clients
- Check that the host/port configuration matches
- Verify firewall settings allow connections on port 8888
"Module not found: pygame"
- Install pygame:
pip install pygame - Ensure you're using the correct Python version
Game freezes during ship placement
- Press 'R' to rotate ships if they don't fit
- Ensure ships don't overlap with existing ships
- Ships must be placed entirely within the 10x10 grid
Network play doesn't work
- Edit the host address in
main.pyto match the server's IP - Ensure both machines can reach each other over the network
- Check firewall settings on both client and server machines
The modular design makes it easy to extend:
- Game Logic: Modify
game_logic.pyfor rule changes - UI: Update
main.pyfor interface improvements - Networking: Extend
server.pyandclient_network.pyfor new features - Protocol: Add new message types in the JSON protocol
- Run
python game_logic.pyto test core game mechanics - Run
python client_network.pyto test network connectivity - Use multiple client instances to test multiplayer functionality
This project is provided as-is for educational and entertainment purposes.