如何使用套接字编程发送pygame屏幕

时间:2017-04-04 14:15:48

标签: python-2.7 pygame serversocket

我尝试使用套接字编程将我的pygame屏幕发送到我的另一台PC,但它在client.py中出错了实际上在pygame中的新内容中为什么会发生这种情况我没有得到它

  

回溯(最近一次呼叫最后一次):文件" webclient.py",第24行,          image = pygame.image.fromstring(dataset,(320,240)," RGB")#转换接收到的图像来自字符串ValueError:字符串长度不相等   格式和分辨率大小

并在server.py

  

Traceback(最近一次调用最后一次):文件   " /home/gaurav/Desktop/Games_Project/panda.py" ;,第35行,在       image = screen.blit(bg,(0,0))AttributeError:' NoneType'对象没有属性' 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()

1 个答案:

答案 0 :(得分:0)

第一个错误:

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。它会将图像推到屏幕上的某个位置。它的返回值是一个 pygame.Rect 对象,但它看起来像是在期待一个图像。

然后,您正在尝试发送此 pygame.Rect 对象,并且会发生此错误,因为您的代码需要一个包含图像的字符串,但它正在接收表示<的字符串< strong> pygame.Rect 对象。

您似乎正在尝试发送 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 来显示。在第二行中,您尝试设置窗口标题。这是您的错误发生的地方。查看此函数的pygame文档:

pygame.display.set_caption()
    Set the current window caption
    set_caption(title, icontitle=None) -> None

该函数返回None!这意味着当您尝试调用 screen.blit 时,屏幕现在等于无,因此错误。

要解决问题,请更改此行:

screen=pygame.display.set_caption("Game by Players")

pygame.display.set_caption("Game by Players")