如何使用套接字编程发送 pygame 屏幕
how to send pygame screen using socket programming
我尝试使用套接字编程将我的 pygame 屏幕发送到我的另一台电脑,但它在 client.py 中给出错误实际上在 pygame 中是新的所以为什么这些发生我没有明白了
Traceback (most recent call last): File "webclient.py", line 24, in
image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string ValueError: String length does not equal
format and resolution size
以及 server.py
Traceback (most recent call last): File
"/home/gaurav/Desktop/Games_Project/panda.py", line 35, in
image = screen.blit(bg, (0, 0)) AttributeError: 'NoneType' object has no attribute 'blit'
这是我的server.py
import pygame
import socket
import os
import time
width=800
height=600
fps=60
port=5012
imgdir=os.path.join(os.path.dirname(__file__),"img")
serversocket =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serversocket.bind(("",port))
serversocket.listen(1)
pygame.init()
clock=pygame.time.Clock()
screen=pygame.display.set_mode((width,height))
screen=pygame.display.set_caption("Game by Players")
bg = pygame.image.load(os.path.join(imgdir,"starfield.png")).convert()
running=True
while running:
clock.tick(fps)
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
running = False
connection, address = serversocket.accept()
image = screen.blit(bg, (0, 0))
pygame.display.flip()
data = pygame.image.tostring(image, "RGB") # convert captured image to string, use RGB color scheme
connection.sendall(data)
connection.close()
pygame.quit()
这是我的client.py
import socket
import pygame
import sys
host = "127.0.0.1"
port=5012
screen = pygame.display.set_mode((320,240),0)
while True:
clientsocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect((host, port))
received = []
# loop .recv, it returns empty string when done, then transmitted data is completely received
while True:
recvd_data = clientsocket.recv(230400)
if not recvd_data:
break
else:
received.append(recvd_data)
dataset = ''.join(received)
image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string
screen.blit(image,(0,0)) # "show image" on the screen
pygame.display.update()
# check for quit events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
第一个错误:
Traceback (most recent call last): File "webclient.py", line 24,
in image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string
ValueError: String length does not equal format and resolution size
我不确定您是否确切了解 blit 的作用。它会将图像推送到屏幕上的某个位置。它的 return 值是 pygame.Rect object,但看起来您期待的是图像。
然后,您正试图发送此 pygame.Rect object,并且会发生此错误,因为您的代码需要一个包含图像,但它正在接收表示 pygame.Rect object.
的字符串
您似乎正在尝试发送 bg 图片。为此,请在 server.py:
中更改此代码
data = pygame.image.tostring(image, "RGB") # convert captured image to string, use RGB color scheme
至
data = pygame.image.tostring(bg, "RGB") # convert captured image to string, use RGB color scheme
第二个错误:
Traceback (most recent call last): File "panda.py", line 35,
in image = screen.blit(bg, (0, 0)) AttributeError: 'NoneType' object has no attribute 'blit'
看看这些行:
screen=pygame.display.set_mode((width,height))
screen=pygame.display.set_caption("Game by Players")
在第一行中,您正确地创建了 pygame.Surface 来显示。在第二行中,您试图设置 window 标题。这是您的错误发生的地方。查看此函数的 pygame 文档:
pygame.display.set_caption()
Set the current window caption
set_caption(title, icontitle=None) -> None
函数returns None!这意味着当您尝试调用 screen.blit 时,您的 screen 现在等于 None,因此出现错误。
要解决此问题,请更改此行:
screen=pygame.display.set_caption("Game by Players")
至
pygame.display.set_caption("Game by Players")
我尝试使用套接字编程将我的 pygame 屏幕发送到我的另一台电脑,但它在 client.py 中给出错误实际上在 pygame 中是新的所以为什么这些发生我没有明白了
Traceback (most recent call last): File "webclient.py", line 24, in image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string ValueError: String length does not equal format and resolution size
以及 server.py
Traceback (most recent call last): File "/home/gaurav/Desktop/Games_Project/panda.py", line 35, in image = screen.blit(bg, (0, 0)) AttributeError: 'NoneType' object has no attribute 'blit'
这是我的server.py
import pygame
import socket
import os
import time
width=800
height=600
fps=60
port=5012
imgdir=os.path.join(os.path.dirname(__file__),"img")
serversocket =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serversocket.bind(("",port))
serversocket.listen(1)
pygame.init()
clock=pygame.time.Clock()
screen=pygame.display.set_mode((width,height))
screen=pygame.display.set_caption("Game by Players")
bg = pygame.image.load(os.path.join(imgdir,"starfield.png")).convert()
running=True
while running:
clock.tick(fps)
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
running = False
connection, address = serversocket.accept()
image = screen.blit(bg, (0, 0))
pygame.display.flip()
data = pygame.image.tostring(image, "RGB") # convert captured image to string, use RGB color scheme
connection.sendall(data)
connection.close()
pygame.quit()
这是我的client.py
import socket
import pygame
import sys
host = "127.0.0.1"
port=5012
screen = pygame.display.set_mode((320,240),0)
while True:
clientsocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect((host, port))
received = []
# loop .recv, it returns empty string when done, then transmitted data is completely received
while True:
recvd_data = clientsocket.recv(230400)
if not recvd_data:
break
else:
received.append(recvd_data)
dataset = ''.join(received)
image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string
screen.blit(image,(0,0)) # "show image" on the screen
pygame.display.update()
# check for quit events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
第一个错误:
Traceback (most recent call last): File "webclient.py", line 24,
in image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string
ValueError: String length does not equal format and resolution size
我不确定您是否确切了解 blit 的作用。它会将图像推送到屏幕上的某个位置。它的 return 值是 pygame.Rect object,但看起来您期待的是图像。
然后,您正试图发送此 pygame.Rect object,并且会发生此错误,因为您的代码需要一个包含图像,但它正在接收表示 pygame.Rect object.
的字符串您似乎正在尝试发送 bg 图片。为此,请在 server.py:
中更改此代码data = pygame.image.tostring(image, "RGB") # convert captured image to string, use RGB color scheme
至
data = pygame.image.tostring(bg, "RGB") # convert captured image to string, use RGB color scheme
第二个错误:
Traceback (most recent call last): File "panda.py", line 35,
in image = screen.blit(bg, (0, 0)) AttributeError: 'NoneType' object has no attribute 'blit'
看看这些行:
screen=pygame.display.set_mode((width,height))
screen=pygame.display.set_caption("Game by Players")
在第一行中,您正确地创建了 pygame.Surface 来显示。在第二行中,您试图设置 window 标题。这是您的错误发生的地方。查看此函数的 pygame 文档:
pygame.display.set_caption()
Set the current window caption
set_caption(title, icontitle=None) -> None
函数returns None!这意味着当您尝试调用 screen.blit 时,您的 screen 现在等于 None,因此出现错误。
要解决此问题,请更改此行:
screen=pygame.display.set_caption("Game by Players")
至
pygame.display.set_caption("Game by Players")