我随后跟随techwithtim制作mmorpg的教程,我从视频4中复制了以下代码。所有这些都发生在禁用防火墙和bitdefender的同一台计算机上。
https://techwithtim.net/tutorials/python-online-game-tutorial/connecting-multiple-clients/
我运行'gamerun':
import socket
from _thread import *
import sys
server='192.168.142.1'
port=5555
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.bind((server,port))
except socket.error as e:
str(e)
s.listen(2)
print("waiting...")
def read_pos(str):
str=str.split(',')
return int(str[0]),int(str[1])
def make_pos(tup):
return str(tup[0])+','+str(tup[1])
pos=[(0,0),(100,100)]
def threaded_client(conn, player):
conn.send(str.encode(make_pos(pos[player])))
reply=''
while True:
try:
data=read_pos(conn.recv(2048).decode())
pos[player]=data
print(player)
if not data:
print('dc')
break
else:
print('yay')
if player == 1:
reply=pos[0]
else:
reply=pos[1]
print('rcv:',data)
print('snd:',reply)
conn.sendall(str.encode(make_pos(reply)))
except:
break
print('lost conn')
conn.close()
currentPlayer=0
while True:
conn, addr=s.accept()
print("connected to:", addr)
start_new_thread(threaded_client, (conn,currentPlayer))
currentPlayer+=1
然后我运行以下命令,将其复制到两个名称不同的文件中,因为这会引起问题。叫做“游戏”
from talkydaemon import talky
width=500
height=500
win=pygame.display.set_mode((width, height))
pygame.display.set_caption("Client")
clientNumber=0
class Player():
def __init__(self, x, y, width, height, color):
self.x=x
self.y=y
self.width=width
self.height=height
self.color=color
self.rect=x,y,width,height
self.vel=.5
def draw(self,win):
pygame.draw.rect(win, self.color, self.rect)
def move(self):
keys=pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.x-=self.vel
if keys[pygame.K_RIGHT]:
self.x+=self.vel
if keys[pygame.K_UP]:
self.y-=self.vel
if keys[pygame.K_DOWN]:
self.y+=self.vel
self.update()
def update(self):
self.rect=(self.x,self.y,self.width,self.height)
def read_pos(str):
str=str.split(',')
return int(str[0]),int(str[1])
def make_pos(tup):
return str(tup[0])+','+str(tup[1])
def redrawWindow(win,player,player2):
win.fill((255,255,255))
player.draw(win)
player2.draw(win)
pygame.display.update()
def main():
run=True
t=talky()
pstart=read_pos(t.getPos())
p=Player(pstart[0],pstart[1],100,100,(0,255,255))
p2=Player(0,0,100,100,(0,255,255))
while run:
p2pos=(100,100)
print('catch '+t.send(make_pos((p.x,p.y))))
p2.x=p2pos[0]
p2.y=p2pos[1]
p2.update
for event in pygame.event.get():
if event.type==pygame.QUIT:
run=False
pygame.quit()
p.move()
redrawWindow(win,p,p2)
main()
正在调用talkydaemon:
import socket
class talky:
def __init__(self):
self.client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.server='192.168.142.1'
self.port=5555
self.addr=(self.server,self.port)
self.pos=self.connect()
print(self.pos)
def connect(self):
try:
self.client.connect(self.addr)
return self.client.recv(2048).decode()
except:
pass
def send(self, data):
try:
self.client.send(str.encode(data))
return self.client.recv(2048).decode()
except socket.error as e:
return str(e)
def getPos(self):
return self.pos
这是代码或操作系统的问题吗?我曾尝试禁用bitdefender和Windows防火墙,但其行为仍然相同。
我在游戏机中获得以下内容:
catch 0,0
catch 0,0...
,然后在我移入任何一个客户端中后:
catch [WinError 10053] An establish connection was aborted by the software in your host machine
catch [WinError 10053] An establish connection was aborted by the software in your host machine
catch [WinError 10053] An establish connection was aborted by the software in your host machine...```
while the other client continues to spit positions. where can i look?