def initNewGame(self,playerData): self.unitSet = TreeContainer(10,10,self.map.groundwidth-20,self.map.groundheight-20) self.running = False self.players = [Player(i) for i in range(2)] for player in self.players: player.money = 30000 for player in playerData: data = playerData[player] flag = data["flag"] col,row = data["position"] mcv = MCV(flag) self.addUnitGrid(mcv,col,row) for unitdata in data["initials"]: name = unitdata["name"] x,y = unitdata["pos"] animation = unitdata["animation"] x,y = x+mcv.offsetx,y+mcv.offsety self.addUnit(classmap[name](flag,animation),x,y) self.updatePosition()
class Game(): def __init__(self,map): self.map = map self.unitSet = None self.x = map.x self.y = map.y self.running = False self.players = [] def initNewGame(self,playerData): self.unitSet = TreeContainer(10,10,self.map.groundwidth-20,self.map.groundheight-20) self.running = False self.players = [Player(i) for i in range(2)] for player in self.players: player.money = 30000 for player in playerData: data = playerData[player] flag = data["flag"] col,row = data["position"] mcv = MCV(flag) self.addUnitGrid(mcv,col,row) for unitdata in data["initials"]: name = unitdata["name"] x,y = unitdata["pos"] animation = unitdata["animation"] x,y = x+mcv.offsetx,y+mcv.offsety self.addUnit(classmap[name](flag,animation),x,y) self.updatePosition() def addUnit(self,unit,x,y): if self.unitSet.addPos(unit,x,y): self.players[unit.player]._addUnit(unit) return True return False def addUnitGrid(self,unit,col,row): if self.unitSet.addGrid(unit,col,row): self.players[unit.player]._addUnit(unit) return True return False def removeUnit(self,unit): self.unitSet.remove(unit) self.players[unit.player]._removeUnit(unit) def available(self,unit,x,y): return self.unitSet.available(unit,x,y) def step(self): unitlist = [] for unit in self.unitSet: unitlist.append(unit) for unit in unitlist: unit.step(self.map,self) if hasattr(unit,"replace") and unit.replace != None: name = unit.replace newunit = classmap[name](unit.player) newunit.HP = unit.HP newunit.fullHP = unit.fullHP if isinstance(newunit,Building): offsetx,offsety = getGridCenter(unit.offsetx,unit.offsety) offsetx += modify[name][0] offsety += modify[name][1] self.removeUnit(unit) if not self.addUnit(newunit,offsetx,offsety): self.addUnit(unit) def updatePosition(self): self.x = self.map.x self.y = self.map.y for unit in self.unitSet: unit.x = self.x + unit.offsetx unit.y = self.y + unit.offsety def update(self): self.updatePosition() if self.running: for player in self.players: player._update() if player.createIsReady["infantry"] and player.mainGpile != None: player.createMobUnit(player.mainGpile,"infantry",self) if player.createIsReady["vehicle"] and player.mainGweap != None: player.createMobUnit(player.mainGweap,"vehicle",self) self.step() def get_battle_rect(): return pygame.Rect(0,0,battlewidth,battleheight) def isEmpty(self,col,row): if not self.map.validFullPos(col,row): return False x,y = getAbsPos(col,row,True) return self.unitSet.availableSize(6,x,y) def inarea(self,unit): rect = unit.get_rect().copy() center = rect.center rect.width *= 3 rect.height *= 3 rect.center = center return rect.colliderect(pygame.Rect(0,0,battlewidth,battleheight)) def draw(self,screen): self.updatePosition() units = [] for unit in self.unitSet: units.append(unit) units.sort(key=lambda unit: unit.offsety) for unit in units: if self.inarea(unit): unit.draw(screen) def onMouseDown(self,x,y,button): for unit in self.unitSet: unit.onMouseDown(x,y,button) def onMouseUp(self,x,y,button): pass def onMouseMove(self,x,y,button1=None,button2=None,button3=None): pass