【问题标题】:how to send pygame screen using socket programming如何使用套接字编程发送pygame屏幕
【发布时间】:2017-04-05 05:20:10
【问题描述】:

我尝试使用套接字编程将我的 pygame 屏幕发送到我的另一台电脑,但它在 client.py 中给出错误实际上在 pygame 中是新的,所以为什么会发生这些我没有得到它

Traceback(最近一次调用最后一次):文件“webclient.py”,第 24 行,在 image = pygame.image.fromstring(dataset,(320,240),"RGB") # 从字符串中转换接收到的图像 ValueError: String length does not equal 格式和分辨率大小

在 server.py 上

Traceback(最近一次调用最后一次):文件 “/home/gaurav/Desktop/Games_Project/panda.py”,第 35 行,在 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()

【问题讨论】:

    标签: python-2.7 pygame serversocket


    【解决方案1】:

    第一个错误:

    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 对象,并且会发生此错误,因为您的代码需要一个包含图像的字符串,但它正在接收一个表示 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
    

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

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

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

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

    【讨论】:

    • 如何使用 pygame 和 socket 编程创建多人游戏
    • 我不是来写你的程序的。我已经帮助修复了你的错误,现在做必要的工作来实现你想要的。
    • 我只想要多人游戏的概念
    • 多人游戏超出了您的能力范围。考虑从小处着手,做一个你可以真正完成的游戏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2022-06-12
    相关资源
    最近更新 更多