Humantron
# Form the rectangle of units around the peasant. # You need 2 archers and 2 soldiers. # This function can be helpful. def summonAndSend(type, x, y): hero.summon(type) unit = hero.built[len(hero.built)-1] hero.command(unit, "move", {"x": x, "y": y}) # The rectangle should be formed around the peasant. centerUnit = hero.findNearest(hero.findFriends()) # It's the center of the rectangle. center = centerUnit.pos # Also you need the height and width. rectWidth = centerUnit.rectWidth rectHeight = centerUnit.rectHeight # First "soldier" to the left bottom corner of the rectangle. leftBottomX = center.x - rectWidth / 2 leftBottomY = center.y - rectHeight / 2 summonAndSend("soldier", leftBottomX, leftBottomY) # An "archer" to the left top corner. leftTopX = center.x - rectWidth / 2 leftTopY = center.y + rectHeight / 2 summonAndSend("archer", leftTopX, leftTopY) # Summon and send a "soldier" to the right top corner. rightTopX = center.x + rectWidth / 2 rightTopY = center.y - rectHeight / 2 summonAndSend("archer", rightTopX, rightTopY) # Summon and send an "archer" to the right bottom corner. rightBottomX = center.x + rectWidth / 2 rightBottomY = center.y + rectHeight / 2 summonAndSend("soldier", rightBottomX, rightBottomY) # Now hide or fight.Rectangle Formation
# Form up soldiers and archers in rectangle formations. # The distance between units. step = 8 # First form up soldiers. sergeant = hero.findNearest(hero.findByType("soldier")) # The coordinates of the bottom left corner. soldierX = 8 soldierY = 8 # The width and height of the formation. width = sergeant.rectWidth height = sergeant.rectHeight for x in range(soldierX, soldierX + width + 1, 8): for y in range(soldierY, soldierY + height + 1, 8): hero.summon("soldier") lastUnit = hero.built[len(hero.built) - 1] # Command the last built unit by using the x/y variables: hero.command(lastUnit, 'move', {'x': x, 'y': y}) # Next form up archers. sniper = hero.findNearest(hero.findByType("archer")) # The coordinates of the bottom left corner. archerX1 = 48 archerY1 = 8 # The coordinates of the top right corner. archerX2 = sniper.archerX2 archerY2 = sniper.archerY2 for x in range(archerX1, archerX2 + 1, 8): for y in range(archerY1, archerY2 + 1, 8): # Summon an archer. hero.summon("archer") # Find the last built unit. lastUnit = hero.built[len(hero.built) - 1] # Command the last built unit by using the x/y variables: hero.command(lastUnit, 'move', {'x': x, 'y': y})Area of Yetis 个人感觉本关的提示存在问题,在最后末尾若不加英雄攻击的代码,那个士兵也会自动攻击雪人,只要你说出了面试。
# Defeat Yetis. yets = hero.findEnemies()[0] # The chosen one can stun yetis with a Shout. chosen = hero.findFriends()[0] # The power of the Shout is equal to the area of a circle. radius = chosen.distanceTo(chosen.findNearestEnemy()) # The area of a circle can be calculated with the formula: area = radius * radius * Math.PI # Tell the area to the chosen one. hero.say(area) # Don't just stand there! Fight! hero.attack(yets)Safe Spot
# Shout to activate the bombs and move to the entrance. # The function checks if numbers are almost equal. def almostEqual(n1, n2): return abs(n1 - n2) <= 0.5 # The function checks that all # thangs are on the same distance from the hero. def allSameDistance(thangs): if len(thangs) == 0: return True # We can use any thang as an etalon. etalon = hero.distanceTo(thangs[0]) # Iterate all thangs: for thang in thangs: # Use almostEqual to compare `etalon` and the distance to `unit`. distance = hero.distanceTo(thang) # If they are not equal (almost): if not almostEqual(distance, etalon): # Return False. return False # All the same. Return True. return True bombs = hero.findEnemies() for x in range(36, 45): for y in range(30, 39): hero.moveXY(x, y) if allSameDistance(bombs): # It's a safe spot. Rock'n'Roll! hero.say("HEEEEEEEEEY!!!") hero.moveXY(40, 58) hero.say("Heh. Nothing.");Fence Builder
# Build a fence around the farm. # Take coordinates of the opposite corners. customer = hero.findNearest(hero.findFriends()) x1 = customer.leftBottom.x y1 = customer.leftBottom.y x2 = customer.rightTop.x y2 = customer.rightTop.y step = 4 # Let's build the left side. for y in range(y2 - step, y1 - 1, -step): hero.buildXY("fence", x1, y) # Build the bottom side. for x in range(x1 + step, x2 + 1, step): hero.buildXY("fence", x, y1) # Then the right side. for y in range(y1 + step, y2 + 1, step): hero.buildXY("fence", x2, y) # Build the top side. for x in range(x2 - step, x1 - 1, -step): hero.buildXY("fence", x, y2)