def setMap(self, mapFileName, x, y): if not self.displayMap: print "Entering map %s" % (mapFileName) self.mode = "game" self.update = self.updateGame self.name = self.loginGui.name self.displayMap = Map(mapFileName) self.addPlayer(self.name, x, y) else: if self.displayMap.filename == mapFileName: print "Warping in current map (%s)" % (mapFileName) self.displayMap.players[self.name].setPos(x, y) else: print "Changing map for %s" % (mapFileName) self.displayMap = Map(mapFileName) self.addPlayer(self.name, x, y)
class Game(GameClient): def __init__(self, host, port): self.screen = SCREEN self.host = host self.port = port pygame.init() self.connectToServer() self.kh = KeyHandler() self.dx = 0 self.dy = 0 self.prevMove = (0,0) self.running = True self.sendPosCooldown = 0.0 self.prevTime = 0.0 self.speed = 0.05 self.displayMap = None # GUI self.gameGui = GameGUI(self) self.loginGui = LoginGUI(self) self.mode = "login" self.update = self.updateLogin def connectToServer(self): GameClient.__init__(self, self.host, self.port) def addPlayer(self, name, x, y): if name == "anonymous": return print "adding player to map : %s, at %s, %s" % (name, x, y) self.displayMap.addPlayer(name, x, y) def delPlayer(self, name): self.displayMap.delPlayer(name) def addMob(self, name, x=50.0, y=50.0): self.displayMap.addMob(name, 1, x, y) def delMob(self, name): self.displayMap.delMob(name) def setMap(self, mapFileName, x, y): if not self.displayMap: print "Entering map %s" % (mapFileName) self.mode = "game" self.update = self.updateGame self.name = self.loginGui.name self.displayMap = Map(mapFileName) self.addPlayer(self.name, x, y) else: if self.displayMap.filename == mapFileName: print "Warping in current map (%s)" % (mapFileName) self.displayMap.players[self.name].setPos(x, y) else: print "Changing map for %s" % (mapFileName) self.displayMap = Map(mapFileName) self.addPlayer(self.name, x, y) def getClosestMobName(self): myRect = self.displayMap.players[self.name].mapRect minDist = 2000.0 closestMob = None for mobName, mob in self.displayMap.mobs.items(): dist = getDist(mob.mapRect, myRect) if dist < minDist: minDist = dist closestMob = mobName return closestMob def sitPlayer(self): sprite = self.displayMap.players[self.name]._sprite if not sprite.sitting: self.SendSitRequest() def addDirtyRect(self, rect): if not self.displayMap: return self.displayMap.addDirtyRect(rect) def updateNetwork(self): self.Loop() def updateLogin(self): self.updateNetwork() self.loginGui.update() def updateGame(self): self.updateNetwork() #time.sleep(0.015) t = pygame.time.get_ticks() x, y = pygame.mouse.get_pos() dt = t - self.prevTime #if dt<10: # return #print "update time : dt = %s" % (dt) self.prevTime = t self.prevMove = (self.dx, self.dy) if self.name not in self.displayMap.players: #print "not connected to map" return events = pygame.event.get() self.kh.handleEvents(events) moved = False # keyboard handling # player direction #self.dx = self.kh.keyDict[KEY_RIGHT] - self.kh.keyDict[KEY_LEFT] #self.dy = self.kh.keyDict[KEY_DOWN] - self.kh.keyDict[KEY_UP] self.dx = pygame.key.get_pressed()[KEY_RIGHT] - pygame.key.get_pressed()[KEY_LEFT] self.dy = pygame.key.get_pressed()[KEY_DOWN] - pygame.key.get_pressed()[KEY_UP] if not self.gameGui.chatWindow.entry.has_focus: if (self.prevMove != (self.dx, self.dy)):# or (t>self.sendPosCooldown): self.displayMap.players[self.name].setMovement(self.dx, self.dy) #self.sendPosCooldown = t+25 #print "Player direction changed from %s to %s/%s" % (self.prevMove, self.dx, self.dy) self.SendUpdateMove(self.displayMap.players[self.name].x, self.displayMap.players[self.name].y, self.dx, self.dy) else: self.dx = 0 self.dy = 0 offx = self.displayMap.players[self.name].mapRect.x-SCREEN_WIDTH/2 offy = self.displayMap.players[self.name].mapRect.y-SCREEN_HEIGHT/2 self.displayMap.setOffset(offx, offy) self.displayMap.update(dt) for event in events: if event.type == pygame.KEYDOWN: if self.gameGui.chatWindow.entry.has_focus: continue key = event.key if key == pygame.K_ESCAPE: #print "Escape and no typing : quit" #pygame.quit() self.running = False if key == pygame.K_SPACE: #print "Starting to type text..." self.SendWarpInfoRequest() self.displayMap.warpVisible = not self.displayMap.warpVisible self.displayMap.collisionVisible = not self.displayMap.collisionVisible self.displayMap.needFullBlit = True if key == KEY_SELECT_TARGET: mobName = self.getClosestMobName() if mobName: self.displayMap.selectTarget(mobName) else: self.displayMap.unselectTarget() if key == KEY_ATTACK: if self.displayMap.selected: self.SendAttackMob(self.displayMap.selected) if key == KEY_SIT: self.sitPlayer() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: self.displayMap.handleClick() elif event.type == pygame.QUIT: #pygame.quit() self.running = False self.gameGui.handleEvents(events) # graphics self.displayMap.blit(self.screen) # gui display self.gameGui.blit() pygame.display.flip()
def load(self, filename): self.filename = filename self.map = Map(filename)
def new(self, x, y): self.map = Map("new") self.map.setSize(x, y)
class MapEditor(object): def __init__(self, screen = SCREEN): self.map = None self.currentTileCode = "wwww" self.dragging = False self.dragOriginX = 0 self.dragOriginY = 0 if screen: self.screen = screen else: self.screen = pygame.display.set_mode((800,600)) self.currentLayer = "ground" def open(self, filename): self.load(filename) def load(self, filename): self.filename = filename self.map = Map(filename) def save(self, filename): if not self.map:return self.map.save(filename) def new(self, x, y): self.map = Map("new") self.map.setSize(x, y) def random(self): self.map.setSize(80, 60) randint = random.randint #making the ocean for y in range(self.map.h): for x in range(self.map.w): self.drawTile(self.currentLayer, x, y) #each vulcan generates a small island vulcans = randint(10,30) ndirties = randint(1,3) nroads = randint(1,3) for n in range(vulcans): #used to kill slow process self.starttime = clock() print "Generating islands %d" % (n+1,) self.islandspoints = [] x, y = randint(1, self.map.w-1), randint(1, self.map.h-1) radius = randint(1,30) self.randomIsland(x, y, radius) print "Islands %d generated" % (n+1,) self.toggleTileCode() #dirty areas generation to fix for n in range(ndirties): if len(self.islandspoints) < 1: break print "Adding dirty area number", n+1 source = list(self.islandspoints[randint(0, len(self.islandspoints)-1)]) dest = list(self.islandspoints[randint(0, len(self.islandspoints)-1)]) dx, dy = cmp(dest[0] - source[0], 0), cmp(dest[1] - source[1], 0) while source != dest and 0 < source[0] < self.map.w and 0 < source [1] < self.map.h: if source[0] != dest[0]: source[0] = source[0] + dx if source[1] != dest[1]: source[1] = source[1] + dy self.drawTile(self.currentLayer, source[0], source[1]) print "Dirty areas added" self.toggleTileCode() #roads generation to fix #for n in range(nroads): # if len(self.islandspoints) < 1: # break # print "Adding road number", n # source = list(self.islandspoints[randint(0, len(self.islandspoints)-1)]) # dest = list(self.islandspoints[randint(0, len(self.islandspoints)-1)]) # dx, dy = cmp(dest[0] - source[0], 0), cmp(dest[1] - source[1], 0) # while source != dest and 0 < source[0] < self.map.w and 0 < source [1] < self.map.h: # if source[0] != dest[0]: # source[0] = source[0] + dx # if source[1] != dest[1]: # source[1] = source[1] + dy # self.drawTile(self.currentLayer, source[0], source[1]) #del self.islandspoints #print "Roads added" self.toggleTileCode() #self.toggleTileCode() def randomIsland(self, x, y, radius): #killing the process if it takes too much which also add some random shape if clock() - self.starttime > 5.0: return try: #tree pruning if radius < 1 or self.map.layers["ground"].getTile(x, y) == "gggg": return except: #skipping positions outside the map return self.drawGrass(self.currentLayer, x, y) if radius > 2: self.islandspoints.append((x,y)) self.randomIsland(x-1, y, radius-1) self.randomIsland(x-1, y-1, radius-1) self.randomIsland(x-1, y+1, radius-1) self.randomIsland(x+1, y, radius-1) self.randomIsland(x+1, y-1, radius-1) self.randomIsland(x+1, y+1, radius-1) self.randomIsland(x, y-1, radius-1) self.randomIsland(x, y+1, radius-1) def setSize(self, x, y): if not self.map:return self.map.setSize(x, y) def getMouseTilePos(self, x, y): tx = (x+self.map.offsetX)/self.map.tileWidth ty = (y+self.map.offsetY)/self.map.tileHeight return tx, ty def startDrag(self): if not self.map:return print "started to drag map" self.dragging = True x, y = pygame.mouse.get_pos() self.dragOriginX = self.map.offsetX + x self.dragOriginY = self.map.offsetY + y def stopDrag(self): print "stopped dragging" self.dragging = False def update(self, events = []): if not self.map:return x, y = pygame.mouse.get_pos() if self.dragging: self.map.setOffset(self.dragOriginX-x, self.dragOriginY-y) #print "setting map offset : %s %s" % (self.map.offsetX, self.map.offsetY) tx, ty = self.getMouseTilePos(x, y) if pygame.mouse.get_pressed()[0]==1: self.drawTile(self.currentLayer, tx, ty) elif pygame.mouse.get_pressed()[2]==1: self.drawGrass(self.currentLayer, tx, ty) for event in events: if event.type == pygame.MOUSEBUTTONDOWN: if pygame.mouse.get_pressed()[1]==1 and not self.dragging: self.startDrag() elif event.type == pygame.MOUSEBUTTONUP: if self.dragging: self.stopDrag() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: self.toggleTileCode() elif event.key == pygame.K_s: self.save("maps/testmap2.txt") elif event.key == pygame.K_o: self.open("maps/testmap2.txt") elif event.key == pygame.K_r: self.setSize(40,20) elif event.key == pygame.K_g: self.random() #self.screen.fill((0,0,0)) self.map.blit(self.screen) pygame.display.update() def blit(self, dest): self.map.blit(dest) def drawTile(self, layerName, x, y): if not self.map:return self.map.setTile(layerName, x, y, self.currentTileCode) self.map.needFullBlit = True def drawGrass(self, layerName, x, y): if not self.map:return self.map.setTile(layerName, x, y, "gggg") self.map.needFullBlit = True def setTileCode(self, code): self.currentTileCode = code def toggleTileCode(self): if self.currentTileCode == "wwww": self.setTileCode("dddd") elif self.currentTileCode == "dddd": self.setTileCode("rrrr") else: self.setTileCode("wwww")