def lineReceived(self, data): #will happen if server only has one player connected (you) if data == 'waiting for players': print 'Waiting for another player..' elif data == 'lost conn': print 'Connection was lost, sorry' reactor.stop() #will happen if server has two connection. Will send your player number over (PN) along with random seed elif data.startswith('PN'): comp = data.split(':') self.playerNumber = int(comp[1]) #so that both clients have the same seed to create fruits self.randSeed = int(comp[2]) self.minFruitSpeed = int(comp[3]) self.maxFruitSpeed = int(comp[4]) self.client.blenderConn.playerNumber = self.playerNumber if self.playerNumber == 1: print 'Game Started: You are the Pink Player' else: print 'Game Started: You are the Green Player' #create the gamespace and start the loop self.gs = GameSpace(self, self.playerNumber, self.randSeed, self.minFruitSpeed, self.maxFruitSpeed) self.client.blenderConn.gs = self.gs self.gs.main() self.lc = LoopingCall(self.gs.countDown) self.lc.start(1 / 60) #start sending blender data continuously self.client.blenderConn.sendMyData() #when you receive gameplay data (will either be to add a fruit or freeze a fruit) else: self.parseData(data)
def lineReceived(self, data): # will happen if server only has one player connected (you) if data == "waiting for players": print "Waiting for another player.." elif data == "lost conn": print "Connection was lost, sorry" reactor.stop() # will happen if server has two connection. Will send your player number over (PN) along with random seed elif data.startswith("PN"): comp = data.split(":") self.playerNumber = int(comp[1]) # so that both clients have the same seed to create fruits self.randSeed = int(comp[2]) self.minFruitSpeed = int(comp[3]) self.maxFruitSpeed = int(comp[4]) self.client.blenderConn.playerNumber = self.playerNumber if self.playerNumber == 1: print "Game Started: You are the Pink Player" else: print "Game Started: You are the Green Player" # create the gamespace and start the loop self.gs = GameSpace(self, self.playerNumber, self.randSeed, self.minFruitSpeed, self.maxFruitSpeed) self.client.blenderConn.gs = self.gs self.gs.main() self.lc = LoopingCall(self.gs.countDown) self.lc.start(1 / 60) # start sending blender data continuously self.client.blenderConn.sendMyData() # when you receive gameplay data (will either be to add a fruit or freeze a fruit) else: self.parseData(data)
class FruitConn(LineReceiver): def __init__(self, client): self.client = client self.fruitQueue = DeferredQueue() self.fruitQueue.get().addCallback(self.sendMyData) #sends the data over to the server def sendMyData(self, fruitData): datapd = pickle.dumps(fruitData) theString = str(self.playerNumber) + ':' + datapd self.sendLine(theString) self.fruitQueue.get().addCallback(self.sendMyData) def connectionMade(self): self.client.fruitConn = self print 'Connection succesfully made to the server' self.sendLine('start') #self.transport.write('start') def closeConn(self): reactor.stop() #whenever data is received def lineReceived(self, data): #will happen if server only has one player connected (you) if data == 'waiting for players': print 'Waiting for another player..' elif data == 'lost conn': print 'Connection was lost, sorry' reactor.stop() #will happen if server has two connection. Will send your player number over (PN) along with random seed elif data.startswith('PN'): comp = data.split(':') self.playerNumber = int(comp[1]) #so that both clients have the same seed to create fruits self.randSeed = int(comp[2]) self.minFruitSpeed = int(comp[3]) self.maxFruitSpeed = int(comp[4]) self.client.blenderConn.playerNumber = self.playerNumber if self.playerNumber == 1: print 'Game Started: You are the Pink Player' else: print 'Game Started: You are the Green Player' #create the gamespace and start the loop self.gs = GameSpace(self, self.playerNumber, self.randSeed, self.minFruitSpeed, self.maxFruitSpeed) self.client.blenderConn.gs = self.gs self.gs.main() self.lc = LoopingCall(self.gs.countDown) self.lc.start(1 / 60) #start sending blender data continuously self.client.blenderConn.sendMyData() #when you receive gameplay data (will either be to add a fruit or freeze a fruit) else: self.parseData(data) def parseData(self, data): comp = data.split(':') fruitData = pickle.loads(comp[1]) if fruitData.dataType == 'create': self.gs.addFruit(fruitData.fruitInt, fruitData.xpos, fruitData.vspeed, fruitData.foodType, fruitData.fruitID) #freeze type else: if fruitData.freezeDirection == 'left': self.gs.freezeLeftFruitWithID(fruitData.freezeID) else: self.gs.freezeRightFruitWithID(fruitData.freezeID) #will be called after the countdown finishes def startGameLoop(self): self.lc.stop() self.lc = LoopingCall(self.gs.gameLoopIteration) self.lc.start(1 / 60) #will be called once someone wins def gameOver(self, text): self.lc = LoopingCall(self.gs.goToGameOver, (text)) self.lc.start(1 / 60) #called from gamespace whenever they click on a fruit on the right side of their screen def freezeLeftFruit(self, fruitID): fruitData = FruitData(dataType='freeze', freezeDirection='left', freezeID=fruitID) self.fruitQueue.put(fruitData) #called from gamespace whenever they click on a fruit on the left side of their screen def freezeRightFruit(self, fruitID): fruitData = FruitData(dataType='freeze', freezeDirection='right', freezeID=fruitID) self.fruitQueue.put(fruitData)
class FruitConn(LineReceiver): def __init__(self, client): self.client = client self.fruitQueue = DeferredQueue() self.fruitQueue.get().addCallback(self.sendMyData) # sends the data over to the server def sendMyData(self, fruitData): datapd = pickle.dumps(fruitData) theString = str(self.playerNumber) + ":" + datapd self.sendLine(theString) self.fruitQueue.get().addCallback(self.sendMyData) def connectionMade(self): self.client.fruitConn = self print "Connection succesfully made to the server" self.sendLine("start") # self.transport.write('start') def closeConn(self): reactor.stop() # whenever data is received def lineReceived(self, data): # will happen if server only has one player connected (you) if data == "waiting for players": print "Waiting for another player.." elif data == "lost conn": print "Connection was lost, sorry" reactor.stop() # will happen if server has two connection. Will send your player number over (PN) along with random seed elif data.startswith("PN"): comp = data.split(":") self.playerNumber = int(comp[1]) # so that both clients have the same seed to create fruits self.randSeed = int(comp[2]) self.minFruitSpeed = int(comp[3]) self.maxFruitSpeed = int(comp[4]) self.client.blenderConn.playerNumber = self.playerNumber if self.playerNumber == 1: print "Game Started: You are the Pink Player" else: print "Game Started: You are the Green Player" # create the gamespace and start the loop self.gs = GameSpace(self, self.playerNumber, self.randSeed, self.minFruitSpeed, self.maxFruitSpeed) self.client.blenderConn.gs = self.gs self.gs.main() self.lc = LoopingCall(self.gs.countDown) self.lc.start(1 / 60) # start sending blender data continuously self.client.blenderConn.sendMyData() # when you receive gameplay data (will either be to add a fruit or freeze a fruit) else: self.parseData(data) def parseData(self, data): comp = data.split(":") fruitData = pickle.loads(comp[1]) if fruitData.dataType == "create": self.gs.addFruit( fruitData.fruitInt, fruitData.xpos, fruitData.vspeed, fruitData.foodType, fruitData.fruitID ) # freeze type else: if fruitData.freezeDirection == "left": self.gs.freezeLeftFruitWithID(fruitData.freezeID) else: self.gs.freezeRightFruitWithID(fruitData.freezeID) # will be called after the countdown finishes def startGameLoop(self): self.lc.stop() self.lc = LoopingCall(self.gs.gameLoopIteration) self.lc.start(1 / 60) # will be called once someone wins def gameOver(self, text): self.lc = LoopingCall(self.gs.goToGameOver, (text)) self.lc.start(1 / 60) # called from gamespace whenever they click on a fruit on the right side of their screen def freezeLeftFruit(self, fruitID): fruitData = FruitData(dataType="freeze", freezeDirection="left", freezeID=fruitID) self.fruitQueue.put(fruitData) # called from gamespace whenever they click on a fruit on the left side of their screen def freezeRightFruit(self, fruitID): fruitData = FruitData(dataType="freeze", freezeDirection="right", freezeID=fruitID) self.fruitQueue.put(fruitData)