微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

套接字程序不响应pygame

如何解决套接字程序不响应pygame

我有2个用于在python和pygame中制作的套接字应用程序的程序,有2个rect,都可以使用单独的计算机移动,但无法正常工作,当我运行该程序时,pygame窗口为黑色,如果我单击它说没有响应,然后很快退出。我环顾四周,但找不到解决方法

听到我的代码

import socket,pygame,struct
screen = pygame.display.set_mode([500,500])
screen.fill([255,255,255])
red = pygame.Rect(250,250,50,50)
blue = pygame.Rect(150,150,50)
s = socket.socket()
port = 12345
s.bind(('',port))
s.listen(5)
c,addr = s.accept()
print ("Socket Up and running with a connection from",addr)
while True:
    pygame.draw.rect(screen,[255,0],red)
    pygame.draw.rect(screen,[0,255],blue)
    pygame.display.flip()
    buf = c.recv(8,socket.MSG_WAITALL)
    struct.unpack("2i",buf)
    x,y = struct.unpack("2i",buf)
    red = pygame.Rect(x,y,50)
    screen.fill([255,255])
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        blue.x -= 1
        if blue.x <= 0:
            blue.x +=1
    if keys[pygame.K_RIGHT]:
        blue.x += 1
        if blue.x >= 450:
            blue.x -=1
    if keys[pygame.K_UP]:
        blue.y -= 1
        if blue.y <= 0:
            blue.y +=1
    if keys[pygame.K_DOWN]:
        blue.y += 1
        if blue.y >= 450:
            blue.y -=1
    s.send(struct.pack("2i",blue.x,blue.y))
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                
                pygame.quit()
                s.close()
        if event.type == pygame.QUIT:
            
                
            pygame.quit()
            s.close()

另一端:

import socket,50)
s = socket.socket()
s.connect(('192.168.43.188',12345))
while True:
    screen.fill([255,255])
    pygame.draw.rect(screen,blue)
    pygame.display.flip()
    buf = s.recv(8,buf)
    blue = pygame.Rect(x,50)
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        red.x -= 1
        if red.x <= 0:
            red.x +=1
    if keys[pygame.K_RIGHT]:
        red.x += 1
        if red.x >= 450:
            red.x -=1
    if keys[pygame.K_UP]:
        red.y -= 1
        if red.y <= 0:
            red.y +=1
    if keys[pygame.K_DOWN]:
        red.y += 1
        if red.y >= 450:
            red.y -=1
    s.send(struct.pack("2i",red.x,red.y))
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                
                pygame.quit()
                s.close()
        if event.type == pygame.QUIT:
            
                
            pygame.quit()
            s.close()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。