def doNet(self): #if isServer: # checktimeouts() while simplenet.checkdata(): packet, peer = simplenet.nextpacket() if packet is None: break pType = packet[0] pData = packet[1] #print pType, pData if simplenet.isServer: cl = self.findclient(peer) if cl != None: cl.lastpackettime = 0 if pType=="join": cl = self.addClient(pData, peer) if cl is None: simplenet.sendto("joinfailed", "alreadyjoined?", peer) else: simplenet.sendto("yourein", "dosomethingcool", peer) elif cl is None: simplenet.sendto("whoyou", "i didnt get join message", peer) elif pType == "event": self.handleEvent(pData, cl) else:#client if pType=="gamestate": self.gamestate = pData self.clients = self.gamestate.clients self.walkers = self.clients for w in range(len(self.walkers)): if self.walkers[w].name == self.name: self.mywalker = self.walkers[w] elif pType=="boom": terrain.explode(pData[0], pData[1], pData[2], pData[3]) elif pType=="pastetex": terrain.pastesurfID(pData[0], pData[1], pData[2], pData[3], pData[4]) elif pType=="addtps": particles.addTextParticle(pData[0], pData[1], pData[2], pData[3], pData[4], pData[5]) elif pType=="addps": particles.addParticle(pData[0], pData[1], pData[2], pData[3], pData[4], pData[5], pData[6]) #print pData elif pType=="msg": #addmsg(pData[0], pData[1]) print(pData) elif pType=="whoyou": simplenet.sendserv("join", self.name) elif pType=="wakeup": simplenet.sendserv("keepalive", "dude")
def handleEvent(self, event): if event.type == 2:#keypress if event.key == pgl.K_r: terrain.loadimg("clev") if event.key == pgl.K_w: simplenet.isServer = 0 simplenet.isClient = 0 return True if event.key == pgl.K_s: simplenet.isServer = 1 simplenet.isClient = 0 simplenet.initNet() return True if event.key == pgl.K_c: simplenet.isServer = 0 simplenet.isClient = 1 self.name = "nameless"+str(random()*1000)[0:3] simplenet.sendserv("join", self.name) return True if event.key == pgl.K_h: self.menu.currentMenu = self.menu.helpMenu
def handleEvent(self, event, cplayer): if event.type == pygame.QUIT: sys.exit() elif event.type == pgl.VIDEORESIZE: print(event.dict['size']) display.resize(event.dict['size'][0], event.dict['size'][1]) pygame.display.flip() elif simplenet.isClient: ne = netevent() ne.type = event.type if event.type == 2 or event.type == 3: if event.key == pgl.K_ESCAPE: sys.exit() ne.key = event.key simplenet.sendserv("event", ne) elif event.type == pgl.MOUSEMOTION: ne.pos = [event.pos[0], event.pos[1]] simplenet.sendserv("event", ne) elif event.type == pgl.MOUSEBUTTONDOWN: ne.button = event.button ne.pos = [event.pos[0], event.pos[1]] simplenet.sendserv("event", ne) elif event.type == pgl.MOUSEBUTTONUP: ne.button = event.button ne.pos = [event.pos[0], event.pos[1]] simplenet.sendserv("event", ne) return if event.type == 2:#keypress if event.key == pgl.K_DELETE: if len(self.walkers) > 0:self.walkers.remove(self.walkers[0]) if event.key == pgl.K_w: #for i in range(0, len(self.walkers)):self.walkers[i].upkey = 1 cplayer.upkey = 1 if event.key == pgl.K_ESCAPE: sys.exit() if event.key == pgl.K_m: self.menu.runMenu() if event.key == pgl.K_s: cplayer.dnkey = 1 if event.key == pgl.K_a: cplayer.ltkey = 1 if event.key == pgl.K_d: cplayer.rtkey = 1 if event.key == pgl.K_SPACE: cplayer.jumpkey = 1 if event.key == pgl.K_LCTRL: cplayer.crouchkey = 1 if event.key == pgl.K_v: cplayer.digkey = 1 if event.key == pgl.K_q: cplayer.changegun(1) if event.key == pgl.K_e: cplayer.changegun(-1) if event.type == 3:#keyrelease if event.key == pgl.K_n: newplayer = player.playerclass(self.gamestate, "A Bot " + str(len(self.gamestate.clients)), None) newplayer.isbot = 1#int(random()*2) newplayer.jumpkey = int(random()*2) newplayer.x = cplayer.gmx newplayer.y = cplayer.gmy self.walkers.append(newplayer) if event.key == pgl.K_w: cplayer.upkey = 0 if event.key == pgl.K_s: cplayer.dnkey = 0 if event.key == pgl.K_a: cplayer.ltkey = 0 if event.key == pgl.K_d: cplayer.rtkey = 0 if event.key == pgl.K_SPACE: cplayer.jumpkey = 0 if event.key == pgl.K_LCTRL: cplayer.crouchkey = 0 if event.key == pgl.K_v: cplayer.digkey = 0 if event.type == pgl.MOUSEMOTION or event.type == pgl.MOUSEBUTTONUP or event.type == pgl.MOUSEBUTTONDOWN: cplayer.msinx = event.pos[0] cplayer.msiny = event.pos[1] #print event.type if event.type == pgl.MOUSEBUTTONUP: if event.button == 1: cplayer.unshoot()#jumpkey = 1 if event.button == 3: cplayer.altunshoot()#jumpkey = 1 if event.type == pgl.MOUSEBUTTONDOWN: #print event.button if event.button == 4: cplayer.changegun(1) if event.button == 5: cplayer.changegun(-1) if event.button == 3: cplayer.altshoot() #terrain.explode(cplayer.gmx, cplayer.gmy, 90, 255) if event.button == 1: cplayer.shoot()#jumpkey = 1