Set color for transparent tile (matching the screen background)
tile_images[0].fill(BLUE) # Light blue to blend with background
Function to check if the character is on the ground (on a grass tile)
def is_on_ground(rect):
rect.bottom += 1 # Temporarily move the rect down by 1 pixel
on_ground = False
for y in range(max(0, rect.bottom // TILE_SIZE), min(len(map_layout), (rect.bottom // TILE_SIZE) + 1)):
for x in range(max(0, rect.left // TILE_SIZE), min(len(map_layout[y]), (rect.right // TILE_SIZE) + 1)):
if map_layout[y][x] == 1 or map_layout[y][x] == 2:
tile_rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
if rect.colliderect(tile_rect):
rect.bottom -= 1 # Move the rect back up
on_ground = True
rect.bottom -= 1 # Move the rect back up
return on_ground
Function to check and handle general collision with tiles
def handle_tile_collisions(rect):
global character_velocity_y, character_velocity_x, is_jumping
# Check vertical collisions first (falling or jumping)
for y in range(max(0, rect.top // TILE_SIZE), min(len(map_layout), (rect.bottom // TILE_SIZE) + 1)):
for x in range(max(0, rect.left // TILE_SIZE), min(len(map_layout[y]), (rect.right // TILE_SIZE) + 1)):
tile_id = map_layout[y][x]
tile_rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
if rect.colliderect(tile_rect):
if tile_id == 1: # Solid tile
# Handle vertical collisions
if character_velocity_y > 0: # Falling down
if rect.bottom > tile_rect.top and rect.bottom <= tile_rect.bottom:
rect.bottom = tile_rect.top
character_velocity_y = 0
is_jumping = False
elif character_velocity_y < 0: # Jumping up
if rect.top < tile_rect.bottom and rect.top >= tile_rect.top:
rect.top = tile_rect.bottom
character_velocity_y = 0
elif tile_id == 2: # Jump-through tile
# Handle collision if falling onto the tile
if character_velocity_y > 0 and rect.bottom > tile_rect.top and rect.bottom <= tile_rect.bottom:
rect.bottom = tile_rect.top
character_velocity_y = 0
is_jumping = False
# Check horizontal collisions after vertical collisions are resolved
for y in range(max(0, rect.top // TILE_SIZE), min(len(map_layout), (rect.bottom // TILE_SIZE) + 1)):
for x in range(max(0, rect.left // TILE_SIZE), min(len(map_layout[y]), (rect.right // TILE_SIZE) + 1)):
tile_id = map_layout[y][x]
tile_rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
if rect.colliderect(tile_rect):
if tile_id == 1: # Solid tile
# Handle horizontal collisions
if character_velocity_x > 0: # Moving right
if rect.right > tile_rect.left and rect.right <= tile_rect.right:
rect.right = tile_rect.left
character_velocity_x = 0
elif character_velocity_x < 0: # Moving left
if rect.left < tile_rect.right and rect.left >= tile_rect.left:
rect.left = tile_rect.right
character_velocity_x = 0
Networking setup
player_name, server_ip = get_user_input_pygame()
try:
if ':' in server_ip:
HOST, PORT = server_ip.split(':')
PORT = int(PORT)
else:
HOST = server_ip
PORT = 25566
except ValueError:
HOST = 'localhost'
PORT = 25566
except TypeError:
HOST = 'localhost'
PORT = 25566
except Exception as e:
print(f"An error occurred: {e}")
HOST = 'localhost'
PORT = 25566
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client.connect((HOST, PORT))
# Send initial player data to the server
send_data({'name': player_name, 'position': (character.x, character.y)})
except socket.error as e:
print(f"Unable to connect to the server: {e}")
sys.exit()
def receive_data():
global players, map_layout, running, own_player_id
while running:
try:
data = receive_message(client)
if 'player_id' in data:
own_player_id = data['player_id']
players = data.get('players', {})
map_layout = data.get('map_layout', [])
except Exception as e:
print(f"Disconnected from server: {e}")
running = False
break
class Bullet:
def init(self, owner_id, position, direction):
self.owner_id = owner_id # Unique Player ID
self.x, self.y = position
self.direction = direction # 1 for right, -1 for left
self.speed = 15
self.rect = pygame.Rect(self.x, self.y, 10, 5)
last_shot_time = 0 # Time when the last shot was fired
is_firing = False # Track if the fire key is being held down
Respawn variables
is_dead = False
respawn_time = 0 # Timestamp when respawn should occur
Main game loop
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Track firing state
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_f and not is_firing:
is_firing = True
current_time = pygame.time.get_ticks()
if (current_time - last_shot_time >= BULLET_COOLDOWN) and (len(bullets) < MAX_BULLETS):
# Determine shooting direction based on facing direction
# Assuming facing right by default
direction = 1
if character_velocity_x < 0:
direction = -1 # Left
# Create a bullet at the front of the character
bullet_x = character.x + character.width if direction == 1 else character.x - 10
bullet_y = character.y + character.height // 2
new_bullet = Bullet(own_player_id, (bullet_x, bullet_y), direction)
bullets.append(new_bullet)
# Update the last shot time
last_shot_time = current_time
if event.type == pygame.KEYUP:
if event.key == pygame.K_f:
is_firing = False
# Get keys pressed
keys = pygame.key.get_pressed()
# Handle horizontal movement
if not is_dead and own_player_id in players and not players[own_player_id]['is_dead']:
if keys[pygame.K_LEFT]:
character_velocity_x = -SPEED
elif keys[pygame.K_RIGHT]:
character_velocity_x = SPEED
else:
character_velocity_x *= FRICTION # Apply friction when no key is pressed
# To prevent drifting due to very low velocity
if abs(character_velocity_x) < 0.1:
character_velocity_x = 0
# Handle jumping
if (keys[pygame.K_SPACE] or keys[pygame.K_UP] or keys[pygame.K_w]) and not is_jumping and is_on_ground(character):
character_velocity_y = -JUMP_STRENGTH
is_jumping = True
# Apply gravity
character_velocity_y += GRAVITY
character.x += character_velocity_x
character.y += character_velocity_y
# Check and handle collisions with tiles
handle_tile_collisions(character)
# Ensure character stays within screen bounds
if character.left < 0:
character.left = 0
if character.right > SCREEN_WIDTH:
character.right = SCREEN_WIDTH
if character.top < 0:
character.top = 0
if character.bottom > SCREEN_HEIGHT:
character.bottom = SCREEN_HEIGHT
character_velocity_y = 0
is_jumping = False
# Update bullets
for bullet in bullets[:]:
bullet.move()
# Remove bullets that are off-screen
if bullet.x < 0 or bullet.x > SCREEN_WIDTH:
bullets.remove(bullet)
# Send character position and bullets to server
try:
# Serialize bullet data
bullet_data = [{'x': b.x, 'y': b.y, 'direction': b.direction} for b in bullets]
send_data({'name': player_name, 'position': (character.x, character.y), 'bullets': bullet_data})
except socket.error as e:
print(f"Failed to send data to server: {e}")
running = False
# Handle respawn
if is_dead and pygame.time.get_ticks() >= respawn_time:
is_dead = False
if own_player_id in players:
spawn_pos = players[own_player_id]['position']
character.x, character.y = spawn_pos
character_velocity_x, character_velocity_y = 0, 0
is_jumping = False
bullets.clear()
# Inform server about respawn
send_data({'name': player_name, 'position': (character.x, character.y), 'respawn': True})
# Draw everything
screen.fill(BLUE)
# Draw the map layout
for y, row in enumerate(map_layout):
for x, tile_id in enumerate(row):
tile_image = tile_images.get(tile_id)
if tile_image:
screen.blit(tile_image, (x * TILE_SIZE, y * TILE_SIZE))
# Draw all players
for pid, info in players.items():
pos = info['position']
color = info['color']
health = info.get('health', 100)
player_rect = pygame.Rect(pos[0], pos[1], 50, 50)
pygame.draw.rect(screen, color, player_rect)
name = info.get('name', 'Unknown')
text_surface = SMALL_FONT.render(name, True, BLACK)
screen.blit(text_surface, (pos[0], pos[1] - 20)) # Display name above player
# Draw health bar
health_bar_width = 50
health_bar_height = 5
health_ratio = max(health / 100, 0)
pygame.draw.rect(screen, RED, (pos[0], pos[1] - 10, health_bar_width, health_bar_height))
pygame.draw.rect(screen, GREEN, (pos[0], pos[1] - 10, health_bar_width * health_ratio, health_bar_height))
# Draw all bullets from other players
for pid, info in players.items():
for b in info.get('bullets', []):
bullet_rect = pygame.Rect(b['x'], b['y'], 10, 5)
pygame.draw.rect(screen, RED, bullet_rect)
# Draw local bullets
for bullet in bullets:
bullet.draw(screen)
# Update the display
pygame.display.flip()
# Cap the frame rate
clock.tick(60)
# Handle death and respawn if necessary
if own_player_id in players:
if players[own_player_id].get('health', 100) <= 0 and not is_dead:
is_dead = True
respawn_time = pygame.time.get_ticks() + 3000 # 3 seconds cooldown
print("You have been eliminated! Respawning in 3 seconds...")
# Reset local character position to server's value for consistency
spawn_pos = players[own_player_id].get('position', (375, 375))
character.x, character.y = spawn_pos
character_velocity_x, character_velocity_y = 0, 0
is_jumping = False
Close the client socket and quit Pygame
client.close()
pygame.quit()
sys.exit()
server.py
server.py
import random
import socket
import threading
import pickle
import struct
import time
import pygame
Define SCREEN_WIDTH and SCREEN_HEIGHT to match client
SCREEN_WIDTH, SCREEN_HEIGHT = 1280, 720
Server settings
HOST = '0.0.0.0' # Listen on all interfaces
PORT = 25566
Store player positions, colors, health, bullets, and respawn info
players = {}
List to keep track of connections
connections = []
Tile size
TILE_SIZE = 72 # Updated to match client
Initialize Pygame for collision rects (optional, can be removed if not needed)
def send_data(conn, data):
"""Serialize and send data to a client."""
try:
serialized_data = pickle.dumps(data)
length = len(serialized_data)
conn.sendall(struct.pack('>I', length) + serialized_data)
except Exception as e:
print(f"Error sending data: {e}")
close_connection(conn)
def receive_message(conn):
"""Receive and deserialize a message from a client."""
try:
length_data = conn.recv(4)
if len(length_data) < 4:
raise Exception("Received incomplete length data")
data_length = struct.unpack('>I', length_data)[0]
data = b''
while len(data) < data_length:
packet = conn.recv(data_length - len(data))
if not packet:
raise Exception("Received incomplete data")
data += packet
return pickle.loads(data)
except Exception as e:
print(f"Error receiving message: {e}")
close_connection(conn)
raise
connections.append(conn)
# Send initial data to the new client, including their player_id
send_data(conn, {
'player_id': player_id,
'players': players,
'map_layout': current_map_layout
})
# Broadcast to all clients about the new player
broadcast({'players': players, 'map_layout': current_map_layout}, exclude=None)
while True:
data = receive_message(conn)
if not data:
break
player = players.get(player_id)
if not player:
continue # Player might have been disconnected
# Handle respawn request
if data.get('respawn'):
player['is_dead'] = False
player['health'] = 100
player['position'] = get_random_spawn_position()
player['bullets'] = []
send_data(conn, {'respawned': True, 'position': player['position']})
broadcast({'players': players, 'map_layout': current_map_layout}, exclude=None)
continue
# Update player position if not dead
if not player['is_dead']:
player['position'] = data.get('position', player['position'])
# Handle bullets from this player
incoming_bullets = data.get('bullets', [])
for b in incoming_bullets:
bullet = {
'owner_id': player_id,
'x': b['x'],
'y': b['y'],
'direction': b['direction']
}
player['bullets'].append(bullet)
# Update bullets positions and handle collisions
update_bullets()
# Update all clients
broadcast({'players': players, 'map_layout': current_map_layout}, exclude=None)
except Exception as e:
print(f"Exception in handle_client: {e}")
finally:
disconnect_client(conn, player_id)
def broadcast(data, exclude=None):
"""Send data to all clients except the specified one."""
for player_conn in connections[:]:
if player_conn != exclude:
try:
send_data(player_conn, data)
except Exception as e:
print(f"Error broadcasting data: {e}")
close_connection(player_conn)
def get_random_spawn_position():
x = random.randint(0, len(current_map_layout[0]) - 2)
y = len(current_map_layout) - 2 # Just above the ground
return x * TILE_SIZE, y * TILE_SIZE
def change_map_layout_periodically():
"""Change the map layout every 60 seconds."""
global current_map_layout
map_index = 0
while True:
time.sleep(60)
current_map_layout = map_layouts[map_index]
map_index = (map_index + 1) % len(map_layouts)
print("Map layout changed.")
broadcast({'players': players, 'map_layout': current_map_layout})
Connection handling
def close_connection(conn):
"""Close the connection and remove it from the active list."""
if conn in connections:
connections.remove(conn)
conn.close()
def disconnect_client(conn, player_id):
"""Remove a client from the player list and notify others."""
print(f"Disconnected Player ID: {player_id}")
if player_id in players:
del players[player_id]
close_connection(conn)
broadcast({'players': players, 'map_layout': current_map_layout})
Core game functions
def update_bullets():
"""Update bullet positions and handle collisions."""
global players
for player_id, info in players.items():
if info['is_dead']:
continue # Dead players can't shoot
bullets_to_remove = []
for bullet in info['bullets']:
# Move bullet
bullet['x'] += bullet['direction'] * 15
# Check if bullet is out of bounds
if bullet['x'] < 0 or bullet['x'] > SCREEN_WIDTH:
bullets_to_remove.append(bullet)
continue
# Check collision with tiles
bullet_rect = pygame.Rect(bullet['x'], bullet['y'], 10, 5)
tile_x = bullet_rect.x // TILE_SIZE
tile_y = bullet_rect.y // TILE_SIZE
if 0 <= tile_y < len(current_map_layout) and 0 <= tile_x < len(current_map_layout[0]):
tile_id = current_map_layout[tile_y][tile_x]
if tile_id != 0: # Non-empty tile
bullets_to_remove.append(bullet)
continue
# Check collision with other players
for other_id, other_info in players.items():
if other_id == player_id or other_info['is_dead']:
continue
other_rect = pygame.Rect(other_info['position'][0], other_info['position'][1], 50, 50)
if bullet_rect.colliderect(other_rect):
# Decrease health
other_info['health'] -= 10
print(f"{other_info['name']} was hit by {info['name']}! Health: {other_info['health']}")
# Remove the bullet
bullets_to_remove.append(bullet)
# Check if player is dead
if other_info['health'] <= 0:
print(f"{other_info['name']} has been eliminated!")
# Set player as dead and set respawn time
other_info['is_dead'] = True
other_info['respawn_time'] = time.time() + 3 # 3 seconds cooldown
# Reset player bullets
other_info['bullets'] = []
# Inform the player to respawn
for conn in connections:
try:
send_data(conn, {'players': players, 'map_layout': current_map_layout})
except:
pass
break # Bullet can hit only one player
# Remove bullets that have hit, collided, or are out of bounds
for bullet in bullets_to_remove:
if bullet in info['bullets']:
info['bullets'].remove(bullet)
# Handle respawning
current_time = time.time()
for player_id, info in players.items():
if info['is_dead'] and current_time >= info['respawn_time']:
info['is_dead'] = False
info['health'] = 100
info['position'] = get_random_spawn_position()
print(f"{info['name']} has respawned.")
# Notify all clients
broadcast({'players': players, 'map_layout': current_map_layout}, exclude=None)
Server setup
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen()
print("Server started, waiting for connections...")
while True:
conn, addr = server.accept()
with player_id_lock:
player_id = player_id_counter
player_id_counter += 1
threading.Thread(target=handle_client, args=(conn, addr, player_id), daemon=True).start()
To optimize your multiplayer game for high ping scenarios by reducing the frequency of player movement updates, you can implement the following strategies:
Send Player Movement Every 24 Frames: Instead of sending movement data every frame (60 times per second), send it every 24 frames (~2.5 times per second). This reduces the amount of data transmitted, helping mitigate potential lag from high ping.
Simulate Other Players' Movements Between Updates: Use interpolation to smoothly update the positions of other players based on the last received data. This ensures that movements appear fluid even when updates are less frequent.
Below are the detailed steps and code modifications to implement these strategies in your game.py and server.py files.
1. Client-Side Modifications (game.py)
A. Implement Frame Counting and Controlled Data Sending
Add a frame counter to track the number of frames rendered. Use this counter to send player movement data every 24 frames.
# Add at the beginning of your main game loop
frame_count = 0 # Initialize frame counter
SEND_EVERY_FRAMES = 24 # Send data every 24 frames
Within the main loop, increment the counter and send data conditionally:
# Inside the main game loop
while running:
frame_count += 1
# ... existing event handling and game logic ...
# Send data every SEND_EVERY_FRAMES
if frame_count % SEND_EVERY_FRAMES == 0 and not is_dead and own_player_id in players and not players[own_player_id]['is_dead']:
try:
# Serialize bullet data
bullet_data = [{'x': b.x, 'y': b.y, 'direction': b.direction} for b in bullets]
send_data({'name': player_name, 'position': (character.x, character.y), 'bullets': bullet_data})
except socket.error as e:
print(f"Failed to send data to server: {e}")
running = False
B. Add Interpolation for Other Players' Movements
To ensure smooth movement of other players between updates, implement interpolation. This involves storing the last known position and the new position when an update arrives and smoothly transitioning between them.
Create a Data Structure to Store Interpolation Data
Add a dictionary to keep track of each player's interpolation state.
# Initialize interpolation data
interpolation_data = {}
INTERPOLATION_SPEED = 0.1 # Adjust for smoother or faster interpolation
Update Interpolation Data When Receiving New Positions
Modify the receive_data function to update interpolation targets for other players.
def receive_data():
global players, map_layout, running, own_player_id, interpolation_data
while running:
try:
data = receive_message(client)
if 'player_id' in data:
own_player_id = data['player_id']
if 'map_layout' in data:
map_layout = data['map_layout']
if 'players' in data:
for pid, info in data['players'].items():
if pid != own_player_id:
# Initialize interpolation data if not present
if pid not in interpolation_data:
interpolation_data[pid] = {
'current_pos': info['position'],
'target_pos': info['position']
}
else:
interpolation_data[pid]['target_pos'] = info['position']
players = data.get('players', {})
except Exception as e:
print(f"Disconnected from server: {e}")
running = False
break
Interpolate Positions Each Frame
Within the main loop, smoothly interpolate other players' positions towards their target positions.
# Inside the main game loop, after handling local player movement and before drawing
for pid, info in players.items():
if pid == own_player_id:
continue # Skip own player
if pid in interpolation_data:
current_x, current_y = interpolation_data[pid]['current_pos']
target_x, target_y = interpolation_data[pid]['target_pos']
# Calculate interpolated position
new_x = current_x + (target_x - current_x) * INTERPOLATION_SPEED
new_y = current_y + (target_y - current_y) * INTERPOLATION_SPEED
# Update current position
interpolation_data[pid]['current_pos'] = (new_x, new_y)
# Update player's position for rendering
players[pid]['position'] = (new_x, new_y)
C. Modify Drawing Logic to Use Interpolated Positions
Ensure that the drawing of other players uses the interpolated positions.
# Drawing all players
for pid, info in players.items():
pos = info['position']
if pid != own_player_id and pid in interpolation_data:
pos = interpolation_data[pid]['current_pos']
color = info['color']
health = info.get('health', 100)
player_rect = pygame.Rect(int(pos[0]), int(pos[1]), 50, 50)
pygame.draw.rect(screen, color, player_rect)
name = info.get('name', 'Unknown')
text_surface = SMALL_FONT.render(name, True, BLACK)
screen.blit(text_surface, (player_rect.x, player_rect.y - 20)) # Display name above player
# Draw health bar
health_bar_width = 50
health_bar_height = 5
health_ratio = max(health / 100, 0)
pygame.draw.rect(screen, RED, (player_rect.x, player_rect.y - 10, health_bar_width, health_bar_height))
pygame.draw.rect(screen, GREEN, (player_rect.x, player_rect.y - 10, health_bar_width * health_ratio, health_bar_height))
D. Optional: Adjust Bullet Handling for Smoothness
If you experience bullet synchronization issues, you might want to apply similar interpolation or prediction techniques to bullets. However, bullets typically require precise synchronization due to their fast movement, so ensure that their handling remains accurate.
2. Server-Side Modifications (server.py)
The server primarily handles receiving and broadcasting player data. Since the client now sends fewer updates, the server doesn't require significant changes. However, ensure that the server broadcasts the current state consistently to all clients.
A. Optimize Data Broadcasting
Ensure that the server efficiently broadcasts updated player states. You can maintain a consistent broadcast rate (e.g., 20 times per second) regardless of individual client update rates.
Implement a Dedicated Broadcast Loop
Add a separate thread to handle broadcasting at fixed intervals.
def broadcast_loop():
while True:
broadcast({'players': players, 'map_layout': current_map_layout}, exclude=None)
time.sleep(1 / 20) # 20 times per second
Since broadcasting is now handled separately, remove the broadcast call from the handle_client function to prevent redundant broadcasts.
# Remove or comment out this line in handle_client
# broadcast({'players': players, 'map_layout': current_map_layout}, exclude=None)
Ensure Bullet Updates Are Handled Properly
Ensure that bullet updates are processed and included in the broadcasted players data.
# In the handle_client function, after updating bullets
update_bullets()
# No need to broadcast here as the broadcast_loop handles it
B. Final Server Code Snippet
Here's the modified section of your server.py to include the broadcast loop:
# Add after existing function definitions
def broadcast_loop():
while True:
broadcast({'players': players, 'map_layout': current_map_layout}, exclude=None)
time.sleep(1 / 20) # 20 times per second
# Start the broadcast loop
threading.Thread(target=broadcast_loop, daemon=True).start()
# Remove broadcasting from handle_client
def handle_client(conn, addr, player_id):
# ... existing code ...
# Remove or comment out the broadcast call here
# broadcast({'players': players, 'map_layout': current_map_layout}, exclude=None)
while True:
# ... existing code ...
data = receive_message(conn)
# ... existing code ...
update_bullets()
# No need to broadcast here
3. Additional Considerations
A. Handling High Ping and Packet Loss
Even with reduced update frequency, high ping and packet loss can still affect gameplay. Consider implementing the following to further enhance smoothness:
Client-Side Prediction: Predict other players' movements based on their last known direction and speed.
Extrapolation: Estimate future positions if no new data has been received recently.
Lag Compensation: Adjust game mechanics to account for network delays.
B. Choose Appropriate Send Interval
Sending data every 24 frames (~2.5 times per second) is a trade-off between latency and bandwidth usage. You may need to experiment with different intervals to find the optimal balance for your specific game and target network conditions.
C. Optimize Data Payload
Ensure that only essential data is sent to reduce the payload size:
Simplify Data Structures: Only include necessary information (e.g., position, velocity).
Use Efficient Serialization: If pickle is too heavy, consider using more efficient methods like json or binary protocols.
Complete Modified game.py
Below is the complete modified game.py incorporating the above changes. Comments are added to indicate new or changed sections.
# game.py
import time
import pygame
import socket
import pickle
import threading
import struct
import sys
# Initialize Pygame
pygame.init()
# Set up the screen
SCREEN_WIDTH, SCREEN_HEIGHT = 1280, 720
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('2D Fight Game')
# Constants
TILE_SIZE = 72 # Tile size after scaling
GRAVITY = 1
JUMP_STRENGTH = 15
SPEED = 11
FRICTION = 0.05 # Adjusted friction for better movement
FONT = pygame.font.Font(None, 48)
SMALL_FONT = pygame.font.Font(None, 36)
WHITE = (255, 255, 255)
BLACK = (46, 46, 46)
BLUE = (135, 206, 235)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Bullet settings
MAX_BULLETS = 1 # Maximum number of bullets allowed on screen
BULLET_COOLDOWN = 300 # Cooldown time in milliseconds between shots
# Input boxes
player_name_input_box = pygame.Rect(400, 200, 400, 50)
server_ip_input_box = pygame.Rect(400, 300, 400, 50)
# Text variables
player_name = 'Player'
server_ip = 'localhost:25566'
input_active = None
# Connecting flag
connecting = False
# Function to display text
def draw_text(text, font, color, surface, x, y):
textobj = font.render(text, True, color)
textrect = textobj.get_rect(center=(x, y))
surface.blit(textobj, textrect)
# Connecting screen
def show_connecting_screen():
screen.fill(BLUE)
draw_text("Connecting...", FONT, BLACK, screen, SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
pygame.display.flip()
# Function to handle user input for name and server IP in Pygame
def get_user_input_pygame():
global player_name, server_ip, input_active, connecting
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if player_name_input_box.collidepoint(event.pos):
input_active = 'name'
elif server_ip_input_box.collidepoint(event.pos):
input_active = 'server_ip'
else:
input_active = None
if event.type == pygame.KEYDOWN:
if input_active == 'name':
if event.key == pygame.K_RETURN:
input_active = 'server_ip'
elif event.key == pygame.K_BACKSPACE:
player_name = player_name[:-1]
else:
player_name += event.unicode
elif input_active == 'server_ip':
if event.key == pygame.K_RETURN:
connecting = True
show_connecting_screen()
return player_name, server_ip
elif event.key == pygame.K_BACKSPACE:
server_ip = server_ip[:-1]
else:
server_ip += event.unicode
elif event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
screen.fill(BLUE)
draw_text("Enter Player Name:", FONT, BLACK, screen, SCREEN_WIDTH // 2, 160)
draw_text("Enter Server IP (e.g., localhost:25566):", FONT, BLACK, screen, SCREEN_WIDTH // 2, 260)
pygame.draw.rect(screen, WHITE, player_name_input_box, 2)
pygame.draw.rect(screen, WHITE, server_ip_input_box, 2)
draw_text(player_name, SMALL_FONT, BLACK, screen, player_name_input_box.centerx, player_name_input_box.centery)
draw_text(server_ip, SMALL_FONT, BLACK, screen, server_ip_input_box.centerx, server_ip_input_box.centery)
pygame.display.flip()
pygame.time.Clock().tick(60) # Control input loop frame rate
# Function to send data
def send_data(data):
serialized_data = pickle.dumps(data)
length = len(serialized_data)
try:
client.sendall(struct.pack('>I', length) + serialized_data)
except socket.error as e:
print(f"Failed to send data to server: {e}")
# Function to receive messages
def receive_message(conn):
length_data = conn.recv(4)
if len(length_data) < 4:
raise Exception("Received incomplete length data")
data_length = struct.unpack('>I', length_data)[0]
data = b''
while len(data) < data_length:
packet = conn.recv(data_length - len(data))
if not packet:
raise Exception("Received incomplete data")
data += packet
return pickle.loads(data)
# Set up the character
character = pygame.Rect(375, 375, 50, 50) # Initial position
character_velocity_x = 0
character_velocity_y = 0
is_jumping = False
# Initialize player data and map layout
players = {}
map_layout = []
own_player_id = None # To store the player's unique ID assigned by the server
# Load tile images
tile_images = {
0: pygame.Surface((TILE_SIZE, TILE_SIZE)), # Empty space
1: pygame.image.load('./assets/grass.png').convert_alpha(), # Grass tile (solid)
2: pygame.image.load('./assets/grass.png').convert_alpha(), # Grass tile (jump-through)
}
# Scale the grass tile image
tile_images[1] = pygame.transform.scale(tile_images[1], (TILE_SIZE, TILE_SIZE))
tile_images[2] = pygame.transform.scale(tile_images[2], (TILE_SIZE, TILE_SIZE))
# Set color for transparent tile (matching the screen background)
tile_images[0].fill(BLUE) # Light blue to blend with background
# Function to check if the character is on the ground (on a grass tile)
def is_on_ground(rect):
rect.bottom += 1 # Temporarily move the rect down by 1 pixel
on_ground = False
for y in range(max(0, rect.bottom // TILE_SIZE), min(len(map_layout), (rect.bottom // TILE_SIZE) + 1)):
for x in range(max(0, rect.left // TILE_SIZE), min(len(map_layout[y]), (rect.right // TILE_SIZE) + 1)):
if map_layout[y][x] == 1 or map_layout[y][x] == 2:
tile_rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
if rect.colliderect(tile_rect):
rect.bottom -= 1 # Move the rect back up
on_ground = True
rect.bottom -= 1 # Move the rect back up
return on_ground
# Function to check and handle general collision with tiles
def handle_tile_collisions(rect):
global character_velocity_y, character_velocity_x, is_jumping
# Check vertical collisions first (falling or jumping)
for y in range(max(0, rect.top // TILE_SIZE), min(len(map_layout), (rect.bottom // TILE_SIZE) + 1)):
for x in range(max(0, rect.left // TILE_SIZE), min(len(map_layout[y]), (rect.right // TILE_SIZE) + 1)):
tile_id = map_layout[y][x]
tile_rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
if rect.colliderect(tile_rect):
if tile_id == 1: # Solid tile
# Handle vertical collisions
if character_velocity_y > 0: # Falling down
if rect.bottom > tile_rect.top and rect.bottom <= tile_rect.bottom:
rect.bottom = tile_rect.top
character_velocity_y = 0
is_jumping = False
elif character_velocity_y < 0: # Jumping up
if rect.top < tile_rect.bottom and rect.top >= tile_rect.top:
rect.top = tile_rect.bottom
character_velocity_y = 0
elif tile_id == 2: # Jump-through tile
# Handle collision if falling onto the tile
if character_velocity_y > 0 and rect.bottom > tile_rect.top and rect.bottom <= tile_rect.bottom:
rect.bottom = tile_rect.top
character_velocity_y = 0
is_jumping = False
# Check horizontal collisions after vertical collisions are resolved
for y in range(max(0, rect.top // TILE_SIZE), min(len(map_layout), (rect.bottom // TILE_SIZE) + 1)):
for x in range(max(0, rect.left // TILE_SIZE), min(len(map_layout[y]), (rect.right // TILE_SIZE) + 1)):
tile_id = map_layout[y][x]
tile_rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
if rect.colliderect(tile_rect):
if tile_id == 1: # Solid tile
# Handle horizontal collisions
if character_velocity_x > 0: # Moving right
if rect.right > tile_rect.left and rect.right <= tile_rect.right:
rect.right = tile_rect.left
character_velocity_x = 0
elif character_velocity_x < 0: # Moving left
if rect.left < tile_rect.right and rect.left >= tile_rect.left:
rect.left = tile_rect.right
character_velocity_x = 0
# Networking setup
player_name, server_ip = get_user_input_pygame()
try:
if ':' in server_ip:
HOST, PORT = server_ip.split(':')
PORT = int(PORT)
else:
HOST = server_ip
PORT = 25566
except ValueError:
HOST = 'localhost'
PORT = 25566
except TypeError:
HOST = 'localhost'
PORT = 25566
except Exception as e:
print(f"An error occurred: {e}")
HOST = 'localhost'
PORT = 25566
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client.connect((HOST, PORT))
# Send initial player data to the server
send_data({'name': player_name, 'position': (character.x, character.y)})
except socket.error as e:
print(f"Unable to connect to the server: {e}")
sys.exit()
def receive_data():
global players, map_layout, running, own_player_id, interpolation_data
while running:
try:
data = receive_message(client)
if 'player_id' in data:
own_player_id = data['player_id']
if 'map_layout' in data:
map_layout = data['map_layout']
if 'players' in data:
for pid, info in data['players'].items():
if pid != own_player_id:
# Initialize interpolation data if not present
if pid not in interpolation_data:
interpolation_data[pid] = {
'current_pos': info['position'],
'target_pos': info['position']
}
else:
interpolation_data[pid]['target_pos'] = info['position']
players = data.get('players', {})
except Exception as e:
print(f"Disconnected from server: {e}")
running = False
break
# Start a thread to receive data from the server
running = True
interpolation_data = {} # Initialize interpolation data
INTERPOLATION_SPEED = 0.1 # Adjust for smoother or faster interpolation
threading.Thread(target=receive_data, daemon=True).start()
# Bullet class to manage bullets
class Bullet:
def __init__(self, owner_id, position, direction):
self.owner_id = owner_id # Unique Player ID
self.x, self.y = position
self.direction = direction # 1 for right, -1 for left
self.speed = 15
self.rect = pygame.Rect(self.x, self.y, 10, 5)
def move(self):
self.x += self.speed * self.direction
self.rect.x = self.x
def draw(self, surface):
pygame.draw.rect(surface, RED, self.rect)
# List to store local bullets
bullets = []
# Variables for bullet cooldown and firing
last_shot_time = 0 # Time when the last shot was fired
is_firing = False # Track if the fire key is being held down
# Respawn variables
is_dead = False
respawn_time = 0 # Timestamp when respawn should occur
# Main game loop
clock = pygame.time.Clock()
frame_count = 0 # Initialize frame counter
SEND_EVERY_FRAMES = 24 # Send data every 24 frames
while running:
frame_count += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Track firing state
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_f and not is_firing:
is_firing = True
current_time = pygame.time.get_ticks()
if (current_time - last_shot_time >= BULLET_COOLDOWN) and (len(bullets) < MAX_BULLETS):
# Determine shooting direction based on facing direction
# Assuming facing right by default
direction = 1
if character_velocity_x < 0:
direction = -1 # Left
# Create a bullet at the front of the character
bullet_x = character.x + character.width if direction == 1 else character.x - 10
bullet_y = character.y + character.height // 2
new_bullet = Bullet(own_player_id, (bullet_x, bullet_y), direction)
bullets.append(new_bullet)
# Update the last shot time
last_shot_time = current_time
if event.type == pygame.KEYUP:
if event.key == pygame.K_f:
is_firing = False
# Get keys pressed
keys = pygame.key.get_pressed()
# Handle horizontal movement
if not is_dead and own_player_id in players and not players[own_player_id]['is_dead']:
if keys[pygame.K_LEFT]:
character_velocity_x = -SPEED
elif keys[pygame.K_RIGHT]:
character_velocity_x = SPEED
else:
character_velocity_x *= FRICTION # Apply friction when no key is pressed
# To prevent drifting due to very low velocity
if abs(character_velocity_x) < 0.1:
character_velocity_x = 0
# Handle jumping
if (keys[pygame.K_SPACE] or keys[pygame.K_UP] or keys[pygame.K_w]) and not is_jumping and is_on_ground(character):
character_velocity_y = -JUMP_STRENGTH
is_jumping = True
# Apply gravity
character_velocity_y += GRAVITY
character.x += character_velocity_x
character.y += character_velocity_y
# Check and handle collisions with tiles
handle_tile_collisions(character)
# Ensure character stays within screen bounds
if character.left < 0:
character.left = 0
if character.right > SCREEN_WIDTH:
character.right = SCREEN_WIDTH
if character.top < 0:
character.top = 0
if character.bottom > SCREEN_HEIGHT:
character.bottom = SCREEN_HEIGHT
character_velocity_y = 0
is_jumping = False
# Update bullets
for bullet in bullets[:]:
bullet.move()
# Remove bullets that are off-screen
if bullet.x < 0 or bullet.x > SCREEN_WIDTH:
bullets.remove(bullet)
# Send character position and bullets to server every SEND_EVERY_FRAMES
if frame_count % SEND_EVERY_FRAMES == 0:
try:
# Serialize bullet data
bullet_data = [{'x': b.x, 'y': b.y, 'direction': b.direction} for b in bullets]
send_data({'name': player_name, 'position': (character.x, character.y), 'bullets': bullet_data})
except socket.error as e:
print(f"Failed to send data to server: {e}")
running = False
# Interpolate other players' positions
for pid, info in players.items():
if pid == own_player_id:
continue # Skip own player
if pid in interpolation_data:
current_x, current_y = interpolation_data[pid]['current_pos']
target_x, target_y = interpolation_data[pid]['target_pos']
# Calculate interpolated position
new_x = current_x + (target_x - current_x) * INTERPOLATION_SPEED
new_y = current_y + (target_y - current_y) * INTERPOLATION_SPEED
# Update current position
interpolation_data[pid]['current_pos'] = (new_x, new_y)
# Update player's position for rendering
players[pid]['position'] = (new_x, new_y)
# Handle respawn
if is_dead and pygame.time.get_ticks() >= respawn_time:
is_dead = False
if own_player_id in players:
spawn_pos = players[own_player_id]['position']
character.x, character.y = spawn_pos
character_velocity_x, character_velocity_y = 0, 0
is_jumping = False
bullets.clear()
# Inform server about respawn
send_data({'name': player_name, 'position': (character.x, character.y), 'respawn': True})
# Draw everything
screen.fill(BLUE)
# Draw the map layout
for y, row in enumerate(map_layout):
for x, tile_id in enumerate(row):
tile_image = tile_images.get(tile_id)
if tile_image:
screen.blit(tile_image, (x * TILE_SIZE, y * TILE_SIZE))
# Draw all players
for pid, info in players.items():
pos = info['position']
if pid != own_player_id and pid in interpolation_data:
pos = interpolation_data[pid]['current_pos']
color = info['color']
health = info.get('health', 100)
player_rect = pygame.Rect(int(pos[0]), int(pos[1]), 50, 50)
pygame.draw.rect(screen, color, player_rect)
name = info.get('name', 'Unknown')
text_surface = SMALL_FONT.render(name, True, BLACK)
screen.blit(text_surface, (player_rect.x, player_rect.y - 20)) # Display name above player
# Draw health bar
health_bar_width = 50
health_bar_height = 5
health_ratio = max(health / 100, 0)
pygame.draw.rect(screen, RED, (player_rect.x, player_rect.y - 10, health_bar_width, health_bar_height))
pygame.draw.rect(screen, GREEN, (player_rect.x, player_rect.y - 10, health_bar_width * health_ratio, health_bar_height))
# Draw all bullets from other players
for pid, info in players.items():
if pid == own_player_id:
continue # Skip own bullets
for b in info.get('bullets', []):
bullet_rect = pygame.Rect(b['x'], b['y'], 10, 5)
pygame.draw.rect(screen, RED, bullet_rect)
# Draw local bullets
for bullet in bullets:
bullet.draw(screen)
# Update the display
pygame.display.flip()
# Cap the frame rate
clock.tick(60)
# Handle death and respawn if necessary
if own_player_id in players:
if players[own_player_id].get('health', 100) <= 0 and not is_dead:
is_dead = True
respawn_time = pygame.time.get_ticks() + 3000 # 3 seconds cooldown
print("You have been eliminated! Respawning in 3 seconds...")
# Reset local character position to server's value for consistency
spawn_pos = players[own_player_id].get('position', (375, 375))
character.x, character.y = spawn_pos
character_velocity_x, character_velocity_y = 0, 0
is_jumping = False
# Close the client socket and quit Pygame
client.close()
pygame.quit()
sys.exit()
Conclusion
By implementing frame-based data sending and interpolation for other players' movements, you can significantly reduce network congestion and improve the game's performance under high ping conditions. These modifications ensure that your game remains responsive and provides a smooth experience for all players.
Feel free to adjust the SEND_EVERY_FRAMES and INTERPOLATION_SPEED constants to better suit your game's specific needs and target network environments. Additionally, consider further optimizations and advanced networking techniques as your game evolves.