简体   繁体   English

Python,Pygame,Pyro:如何通过网络发送表面?

[英]Python, Pygame, Pyro: How to send a surface over a network?

I am working on a project in python using pygame and pyro. 我正在使用pygame和pyro在python中进行项目。 I can send data, functions, classes, and the like easily. 我可以轻松地发送数据,函数,类等。 However, I cannot send a surface across the wire without it dying on me in transit. 但是,如果没有在传输过程中使我死亡,我就无法通过电线发送表面。

The server makes a surface in the def __init__ of the class being accessed across the wire: 服务器在通过导线访问的类的def __init__中创建一个表面:

self.screen = pygame.display.set_mode(SCREENRECT.size, NOFRAME)

On the server, the screen prints as Surface(800x800x32 SW) but when retrieved by the client it is Surface(Dead Display) . 在服务器上,屏幕显示为Surface(800x800x32 SW)但在客户端检索时为Surface(Dead Display)

Something to note though. 不过要注意一点。 I get a dead display when I use an accessor function to get my screen. 当我使用访问器功能获取屏幕时,我的屏幕显示呆滞。 If I use print Player.screen to get the variable I instead get what seems to be a pyro pointer to the screen: <Pyro.core._RemoteMethod instance at 0x02B7B7B0> . 如果我使用print Player.screen来获取变量,那么我会得到似乎是指向屏幕的pyro指针: <Pyro.core._RemoteMethod instance at 0x02B7B7B0> Maybe I can dereference this? 也许我可以取消引用?

More than likely I am being thick, does anyone have some insight? 我很可能很胖,有人有见识吗? Thanks. 谢谢。 :) :)

A pygame Surface is a wrapper around an underlying SDL surface, which I suspect can't be serialized by Pyro. pygame曲面是底层SDL曲面的包装,我怀疑Pyro无法对其进行序列化。 If you want to copy its contents across the wire, you would be better off doing something like this: 如果要跨线复制其内容,最好做这样的事情:

  1. on the server use Surface.get_buffer() to get access to the underlying pixels. 在服务器上,使用Surface.get_buffer()可以访问基础像素。
  2. make a note of the Surface's dimensions, colour depth, etc. 记下Surface的尺寸,颜色深度等。
  3. send the data obtained from steps 1 and 2 over the wire to the client. 通过有线将从步骤1和步骤2获得的数据发送给客户端。
  4. on the client create a new surface using the dimensions, colour depth, etc, from step 2. 在客户端上,使用步骤2中的尺寸,颜色深度等来创建新的表面。
  5. set the new Surface's pixels using Surface.get_buffer() and copying in the pixels from step 1. 使用Surface.get_buffer()设置新的Surface像素,并复制第1步中的像素。

Edit: It just occurred to me that I'm overcomplicating it. 编辑:我只是觉得我太复杂了。 To serialise your Surface, use pygame.image.tostring() , and to reload it, use pygame.image.fromstring() . 要序列化Surface,请使用pygame.image.tostring() ,并重新加载它,请使用pygame.image.fromstring()

Generally speaking, you don't want to send a Surface (I'm assuming that a Surface is a device-dependent display) across the network. 一般来说,您不想在网络上发送Surface(我假设Surface是与设备相关的显示器)。 Most of the time, your client will be responsible for managing the drawing on its local Surface, and your server is responsible for telling the client what it needs to draw. 在大多数情况下,您的客户将负责管理其本地Surface上的图形,而您的服务器则负责告诉客户它需要绘制什么。 A server may not even have a display that's capable of showing graphics! 服务器甚至可能没有显示这是能够显示图形!

尝试腌制对象并发送文件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM