Game Description
This multiplayer game supports multiple game lobbies of 4 players each. Collect power-ups, take cover behind walls, and eliminate your opponents to win!
Power-ups include:
- Speed Boost
- Health Pack
- Triple Shot
- Shield
Sending Data
The pickle
module is used to convert objects into a byte stream that can be transmitted across the network, and then reconstruct the original objects from that byte stream at the receiving end.
pickle.dumps()
function serializes an object into a byte stream.
pickle.loads()
function deserializes the received byte stream back into the original Python object.
Custom Protocol
Client Side Format
n.send(msg_type, msg_data)
player_id, p = n.getp() # Get player ID and initial player object from the server
n.send('PLAYER_READY', None)
n.send('PLAYER_DATA', p)
n.send('GET_PLAYERS', None)
if power_up_type == 'speed_boost':
p.vel = 6 # Double the player's speed
n.send('PLAYER_COLLECTED_POWER_UP', power_ups.index(power_up))
pygame.time.set_timer(pygame.USEREVENT, 5000) # Set a timer for 5 seconds
elif power_up_type == 'med_pack':
p.health = 100
elif power_up_type == 'shield':
p.has_shield = True
Server Side
data = pickle.loads(conn.recv(2048))
msgtype, msg_data = data
if msgtype == 'PLAYER_DATA':
players[game_id][player_id] = msg_data
elif msgtype == 'PLAYER_READY':
reply = True
elif msgtype == 'GET_PLAYERS':
reply = players[game_id]
elif msgtype == 'PLAYER_COLLECTED_POWER_UP':
reply = check_win_condition(msg_data)
Multiple Game Lobbies
- Created a dictionary in the server containing multiple game lobby instances.
- When a client connects to the server, they are added to a lobby and assigned a player ID in that lobby.
What Did I Learn?
- Python libraries (Pickle and PyGame)
- Importance of TCP vs UDP in game development
- Addressing common networking issues in video games
- Player disconnect
- Packet loss
- Network congestion