繁体   English   中英

Pygame 开始全屏然后崩溃

[英]Pygame starting at fullscreen and then crashing

这是缩短的服务器代码,它应该从客户端接收屏幕截图,然后在服务器端的 pygame 屏幕上显示:

'''max_bytes = 64000
    udp_conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udp_conn.bind(('0.0.0.0', 5000))
    pygame.init()
    screen = pygame.display.set_mode((1900, 1000))
    clock = pygame.time.Clock()
    while True:
        try:
            size_msg, address = udp_conn.recvfrom(max_bytes)
            size = int.from_bytes(size_msg, byteorder='big')
            while size > 10000000:  # if size is greater than 10M then loop has to fire to get value of the size of
                # the compressed img.
                size = int.from_bytes(size_msg, byteorder='big')
            temp_pixels = recvall(size, udp_conn, max_bytes)
            try:
                pixels = decompress(temp_pixels)
                scrn_img = pygame.image.fromstring(pixels, (950, 500), 'RGB')
                scrn_shot = pygame.transform.scale(scrn_img, (950, 500))
                # putting the screenshot on pygame screen
                screen.blit(scrn_shot, (0, 0))
                pygame.display.flip()
                clock.tick(60)
            except:
                pass
        except:
            pass
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        pygame.quit()
                        sys.exit()'''

pygame 屏幕以全屏启动,然后调整大小并崩溃……我不知道为什么。 任何帮助,将不胜感激。

我认为事件的 for 循环在 except 块中检查缩进并重试

那很好
并尝试将您的代码从这个更新到这个

max_bytes = 64000
udp_conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_conn.bind(('0.0.0.0', 5000))
pygame.init()
screen = pygame.display.set_mode((1900, 1000))
clock = pygame.time.Clock()
while True:
    try:
        size_msg, address = udp_conn.recvfrom(max_bytes)
        size = int.from_bytes(size_msg, byteorder='big')
        while size > 10000000:  # if size is greater than 10M then loop has to fire to get value of the size of
            # the compressed img.
            size = int.from_bytes(size_msg, byteorder='big')
        temp_pixels = recvall(size, udp_conn, max_bytes)
        try:
            pixels = decompress(temp_pixels)
            scrn_img = pygame.image.fromstring(pixels, (950, 500), 'RGB')
            scrn_shot = pygame.transform.scale(scrn_img, (950, 500))
            # putting the screenshot on pygame screen
            screen.blit(scrn_shot, (0, 0))
            pygame.display.flip()
            clock.tick(60)
        except:
            pass
    except:
        pass
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()

至此

max_bytes = 64000
udp_conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_conn.bind(('0.0.0.0', 5000))
pygame.init()
screen = pygame.display.set_mode((1900, 1000))
clock = pygame.time.Clock()
while True:
    try:
        size_msg, address = udp_conn.recvfrom(max_bytes)
        size = int.from_bytes(size_msg, byteorder='big')
        while size > 10000000:  # if size is greater than 10M then loop has to fire to get value of the size of
            # the compressed img.
            size = int.from_bytes(size_msg, byteorder='big')
        temp_pixels = recvall(size, udp_conn, max_bytes)
        try:
            pixels = decompress(temp_pixels)
            scrn_img = pygame.image.fromstring(pixels, (950, 500), 'RGB')
            scrn_shot = pygame.transform.scale(scrn_img, (950, 500))
        except:
            pass
    except:
        pass
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()
    # putting the screenshot on pygame screen
    screen.blit(scrn_shot, (0, 0))
    pygame.display.flip()
    clock.tick(60)

这是因为更新应该发生在每一帧上,而不仅仅是在加载图像时。
为此,您可能应该为scrn_shot变量声明一个默认值。
并在 screen.blit 上方添加 screen.fill(( screen.blit screen.fill((COLOR_IN_RGB)) ,这将每帧用黑色填充屏幕。

而且您的程序应该可以正常工作。

暂无
暂无

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

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