簡體   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