程序挂起,可能是基于套接字的

时间:2014-08-01 06:23:21

标签: python sockets networking pygame

所以我得到了这个.py文件,主文件调用该文件将程序发送到主机服务器并等待有人连接到它。'模式,工作正常和花花公子,除非你退出它然后回到它,程序冻结。

#data.host
#the host with the most

#import modules
import os, sys, pygame #basic stuff
from pygame.locals import * #fiddly bits
import socket
from data.graphics import Letter,Flicker,BackButton,FlickerHandler
import config as conf
import threading
from data.settings import *

def net_srv(): #don't ask me, look it up
    global sock, connected

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('', port))
    sock.listen(1); connected = False

class HostScreen():
    def __init__(self):
        pass
    def enter(self, client):
        conf.state = conf.STATE_HOSTING
        size = (width,height) = [640,480]
        screen = pygame.display.set_mode(size)
        dt = 0.0
        clock = pygame.time.Clock()
        background = pygame.Surface(screen.get_size())
        background = background.convert()
        background.fill((0, 0, 0))
        font = pygame.font.Font(os.path.join("data","graphics","lucon.ttf"), 11)
        text = font.render("Waiting for other player to connect.", 1, (255, 255, 255))
        textpos = text.get_rect()
        textpos.center = background.get_rect().center
        textpos.centery -= 10
        f = Letter("f")
        l = Letter("l")
        i = Letter("i")
        c = Letter("c")
        k = Letter("k")
        e = Letter("e")
        r = Letter("r")
        flicker = Flicker()
        handler = FlickerHandler()
        back = BackButton()
        allsprites2 = (f,l,i,c,k,e,r)
        clickables = [back]
        for s in allsprites2:
            s.update(1)
            s.rect.centery = s.rect.centery - 50
        allsprites = pygame.sprite.LayeredDirty(allsprites2,flicker,clickables)
        flicker.update(1)
        timetoFlicker = 0.5
        titleSwitch = 0
        net_srv()

        def connect():
            global conn, addr, connected
            conn, addr = sock.accept()
            connected = True

        thread = threading.Thread(target=connect)
        thread.start()

        while 1:
            dt = (clock.tick(60)/1000.0)
            timetoFlicker -= dt
            pygame.display.set_caption("Flicker  %d fps" % clock.get_fps())
            events = pygame.event.get()
            for event in events:
                if event.type == pygame.QUIT: sys.exit() #Makes the X button work.
                if event.type == MOUSEBUTTONDOWN: #Checking for clicking.
                    if event.button == 1:
                        for object in clickables:
                            if object.rect.collidepoint(pygame.mouse.get_pos()) and object.clickCheck() == "back":
                                flicker.update(1)
                                timetoFlicker = 0.09
                                titleSwitch = 1
            if connected: return (conn, addr, 'server')
            elif not thread.is_alive():
                flicker.update(1)
                timetoFlicker = 0.09
                titleSwitch = 1
            if timetoFlicker<=0:
                timetoFlicker,vis = handler.flicker(flicker)
                flicker.update(vis)
                for object in clickables:
                    object.update(vis)
                if titleSwitch > 0 and vis == 0:
                    timetoFlicker = 0.02
                elif titleSwitch > 0:
                    sock.close()
                    return False
            pygame.event.pump()
            if not flicker.visible:
                screen.blit(text, textpos)
            rects = allsprites.draw(screen)
            pygame.display.update(rects)
            pygame.display.flip()

任何可能导致挂起的想法?

0 个答案:

没有答案