def __init__(self, data, x, y, team): Building.__init__(self, x, y, team) self.image = pygame.image.load(os.path.join('rts_wood_wall.png')) self.image = pygame.transform.scale(self.image, (30, 90)) self.rect = self.image.get_rect() self.xTileOffset = -1.6 self.yTileOffset = -1.6 self.rect.center = rts_helpers.coord2Pos( data, self.coords[0] + self.xTileOffset, self.coords[1] + self.yTileOffset) self.name = 'WoodWallX' self.prereqs = ['Barracks'] self.buildTime = 5 self.woodCost = 25 self.metalCost = 0 self.energy = 0 self.battery = 0 self.health = 1000 self.maxHealth = self.health self.layout = [[False, False, False], [False, True, False], [False, False, False]]
def __init__(self, data, x, y, team): Building.__init__(self, x, y, team) self.image = pygame.image.load(os.path.join('rts_barracks_frame.png')) self.image = pygame.transform.scale(self.image, [150, 113]) self.rect = self.image.get_rect() self.xTileOffset = -.8 self.yTileOffset = -.8 self.rect.center = rts_helpers.coord2Pos( data, self.coords[0] + self.xTileOffset, self.coords[1] + self.yTileOffset) self.name = 'BarracksX' self.prereqs = ['Farm'] self.rally_pointX = self.rect.center[0] + 100 self.rally_pointY = self.rect.center[1] + 100 self.createStartTime = 0 self.buildQueue = [] self.buildTime = 15 self.woodCost = 200 self.metalCost = 100 self.energy = 0 self.battery = 0 self.health = 1000 self.maxHealth = self.health self.layout = [[False, False, False, False, False], [False, True, True, True, False], [False, True, True, True, False], [False, True, True, True, False], [False, False, False, False, False]]
def __init__(self, data, x, y, team): Building.__init__(self, x, y, team) self.image = pygame.image.load(os.path.join('rts_farm_frame.png')) self.image = pygame.transform.scale(self.image, (50, 50)) self.rect = self.image.get_rect() self.xTileOffset = -1 self.yTileOffset = -.8 self.rect.center = rts_helpers.coord2Pos( data, self.coords[0] + self.xTileOffset, self.coords[1] + self.yTileOffset) self.name = 'FarmX' self.prereqs = ['CommandCenter'] self.buildTime = 8 self.woodCost = 150 self.metalCost = 0 self.energy = 0 self.battery = 0 self.supply = 5 self.health = 500 self.maxHealth = self.health self.layout = [[False, False, False, False], [False, True, True, False], [False, False, False, False]]
def __init__(self, data, x, y, team): Building.__init__(self, x, y, team) self.image = pygame.image.load( os.path.join('rts_geothermal_generator_construction.png')) self.image = pygame.transform.scale(self.image, (25, 50)) self.rect = self.image.get_rect() self.xTileOffset = -.8 self.yTileOffset = -.8 self.rect.center = rts_helpers.coord2Pos( data, self.coords[0] + self.xTileOffset, self.coords[1] + self.yTileOffset) self.name = 'GeothermalGeneratorX' self.prereqs = ['CommandCenter'] self.buildTime = 5 self.woodCost = 0 self.metalCost = 100 self.energy = .05 self.battery = 5 self.health = 500 self.maxHealth = self.health self.layout = [[False, False, False], [False, True, False], [False, False, False]]
def update(self, data): self.rect.center = rts_helpers.coord2Pos( data, self.coords[0] + self.xTileOffset, self.coords[1] + self.yTileOffset) if (self.health <= 0): for tile in self.tiles: data.board[tile[0]][tile[1]] = 'field' self.kill() msg = 'destroyBuilding %d %s \n' % (self.ID, self.tiles) if (data.startMenuState != 'Singleplayer'): data.server.send(msg.encode())
def __init__(self, data, x, y, team): Building.__init__(self, x, y, team) if (self.team == 'yellow'): self.image = pygame.image.load( os.path.join('rts_beacon_base_yellow.png')) elif (self.team == 'blue'): self.image = pygame.image.load( os.path.join('rts_beacon_base_blue.png')) elif (self.team == 'red'): self.image = pygame.image.load( os.path.join('rts_beacon_base_red.png')) elif (self.team == 'green'): self.image = pygame.image.load( os.path.join('rts_beacon_base_green.png')) self.image = pygame.transform.scale(self.image, (200, 145)) self.rect = self.image.get_rect() self.xTileOffset = -1.5 self.yTileOffset = -1.5 self.rect.center = rts_helpers.coord2Pos( data, self.coords[0] + self.xTileOffset, self.coords[1] + self.yTileOffset) self.name = 'BeaconX' self.prereqs = ['Farm', 'GeothermalGenerator'] self.buildTime = 15 self.woodCost = 300 self.metalCost = 500 self.health = 2500 self.maxHealth = self.health self.energy = -.25 self.battery = 500 self.ringHeight = self.rect.height // 2 + 20 self.maxRingHeight = self.rect.height * 1.5 self.winGoal = 10 * 60 self.layout = [[False, False, False, False, False, False], [False, True, True, True, True, False], [False, True, True, True, True, False], [False, True, True, True, True, False], [False, True, True, True, True, False], [False, False, False, False, False, False]]
def build(self, data): if (self.buildState == 'Select'): self.stencil = rts_helpers.compileBuildStencil( data, self.building.layout) elif (self.buildState == 'Place'): for tile in self.stencil: if (tile[0] == False): self.buildState = 'Select' if (self.buildState == 'Place'): if (rts_helpers.buildReqsMet(data, self.building)): newCoords = rts_helpers.placeBuilding(data, self.building) self.building.coords = newCoords self.building.rect.center = rts_helpers.coord2Pos( data, newCoords[0], newCoords[1]) self.building.rally_pointX = self.building.rect.center[0] self.building.rally_pointY = self.building.rect.center[ 1] + 100 self.buildState = 'Build' data.localPlayer.inConstruction.add(self.building) self.stencil = None data.localPlayer.metals -= self.building.metalCost data.localPlayer.wood -= self.building.woodCost self.building.update(data) buildingID = random.randint(1000000, 9999999) data.localPlayer.IDs.add(buildingID) self.building.ID = buildingID tiles = str(self.building.tiles).replace(' ', '') msg = 'buildBuilding %s %d %d %d %s \n' % ( self.building.name, self.building.coords[0], self.building.coords[1], buildingID, tiles) if (data.startMenuState != 'Singleplayer'): data.server.send(msg.encode()) else: self.buildState = None self.stencil = None self.building = None elif (self.buildState == 'Build'): pass
def __init__(self, data, x, y, team): Building.__init__(self, x, y, team) self.image = pygame.image.load( os.path.join('rts_command_center_frame.png')) self.image = pygame.transform.scale(self.image, (200, 147)) self.rect = self.image.get_rect() self.rect.center = rts_helpers.coord2Pos(data, self.coords[0], self.coords[1]) self.xTileOffset = -1.5 self.yTileOffset = -1.5 self.name = 'CommandCenterX' self.prereqs = [] self.rally_pointX = self.rect.center[0] self.rally_pointY = self.rect.center[1] + 100 self.createStartTime = 0 self.buildQueue = [] self.energy = .01 self.battery = 100 self.buildTime = 15 self.woodCost = 300 self.metalCost = 200 self.health = 2000 self.maxHealth = self.health self.layout = [[False, False, False, False, False, False], [False, True, True, True, True, False], [False, True, True, True, True, False], [False, True, True, True, True, False], [False, True, True, True, True, False], [False, False, False, False, False, False]]
def interpServerCommands(data): if (data.serverMsg.qsize() > 0): msg = data.serverMsg.get(False) try: print("received: ", msg, "\n") msg = msg.split() command = msg[0] #command given upon server connection if (command == "myIDis"): ID = msg[1] data.localPlayer.ID = ID if (ID == 'alpha'): data.localPlayer.role = 'Host' else: data.localPlayer.role = 'Client' #command given when another player enters server elif (command == 'newPlayer'): ID = msg[1] data.otherUsers[ID] = rts_classes.Player(data.usernameInput) if (ID == 'alpha'): data.otherUsers[ID].role = 'Host' update = '' update += 'newUsername %s \n' % data.usernameInput update += 'newTeam %s \n' % data.localPlayer.team data.server.send(update.encode()) #command given upon lobby entrance elif (command == 'newUsername'): ID = msg[1] username = msg[2] data.otherUsers[ID].username = username #change teams in lobby elif (command == 'newTeam'): ID = msg[1] team = msg[2] data.otherUsers[ID].team = team #given when host starts game elif (command == 'startGame'): data.playButtonPressed = True #given when host is done compiling random board and sends out info elif (command == 'board'): ID = msg[1] board = msg[2] data.board = board data.boardComplete = True #whenever Player class creates drone elif (command == 'createDrone'): ID = msg[1] x = int(msg[2]) y = int(msg[3]) coord = rts_helpers.coord2Pos(data, x, y) desX = int(msg[4]) desY = int(msg[5]) desCoord = rts_helpers.coord2Pos(data, desX, desY) unitID = eval(msg[6]) #possible values are 7 digit int or None user = data.otherUsers[ID] user.createDrone(data, coord[0], coord[1], desCoord[0], desCoord[1], True, unitID) elif (command == 'createMilitia'): ID = msg[1] x = int(msg[2]) y = int(msg[3]) coord = rts_helpers.coord2Pos(data, x, y) desX = int(msg[4]) desY = int(msg[5]) desCoord = rts_helpers.coord2Pos(data, desX, desY) unitID = eval(msg[6]) #possible values are 7 digit int or None user = data.otherUsers[ID] user.createMilitia(data, coord[0], coord[1], desCoord[0], desCoord[1], True, unitID) #whenever unit destination changes elif (command == 'moveUnit'): ID = msg[1] desX = int(msg[2]) desY = int(msg[3]) unitID = eval(msg[4]) #possible values are 7 digit int or None for unit in data.otherUsers[ID].units: if (unit.ID == unitID): unit.desX = unit.rect.center[0] - desX unit.desY = unit.rect.center[1] - desY #whenever drone builds building elif (command == 'buildBuilding'): ID = msg[1] building = msg[2] xCoord = int(msg[3]) yCoord = int(msg[4]) buildingID = int(msg[5]) #evals string of list of tuples tiles = eval(msg[6]) user = data.otherUsers[ID] if (building == 'CommandCenterX'): newBuilding = rts_buildings.CommandCenter( data, xCoord, yCoord, user.team) elif (building == 'GeothermalGeneratorX'): newBuilding = rts_buildings.GeothermalGenerator( data, xCoord, yCoord, user.team) elif (building == 'FarmX'): newBuilding = rts_buildings.Farm(data, xCoord, yCoord, user.team) elif (building == 'BarracksX'): newBuilding = rts_buildings.Barracks( data, xCoord, yCoord, user.team) elif (building == 'WoodWallX'): newBuilding = rts_buildings.WoodWallX( data, xCoord, yCoord, user.team) newBuilding.ID = buildingID newBuilding.tiles = tiles user.inConstruction.add(newBuilding) for tile in tiles: data.board[tile[0]][tile[1]] = user.team #whenever player right clicks elif (command == 'newTarget'): ID = msg[1] unitID = int(msg[2]) targetID = eval(msg[3]) #possible values are int or None user = data.otherUsers[ID] for unit in user.units: if (unit.ID == unitID): if (targetID != None): unit.target = rts_helpers.findUnitByID( data, targetID) else: unit.target = None elif (command == 'winCondition'): ID = msg[1] condition = msg[2] data.otherUsers[ID].winCondition = condition elif (command == 'destroyBuilding'): ID = msg[1] unitID = int(msg[2]) #evals string of list of tuples tiles = eval(msg[3]) for tile in tiles: data.board[tile[0]][tile[1]] = 'field' unit = rts_helpers.findUnitByID(data, unitID) unit.kill() elif (command == 'destroyUnit'): ID = msg[1] unitID = int(msg[2]) unit = rts_helpers.findUnitByID(data, unitID) unit.kill() elif (command == 'cutTree'): ID = msg[1] x = msg[2] y = msg[3] for tree in data.trees: if (tree.x == x and tree.y == y): tree.kill() data.board[x][y] = 'field' elif (command == 'ready'): ID = msg[1] data.otherUsers[ID].multiplayerReady = True #whenever a unit/building is dealt damage # elif(command=='damageDealt'): # ID=msg[1] # unitID=int(msg[2]) # damage=int(msg[3]) # unit=rts_helpers.findUnitByID(data,unitID) # unit.health-=damage except: print("failed") data.serverMsg.task_done()
def generateMapPos(data): point0 = rts_helpers.coord2Pos(data, *data.mapCoords[0], 'tile')[0] point1 = rts_helpers.coord2Pos(data, *data.mapCoords[1], 'tile')[1] point2 = rts_helpers.coord2Pos(data, *data.mapCoords[2], 'tile')[2] point3 = rts_helpers.coord2Pos(data, *data.mapCoords[3], 'tile')[3] return (point0, point1, point2, point3)