CodeCombat代码全记录(Python学习利器)--安息之云山峰(第四章)代码9
发布日期:2021-05-07 10:54:47 浏览次数:9 分类:原创文章

本文共 4625 字,大约阅读时间需要 15 分钟。

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.rectWidthrectHeight = centerUnit.rectHeight# First "soldier" to the left bottom corner of the rectangle.leftBottomX = center.x - rectWidth / 2leftBottomY = center.y - rectHeight / 2summonAndSend("soldier", leftBottomX, leftBottomY)# An "archer" to the left top corner.leftTopX = center.x - rectWidth / 2leftTopY = center.y + rectHeight / 2summonAndSend("archer", leftTopX, leftTopY)# Summon and send a "soldier" to the right top corner.rightTopX = center.x + rectWidth / 2rightTopY = center.y - rectHeight / 2summonAndSend("archer", rightTopX, rightTopY)# Summon and send an "archer" to the right bottom corner.rightBottomX = center.x + rectWidth / 2rightBottomY = center.y + rectHeight / 2summonAndSend("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 = 8soldierY = 8# The width and height of the formation.width = sergeant.rectWidthheight = sergeant.rectHeightfor 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 = 48archerY1 = 8# The coordinates of the top right corner.archerX2 = sniper.archerX2archerY2 = sniper.archerY2for 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 Truebombs = 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.xy1 = customer.leftBottom.yx2 = customer.rightTop.xy2 = customer.rightTop.ystep = 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)
上一篇:第三章 异常处理和DBhelper类
下一篇:第4章 Linq查询

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年03月22日 13时37分23秒