我想创建一个 "画点什么 "的游戏,至少有三个来自不同电脑的参与者。
我有一个代码,适用于两个玩家。有谁知道如何能让三个人或更多人参与?
...............................................................
这是我的服务器代码。
...............................................................
server = ""
port = 5555
games = {}
idCount = 0
currentPlayer = 0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((server, port))
s.listen()
print("Waiting for a connection, Server Started")
class Game:
def __init__(self, id):
self.connected = False
players = [Player(0), Player(1)]
def threaded_client(conn, player, gameId):
global idCount
conn.send(pickle.dumps(players[player]))
reply = ""
while True:
try:
data = pickle.loads(conn.recv(2048))
players[player] = data
if gameId in games:
game = games[gameId]
if not data:
print("Disconnected")
break
else:
if player == 1:
reply = players[0]
else:
reply = players[1]
conn.sendall(pickle.dumps(reply))
except:
break
print("Lost connection")
try:
del games[gameId]
print("Closing game", gameId)
except:
pass
idCount -= 1
conn.close()
while True:
conn, addr = s.accept()
print("Connected to:", addr)
idCount += 1
p = 0
gameId = (idCount - 1) // 2
if idCount % 2 == 1:
games[gameId] = Game(gameId)
players[0].connected = True
print("Creating a new game")
print("Waiting for another player")
else:
games[gameId].connected = True
p = 1
players[1].connected = True
print("Game is available")
start_new_thread(threaded_client, (conn, currentPlayer, gameId))
currentPlayer += 1
...............................................................
谢谢:) :) :) :)
我不知道这是否有帮助,但我有一个旧的2人游戏。
客户端:
import socket, pickle, pygame, pygame.gfxdraw
pygame.init()
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption('client')
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = int(input(f'port: '))
mysocket.connect((socket.gethostname(), port))
def player(pos, color):
x1, y1 = pos
x2 = x1+10
y2 = y1
x3 = x1+10
y3 = y1+10
x4 = x1
y4 = y1+10
pygame.gfxdraw.filled_polygon(screen, [(x1, y1), (x2, y2), (x3, y3), (x4, y4)], color)
player1 = (0, 0)
up, down, left, right = False, False, False, False
while True:
tosend = player1
mysocket.send(pickle.dumps(tosend))
player2 = pickle.loads(mysocket.recv(1024))
screen.fill(0)
player(player1, (0, 255, 0))
player(player2, (0, 0, 255))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
up = True
if event.key == pygame.K_DOWN:
down = True
if event.key == pygame.K_LEFT:
left = True
if event.key == pygame.K_RIGHT:
right = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
up = False
if event.key == pygame.K_DOWN:
down = False
if event.key == pygame.K_LEFT:
left = False
if event.key == pygame.K_RIGHT:
right = False
if up:
ychange = -1
elif down:
ychange = 1
else:
ychange = 0
if left:
xchange = -1
elif right:
xchange = 1
else:
xchange = 0
x, y = player1
x += xchange
y += ychange
player1 =(x, y)
mysocket.send(pickle.dumps('stop'))
mysocket.close()
服务器:
import socket, pygame, pickle, pygame.gfxdraw
pygame.init()
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption('server')
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 1000
while True:
try:
mysocket.bind((socket.gethostname(), port))
break
except:
port += 1
print(f'hosting server on {socket.gethostname()}:{port}')
def player(pos, color):
x1, y1 = pos
x2 = x1+10
y2 = y1
x3 = x1+10
y3 = y1+10
x4 = x1
y4 = y1+10
pygame.gfxdraw.filled_polygon(screen, [(x1, y1), (x2, y2), (x3, y3), (x4, y4)], color)
player1 = (0, 0)
up, down, left, right = False, False, False, False
running = True
while running:
# socket actions here
mysocket.listen()
client, address = mysocket.accept()
while True:
try:
player2 = pickle.loads(client.recv(1024))
client.send(pickle.dumps(player1))
if player2 == 'stop':
break
except:
break
# pygame actions here
try:
screen.fill(0)
player(player1, (0, 255, 0))
player(player2, (0, 0, 255))
pygame.display.flip()
except:
pass
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
up = True
if event.key == pygame.K_DOWN:
down = True
if event.key == pygame.K_LEFT:
left = True
if event.key == pygame.K_RIGHT:
right = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
up = False
if event.key == pygame.K_DOWN:
down = False
if event.key == pygame.K_LEFT:
left = False
if event.key == pygame.K_RIGHT:
right = False
if up:
ychange = -1
elif down:
ychange = 1
else:
ychange = 0
if left:
xchange = -1
elif right:
xchange = 1
else:
xchange = 0
x, y = player1
x += xchange
y += ychange
player1 =(x, y)
print('connection lost')
mysocket.close()