-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
117 lines (96 loc) · 3.71 KB
/
Copy pathclient.py
File metadata and controls
117 lines (96 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import pygame
import socket
import pickle
class GameClient:
def __init__(self, host='localhost', port=5555):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = host
self.port = port
self.player_id = None
self.players = {}
def connect(self):
try:
self.client.connect((self.host, self.port))
# Receive player ID from server
self.player_id = pickle.loads(self.client.recv(4096))
print(f"Connected as Player {self.player_id}")
return True
except Exception as e:
print(f"Connection failed: {e}")
return False
def send(self, data):
try:
self.client.send(pickle.dumps(data))
return pickle.loads(self.client.recv(4096))
except Exception as e:
print(f"Send error: {e}")
return None
class Game:
def __init__(self):
pygame.init()
self.width = 800
self.height = 600
self.screen = pygame.display.set_mode((self.width, self.height))
pygame.display.set_caption("Multiplayer Game")
self.clock = pygame.time.Clock()
self.running = True
self.client = GameClient()
if not self.client.connect():
self.running = False
return
self.player_x = 400
self.player_y = 300
self.player_speed = 5
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
self.player_x -= self.player_speed
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
self.player_x += self.player_speed
if keys[pygame.K_UP] or keys[pygame.K_w]:
self.player_y -= self.player_speed
if keys[pygame.K_DOWN] or keys[pygame.K_s]:
self.player_y += self.player_speed
# Keep player in bounds
self.player_x = max(20, min(self.width - 20, self.player_x))
self.player_y = max(20, min(self.height - 20, self.player_y))
# Send position to server and get all players
player_data = {'x': self.player_x, 'y': self.player_y}
self.players = self.client.send(player_data)
if self.players is None:
self.running = False
def draw(self):
self.screen.fill((30, 30, 30))
# Draw all players
if self.players:
for pid, pdata in self.players.items():
color = pdata['color']
x, y = int(pdata['x']), int(pdata['y'])
# Draw player circle
pygame.draw.circle(self.screen, color, (x, y), 20)
# Draw player ID
font = pygame.font.Font(None, 24)
if pid == self.client.player_id:
text = font.render(f"You (P{pid})", True, (255, 255, 255))
else:
text = font.render(f"P{pid}", True, (255, 255, 255))
self.screen.blit(text, (x - 30, y - 40))
# Draw instructions
font = pygame.font.Font(None, 20)
text = font.render("Use WASD or Arrow Keys to move", True, (200, 200, 200))
self.screen.blit(text, (10, 10))
pygame.display.flip()
def run(self):
while self.running:
self.handle_events()
self.update()
self.draw()
self.clock.tick(60)
pygame.quit()
if __name__ == "__main__":
game = Game()
game.run()