So I've got this .py file which is called by the main file to send the program into a 'host server and wait for someone to connect to it' mode, which works fine and dandy, except that if you back out of it and then go back into it, the program freezes.
#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()
Any ideas what could be causing the hang?